Use upstream indent & padding writers from reflow

This commit is contained in:
Christian Muehlhaeuser
2019-12-15 03:42:40 +01:00
parent b0857fb690
commit 850c3affd3
9 changed files with 38 additions and 208 deletions
-65
View File
@@ -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
View File
@@ -6,6 +6,8 @@ import (
"github.com/alecthomas/chroma" "github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/quick" "github.com/alecthomas/chroma/quick"
"github.com/alecthomas/chroma/styles" "github.com/alecthomas/chroma/styles"
"github.com/muesli/reflow/ansi"
"github.com/muesli/reflow/indent"
) )
type CodeBlockElement struct { type CodeBlockElement struct {
@@ -50,11 +52,11 @@ func chromaStyle(style StylePrimitive) string {
func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack bs := ctx.blockStack
var indent uint var indentation uint
var margin uint var margin uint
rules := ctx.options.Styles.CodeBlock rules := ctx.options.Styles.CodeBlock
if rules.Indent != nil { if rules.Indent != nil {
indent = *rules.Indent indentation = *rules.Indent
} }
if rules.Margin != nil { if rules.Margin != nil {
margin = *rules.Margin margin = *rules.Margin
@@ -96,12 +98,12 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
chroma.Background: chromaStyle(rules.Chroma.Background), chroma.Background: chromaStyle(rules.Chroma.Background),
})) }))
iw := &IndentWriter{ iw := &indent.Writer{
Indent: indent + margin, Indent: indentation + margin,
IndentFunc: func(wr io.Writer) { IndentFunc: func(wr io.Writer) {
renderText(w, bs.Current().Style.StylePrimitive, " ") renderText(w, bs.Current().Style.StylePrimitive, " ")
}, },
Forward: &AnsiWriter{ Forward: &ansi.Writer{
Forward: w, Forward: w,
}, },
} }
+8 -6
View File
@@ -4,6 +4,8 @@ import (
"bytes" "bytes"
"io" "io"
"github.com/muesli/reflow/ansi"
"github.com/muesli/reflow/indent"
"github.com/muesli/reflow/wordwrap" "github.com/muesli/reflow/wordwrap"
) )
@@ -50,26 +52,26 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack bs := ctx.blockStack
rules := bs.Current().Style rules := bs.Current().Style
var indent uint var indentation uint
var margin uint var margin uint
if rules.Indent != nil { if rules.Indent != nil {
indent = *rules.Indent indentation = *rules.Indent
} }
if rules.Margin != nil { if rules.Margin != nil {
margin = *rules.Margin margin = *rules.Margin
} }
iw := &IndentWriter{ iw := &indent.Writer{
Indent: indent + margin, Indent: indentation + margin,
IndentFunc: func(wr io.Writer) { IndentFunc: func(wr io.Writer) {
renderText(w, bs.Parent().Style.StylePrimitive, " ") renderText(w, bs.Parent().Style.StylePrimitive, " ")
}, },
Forward: &AnsiWriter{ Forward: &ansi.Writer{
Forward: w, 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()) _, err := flow.Write(bs.Current().Block.Bytes())
if err != nil { if err != nil {
return err return err
-61
View File
@@ -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
View File
@@ -2,41 +2,45 @@ package ansi
import ( import (
"io" "io"
"github.com/muesli/reflow/ansi"
"github.com/muesli/reflow/indent"
"github.com/muesli/reflow/padding"
) )
type MarginWriter struct { type MarginWriter struct {
w io.Writer w io.Writer
pw *PaddingWriter pw *padding.Writer
iw *IndentWriter iw *indent.Writer
} }
func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWriter { func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWriter {
bs := ctx.blockStack bs := ctx.blockStack
var indent uint var indentation uint
var margin uint var margin uint
if rules.Indent != nil { if rules.Indent != nil {
indent = *rules.Indent indentation = *rules.Indent
} }
if rules.Margin != nil { if rules.Margin != nil {
margin = *rules.Margin margin = *rules.Margin
} }
pw := &PaddingWriter{ pw := &padding.Writer{
Padding: bs.Width(ctx), Padding: bs.Width(ctx),
PadFunc: func(wr io.Writer) { PadFunc: func(wr io.Writer) {
renderText(w, rules.StylePrimitive, " ") renderText(w, rules.StylePrimitive, " ")
}, },
Forward: &AnsiWriter{ Forward: &ansi.Writer{
Forward: w, Forward: w,
}, },
} }
iw := &IndentWriter{ iw := &indent.Writer{
Indent: indent + margin, Indent: indentation + margin,
IndentFunc: func(wr io.Writer) { IndentFunc: func(wr io.Writer) {
renderText(w, bs.Parent().Style.StylePrimitive, " ") renderText(w, bs.Parent().Style.StylePrimitive, " ")
}, },
Forward: &AnsiWriter{ Forward: &ansi.Writer{
Forward: pw, Forward: pw,
}, },
} }
-53
View File
@@ -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
View File
@@ -3,6 +3,8 @@ package ansi
import ( import (
"io" "io"
"github.com/muesli/reflow/ansi"
"github.com/muesli/reflow/indent"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
) )
@@ -27,22 +29,22 @@ type TableCellElement struct {
func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack bs := ctx.blockStack
var indent uint var indentation uint
var margin uint var margin uint
rules := ctx.options.Styles.Table rules := ctx.options.Styles.Table
if rules.Indent != nil { if rules.Indent != nil {
indent = *rules.Indent indentation = *rules.Indent
} }
if rules.Margin != nil { if rules.Margin != nil {
margin = *rules.Margin margin = *rules.Margin
} }
ctx.table.indentWriter = &IndentWriter{ ctx.table.indentWriter = &indent.Writer{
Indent: indent + margin, Indent: indentation + margin,
IndentFunc: func(wr io.Writer) { IndentFunc: func(wr io.Writer) {
renderText(w, bs.Current().Style.StylePrimitive, " ") renderText(w, bs.Current().Style.StylePrimitive, " ")
}, },
Forward: &AnsiWriter{ Forward: &ansi.Writer{
Forward: w, Forward: w,
}, },
} }
+1 -2
View File
@@ -7,9 +7,8 @@ require (
github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434 github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434
github.com/lucasb-eyer/go-colorful v1.0.3 github.com/lucasb-eyer/go-colorful v1.0.3
github.com/mattn/go-isatty v0.0.10 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/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/olekukonko/tablewriter v0.0.3
github.com/rakyll/statik v0.1.6 github.com/rakyll/statik v0.1.6
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5
+2 -2
View File
@@ -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/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/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-20191215023403-d4be46f67158 h1:l0x6UtAQo3iv86JbD4yd6TarrbcSw622nARCN6QSdqU=
github.com/muesli/reflow v0.0.0-20191215022747-2d83db2ad290/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I= 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/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 h1:i0LBnzgiChAWHJYTQAZJDOgf8MNxAVYZJ2m63SIDimI=
github.com/olekukonko/tablewriter v0.0.3/go.mod h1:YZeBtGzYYEsCHp2LST/u/0NDwGkRoBtmn1cIWCJiS6M= github.com/olekukonko/tablewriter v0.0.3/go.mod h1:YZeBtGzYYEsCHp2LST/u/0NDwGkRoBtmn1cIWCJiS6M=