mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-08-28 04:46:29 -04:00
Use upstream indent & padding writers from reflow
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AnsiWriter struct {
|
||||
Forward io.Writer
|
||||
|
||||
ansi bool
|
||||
ansiseq string
|
||||
lastseq string
|
||||
seqchanged bool
|
||||
}
|
||||
|
||||
// Write is used to write content to the ANSI buffer.
|
||||
func (w *AnsiWriter) Write(b []byte) (int, error) {
|
||||
for _, c := range string(b) {
|
||||
if c == '\x1B' {
|
||||
// ANSI escape sequence
|
||||
w.ansi = true
|
||||
w.seqchanged = true
|
||||
w.ansiseq += string(c)
|
||||
} else if w.ansi {
|
||||
w.ansiseq += string(c)
|
||||
if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
||||
// ANSI sequence terminated
|
||||
w.ansi = false
|
||||
|
||||
_, _ = w.Forward.Write([]byte(w.ansiseq))
|
||||
if strings.HasSuffix(w.ansiseq, "[0m") {
|
||||
// reset sequence
|
||||
w.lastseq = ""
|
||||
} else if strings.HasSuffix(w.ansiseq, "m") {
|
||||
// color code
|
||||
w.lastseq = w.ansiseq
|
||||
}
|
||||
w.ansiseq = ""
|
||||
}
|
||||
} else {
|
||||
_, err := w.Forward.Write([]byte(string(c)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (w *AnsiWriter) LastSequence() string {
|
||||
return w.lastseq
|
||||
}
|
||||
|
||||
func (w *AnsiWriter) ResetAnsi() {
|
||||
if !w.seqchanged {
|
||||
return
|
||||
}
|
||||
_, _ = w.Forward.Write([]byte("\x1b[0m"))
|
||||
}
|
||||
|
||||
func (w *AnsiWriter) RestoreAnsi() {
|
||||
_, _ = w.Forward.Write([]byte(w.lastseq))
|
||||
}
|
||||
+7
-5
@@ -6,6 +6,8 @@ import (
|
||||
"github.com/alecthomas/chroma"
|
||||
"github.com/alecthomas/chroma/quick"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
"github.com/muesli/reflow/ansi"
|
||||
"github.com/muesli/reflow/indent"
|
||||
)
|
||||
|
||||
type CodeBlockElement struct {
|
||||
@@ -50,11 +52,11 @@ func chromaStyle(style StylePrimitive) string {
|
||||
func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
|
||||
var indent uint
|
||||
var indentation uint
|
||||
var margin uint
|
||||
rules := ctx.options.Styles.CodeBlock
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
indentation = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
@@ -96,12 +98,12 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
|
||||
chroma.Background: chromaStyle(rules.Chroma.Background),
|
||||
}))
|
||||
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
iw := &indent.Writer{
|
||||
Indent: indentation + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Current().Style.StylePrimitive, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: &ansi.Writer{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
|
||||
+8
-6
@@ -4,6 +4,8 @@ import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/muesli/reflow/ansi"
|
||||
"github.com/muesli/reflow/indent"
|
||||
"github.com/muesli/reflow/wordwrap"
|
||||
)
|
||||
|
||||
@@ -50,26 +52,26 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
rules := bs.Current().Style
|
||||
|
||||
var indent uint
|
||||
var indentation uint
|
||||
var margin uint
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
indentation = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
iw := &indent.Writer{
|
||||
Indent: indentation + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Parent().Style.StylePrimitive, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: &ansi.Writer{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
|
||||
flow := wordwrap.NewWriter(int(bs.Width(ctx) - indent - margin*2))
|
||||
flow := wordwrap.NewWriter(int(bs.Width(ctx) - indentation - margin*2))
|
||||
_, err := flow.Write(bs.Current().Block.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type IndentFunc = func(w io.Writer)
|
||||
|
||||
type IndentWriter struct {
|
||||
Forward *AnsiWriter
|
||||
Indent uint
|
||||
IndentFunc IndentFunc
|
||||
|
||||
skipIndent bool
|
||||
ansi bool
|
||||
}
|
||||
|
||||
// Write is used to write content to the indent buffer.
|
||||
func (w *IndentWriter) Write(b []byte) (int, error) {
|
||||
for _, c := range string(b) {
|
||||
if c == '\x1B' {
|
||||
// ANSI escape sequence
|
||||
w.ansi = true
|
||||
} else if w.ansi {
|
||||
if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
||||
// ANSI sequence terminated
|
||||
w.ansi = false
|
||||
}
|
||||
} else {
|
||||
if !w.skipIndent {
|
||||
w.Forward.ResetAnsi()
|
||||
if w.IndentFunc != nil {
|
||||
for i := 0; i < int(w.Indent); i++ {
|
||||
w.IndentFunc(w.Forward)
|
||||
}
|
||||
} else {
|
||||
_, err := w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent))))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
w.skipIndent = true
|
||||
w.Forward.RestoreAnsi()
|
||||
}
|
||||
|
||||
if c == '\n' {
|
||||
// end of current line
|
||||
w.skipIndent = false
|
||||
}
|
||||
}
|
||||
|
||||
_, err := w.Forward.Write([]byte(string(c)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
+13
-9
@@ -2,41 +2,45 @@ package ansi
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/muesli/reflow/ansi"
|
||||
"github.com/muesli/reflow/indent"
|
||||
"github.com/muesli/reflow/padding"
|
||||
)
|
||||
|
||||
type MarginWriter struct {
|
||||
w io.Writer
|
||||
pw *PaddingWriter
|
||||
iw *IndentWriter
|
||||
pw *padding.Writer
|
||||
iw *indent.Writer
|
||||
}
|
||||
|
||||
func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWriter {
|
||||
bs := ctx.blockStack
|
||||
|
||||
var indent uint
|
||||
var indentation uint
|
||||
var margin uint
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
indentation = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
|
||||
pw := &PaddingWriter{
|
||||
pw := &padding.Writer{
|
||||
Padding: bs.Width(ctx),
|
||||
PadFunc: func(wr io.Writer) {
|
||||
renderText(w, rules.StylePrimitive, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: &ansi.Writer{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
iw := &indent.Writer{
|
||||
Indent: indentation + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Parent().Style.StylePrimitive, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: &ansi.Writer{
|
||||
Forward: pw,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/mattn/go-runewidth"
|
||||
)
|
||||
|
||||
type PaddingFunc = func(w io.Writer)
|
||||
|
||||
type PaddingWriter struct {
|
||||
Forward *AnsiWriter
|
||||
Padding uint
|
||||
PadFunc PaddingFunc
|
||||
|
||||
lineLen int
|
||||
ansi bool
|
||||
}
|
||||
|
||||
// Write is used to write content to the padding buffer.
|
||||
func (w *PaddingWriter) Write(b []byte) (int, error) {
|
||||
for _, c := range string(b) {
|
||||
if c == '\x1B' {
|
||||
// ANSI escape sequence
|
||||
w.ansi = true
|
||||
} else if w.ansi {
|
||||
if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
||||
// ANSI sequence terminated
|
||||
w.ansi = false
|
||||
}
|
||||
} else {
|
||||
w.lineLen += runewidth.StringWidth(string(c))
|
||||
|
||||
if c == '\n' {
|
||||
// end of current line
|
||||
if w.Padding > 0 && uint(w.lineLen) < w.Padding {
|
||||
for i := 0; i < int(w.Padding)-w.lineLen; i++ {
|
||||
w.PadFunc(w.Forward)
|
||||
}
|
||||
}
|
||||
w.Forward.ResetAnsi()
|
||||
w.lineLen = 0
|
||||
}
|
||||
}
|
||||
|
||||
_, err := w.Forward.Write([]byte(string(c)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
+7
-5
@@ -3,6 +3,8 @@ package ansi
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/muesli/reflow/ansi"
|
||||
"github.com/muesli/reflow/indent"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
@@ -27,22 +29,22 @@ type TableCellElement struct {
|
||||
func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
|
||||
var indent uint
|
||||
var indentation uint
|
||||
var margin uint
|
||||
rules := ctx.options.Styles.Table
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
indentation = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
|
||||
ctx.table.indentWriter = &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
ctx.table.indentWriter = &indent.Writer{
|
||||
Indent: indentation + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Current().Style.StylePrimitive, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: &ansi.Writer{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -7,9 +7,8 @@ require (
|
||||
github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3
|
||||
github.com/mattn/go-isatty v0.0.10
|
||||
github.com/mattn/go-runewidth v0.0.7
|
||||
github.com/microcosm-cc/bluemonday v1.0.2
|
||||
github.com/muesli/reflow v0.0.0-20191215022747-2d83db2ad290
|
||||
github.com/muesli/reflow v0.0.0-20191215023403-d4be46f67158
|
||||
github.com/olekukonko/tablewriter v0.0.3
|
||||
github.com/rakyll/statik v0.1.6
|
||||
github.com/spf13/cobra v0.0.5
|
||||
|
||||
@@ -54,8 +54,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/muesli/reflow v0.0.0-20191215022747-2d83db2ad290 h1:efDGgE60hqLHC7iOwagZe9RrNz/TdFKW74iCD/qylbs=
|
||||
github.com/muesli/reflow v0.0.0-20191215022747-2d83db2ad290/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I=
|
||||
github.com/muesli/reflow v0.0.0-20191215023403-d4be46f67158 h1:l0x6UtAQo3iv86JbD4yd6TarrbcSw622nARCN6QSdqU=
|
||||
github.com/muesli/reflow v0.0.0-20191215023403-d4be46f67158/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I=
|
||||
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
|
||||
github.com/olekukonko/tablewriter v0.0.3 h1:i0LBnzgiChAWHJYTQAZJDOgf8MNxAVYZJ2m63SIDimI=
|
||||
github.com/olekukonko/tablewriter v0.0.3/go.mod h1:YZeBtGzYYEsCHp2LST/u/0NDwGkRoBtmn1cIWCJiS6M=
|
||||
|
||||
Reference in New Issue
Block a user