From 850c3affd366c4637fdd163229e37d132bf532cd Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 15 Dec 2019 03:42:40 +0100 Subject: [PATCH] Use upstream indent & padding writers from reflow --- ansi/ansiwriter.go | 65 ---------------------------------------------- ansi/codeblock.go | 12 +++++---- ansi/heading.go | 14 +++++----- ansi/indent.go | 61 ------------------------------------------- ansi/margin.go | 22 +++++++++------- ansi/padding.go | 53 ------------------------------------- ansi/table.go | 12 +++++---- go.mod | 3 +-- go.sum | 4 +-- 9 files changed, 38 insertions(+), 208 deletions(-) delete mode 100644 ansi/ansiwriter.go delete mode 100644 ansi/indent.go delete mode 100644 ansi/padding.go diff --git a/ansi/ansiwriter.go b/ansi/ansiwriter.go deleted file mode 100644 index 3bed749..0000000 --- a/ansi/ansiwriter.go +++ /dev/null @@ -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)) -} diff --git a/ansi/codeblock.go b/ansi/codeblock.go index 8fb3c35..a8e5fe7 100644 --- a/ansi/codeblock.go +++ b/ansi/codeblock.go @@ -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, }, } diff --git a/ansi/heading.go b/ansi/heading.go index f732300..919f5a5 100644 --- a/ansi/heading.go +++ b/ansi/heading.go @@ -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 diff --git a/ansi/indent.go b/ansi/indent.go deleted file mode 100644 index ec9dfd8..0000000 --- a/ansi/indent.go +++ /dev/null @@ -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 -} diff --git a/ansi/margin.go b/ansi/margin.go index 4fd83c6..f97f372 100644 --- a/ansi/margin.go +++ b/ansi/margin.go @@ -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, }, } diff --git a/ansi/padding.go b/ansi/padding.go deleted file mode 100644 index 247317d..0000000 --- a/ansi/padding.go +++ /dev/null @@ -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 -} diff --git a/ansi/table.go b/ansi/table.go index da5d06c..1952767 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -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, }, } diff --git a/go.mod b/go.mod index f82cf5a..4a10c83 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a4d4b07..a02f344 100644 --- a/go.sum +++ b/go.sum @@ -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=