From 96e48d9d5b53315af44138768c5d687ec516f148 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Mon, 10 Feb 2020 13:36:02 +0100 Subject: [PATCH] Reuse reflow's writer-pipes --- ansi/codeblock.go | 18 +++++++----------- ansi/heading.go | 13 +++---------- ansi/margin.go | 25 ++++++------------------- ansi/table.go | 13 +++---------- go.mod | 2 +- go.sum | 4 ++-- 6 files changed, 22 insertions(+), 53 deletions(-) diff --git a/ansi/codeblock.go b/ansi/codeblock.go index b144497..907893a 100644 --- a/ansi/codeblock.go +++ b/ansi/codeblock.go @@ -6,7 +6,6 @@ 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" ) @@ -102,21 +101,18 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { })) } - iw := &indent.Writer{ - Indent: indentation + margin, - IndentFunc: func(wr io.Writer) { - renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ") - }, - Forward: &ansi.Writer{ - Forward: w, - }, - } + iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) { + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ") + }) if len(theme) > 0 { renderText(iw, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) err := quick.Highlight(iw, e.Code, e.Language, "terminal256", theme) + if err != nil { + return err + } renderText(iw, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix) - return err + return nil } // fallback rendering diff --git a/ansi/heading.go b/ansi/heading.go index 63c5536..fc83a7b 100644 --- a/ansi/heading.go +++ b/ansi/heading.go @@ -4,7 +4,6 @@ import ( "bytes" "io" - "github.com/muesli/reflow/ansi" "github.com/muesli/reflow/indent" "github.com/muesli/reflow/wordwrap" ) @@ -62,15 +61,9 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error { margin = *rules.Margin } - iw := &indent.Writer{ - Indent: indentation + margin, - IndentFunc: func(wr io.Writer) { - renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, " ") - }, - Forward: &ansi.Writer{ - Forward: w, - }, - } + iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) { + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, " ") + }) flow := wordwrap.NewWriter(int(bs.Width(ctx) - indentation - margin*2)) _, err := flow.Write(bs.Current().Block.Bytes()) diff --git a/ansi/margin.go b/ansi/margin.go index 55a784f..17519cf 100644 --- a/ansi/margin.go +++ b/ansi/margin.go @@ -3,7 +3,6 @@ package ansi import ( "io" - "github.com/muesli/reflow/ansi" "github.com/muesli/reflow/indent" "github.com/muesli/reflow/padding" ) @@ -29,29 +28,17 @@ func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWr margin = *rules.Margin } - pw := &padding.Writer{ - Padding: bs.Width(ctx), - PadFunc: func(wr io.Writer) { - renderText(w, ctx.colorProfile, rules.StylePrimitive, " ") - }, - Forward: &ansi.Writer{ - Forward: w, - }, - } + pw := padding.NewWriterPipe(w, bs.Width(ctx), func(wr io.Writer) { + renderText(w, ctx.colorProfile, rules.StylePrimitive, " ") + }) ic := " " if rules.IndentToken != nil { ic = *rules.IndentToken } - iw := &indent.Writer{ - Indent: indentation + margin, - IndentFunc: func(wr io.Writer) { - renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, ic) - }, - Forward: &ansi.Writer{ - Forward: pw, - }, - } + iw := indent.NewWriterPipe(pw, indentation+margin, func(wr io.Writer) { + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, ic) + }) return &MarginWriter{ w: w, diff --git a/ansi/table.go b/ansi/table.go index 78e085a..6b83842 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -3,7 +3,6 @@ package ansi import ( "io" - "github.com/muesli/reflow/ansi" "github.com/muesli/reflow/indent" "github.com/olekukonko/tablewriter" ) @@ -43,15 +42,9 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { margin = *rules.Margin } - iw := &indent.Writer{ - Indent: indentation + margin, - IndentFunc: func(wr io.Writer) { - renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ") - }, - Forward: &ansi.Writer{ - Forward: w, - }, - } + iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) { + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ") + }) style := bs.With(rules.StylePrimitive) ctx.table.styleWriter = NewStyleWriter(ctx, iw, style) diff --git a/go.mod b/go.mod index 14c5e81..e7cc2c5 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.13 require ( github.com/alecthomas/chroma v0.7.0 github.com/microcosm-cc/bluemonday v1.0.2 - github.com/muesli/reflow v0.0.0-20191216070243-e5efeac4e302 + github.com/muesli/reflow v0.0.0-20200210123334-eb23c6404749 github.com/muesli/termenv v0.4.0 github.com/olekukonko/tablewriter v0.0.4 github.com/yuin/goldmark v1.1.22 diff --git a/go.sum b/go.sum index 496288d..9ac95f4 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s= github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/muesli/reflow v0.0.0-20191216070243-e5efeac4e302 h1:jOh3Kh03uOFkRPV3PI4Am5tqACv2aELgbPgr7YgNX00= -github.com/muesli/reflow v0.0.0-20191216070243-e5efeac4e302/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I= +github.com/muesli/reflow v0.0.0-20200210123334-eb23c6404749 h1:YTvKyMCnfPC8bNOqKHQ07uoo9JJdL3rt+zTe8welA70= +github.com/muesli/reflow v0.0.0-20200210123334-eb23c6404749/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I= github.com/muesli/termenv v0.4.0 h1:8Bz8WDF/6OoL7QENtESkAl3TlMyQq6+c3bjtJO3y9V8= github.com/muesli/termenv v0.4.0/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=