diff --git a/ansi/baseelement.go b/ansi/baseelement.go index 4548484..470eb7b 100644 --- a/ansi/baseelement.go +++ b/ansi/baseelement.go @@ -31,12 +31,11 @@ func formatToken(format string, token string) (string, error) { return b.String(), err } -func renderText(w io.Writer, rules StylePrimitive, s string) { +func renderText(w io.Writer, p termenv.Profile, rules StylePrimitive, s string) { if len(s) == 0 { return } - p := termenv.ColorProfile() out := termenv.String(s) if rules.Color != nil { @@ -74,22 +73,22 @@ func renderText(w io.Writer, rules StylePrimitive, s string) { func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack - renderText(w, bs.Current().Style.StylePrimitive, e.Prefix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Prefix) defer func() { - renderText(w, bs.Current().Style.StylePrimitive, e.Suffix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Suffix) }() rules := bs.With(e.Style) // render unstyled prefix/suffix - renderText(w, bs.Current().Style.StylePrimitive, rules.BlockPrefix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) defer func() { - renderText(w, bs.Current().Style.StylePrimitive, rules.BlockSuffix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix) }() // render styled prefix/suffix - renderText(w, rules, rules.Prefix) + renderText(w, ctx.colorProfile, rules, rules.Prefix) defer func() { - renderText(w, rules, rules.Suffix) + renderText(w, ctx.colorProfile, rules, rules.Suffix) }() s := e.Token @@ -100,6 +99,6 @@ func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error { return err } } - renderText(w, rules, s) + renderText(w, ctx.colorProfile, rules, s) return nil } diff --git a/ansi/blockelement.go b/ansi/blockelement.go index ed5659a..5809ca1 100644 --- a/ansi/blockelement.go +++ b/ansi/blockelement.go @@ -21,8 +21,8 @@ func (e *BlockElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack bs.Push(*e) - renderText(w, bs.Parent().Style.StylePrimitive, e.Style.BlockPrefix) - renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, e.Style.Prefix) + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockPrefix) + renderText(bs.Current().Block, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Style.Prefix) return nil } @@ -50,8 +50,8 @@ func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error { } } - renderText(w, bs.Current().Style.StylePrimitive, e.Style.Suffix) - renderText(w, bs.Parent().Style.StylePrimitive, e.Style.BlockSuffix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Style.Suffix) + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockSuffix) bs.Current().Block.Reset() bs.Pop() diff --git a/ansi/codeblock.go b/ansi/codeblock.go index 9e2d91f..b144497 100644 --- a/ansi/codeblock.go +++ b/ansi/codeblock.go @@ -105,7 +105,7 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { iw := &indent.Writer{ Indent: indentation + margin, IndentFunc: func(wr io.Writer) { - renderText(w, bs.Current().Style.StylePrimitive, " ") + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ") }, Forward: &ansi.Writer{ Forward: w, @@ -113,9 +113,9 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { } if len(theme) > 0 { - renderText(iw, bs.Current().Style.StylePrimitive, rules.BlockPrefix) + renderText(iw, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) err := quick.Highlight(iw, e.Code, e.Language, "terminal256", theme) - renderText(iw, bs.Current().Style.StylePrimitive, rules.BlockSuffix) + renderText(iw, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix) return err } diff --git a/ansi/context.go b/ansi/context.go index ee3c2eb..3b6c197 100644 --- a/ansi/context.go +++ b/ansi/context.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/microcosm-cc/bluemonday" + "github.com/muesli/termenv" ) // RenderContext holds the current rendering options and state. @@ -14,16 +15,18 @@ type RenderContext struct { blockStack *BlockStack table *TableElement - stripper *bluemonday.Policy + stripper *bluemonday.Policy + colorProfile termenv.Profile } // NewRenderContext returns a new RenderContext. func NewRenderContext(options Options) RenderContext { return RenderContext{ - options: options, - blockStack: &BlockStack{}, - table: &TableElement{}, - stripper: bluemonday.StrictPolicy(), + options: options, + blockStack: &BlockStack{}, + table: &TableElement{}, + stripper: bluemonday.StrictPolicy(), + colorProfile: termenv.ColorProfile(), } } diff --git a/ansi/heading.go b/ansi/heading.go index c195ae5..63c5536 100644 --- a/ansi/heading.go +++ b/ansi/heading.go @@ -35,7 +35,7 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error { } if !e.First { - renderText(w, bs.Current().Style.StylePrimitive, "\n") + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, "\n") } be := BlockElement{ @@ -44,8 +44,8 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error { } bs.Push(be) - renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockPrefix) - renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, rules.Prefix) + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix) + renderText(bs.Current().Block, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Prefix) return nil } @@ -65,7 +65,7 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error { iw := &indent.Writer{ Indent: indentation + margin, IndentFunc: func(wr io.Writer) { - renderText(w, bs.Parent().Style.StylePrimitive, " ") + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, " ") }, Forward: &ansi.Writer{ Forward: w, @@ -84,8 +84,8 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error { return err } - renderText(w, bs.Current().Style.StylePrimitive, rules.Suffix) - renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockSuffix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Suffix) + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix) bs.Current().Block.Reset() bs.Pop() diff --git a/ansi/margin.go b/ansi/margin.go index 4115103..55a784f 100644 --- a/ansi/margin.go +++ b/ansi/margin.go @@ -32,7 +32,7 @@ func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWr pw := &padding.Writer{ Padding: bs.Width(ctx), PadFunc: func(wr io.Writer) { - renderText(w, rules.StylePrimitive, " ") + renderText(w, ctx.colorProfile, rules.StylePrimitive, " ") }, Forward: &ansi.Writer{ Forward: w, @@ -46,7 +46,7 @@ func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWr iw := &indent.Writer{ Indent: indentation + margin, IndentFunc: func(wr io.Writer) { - renderText(w, bs.Parent().Style.StylePrimitive, ic) + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, ic) }, Forward: &ansi.Writer{ Forward: pw, diff --git a/ansi/paragraph.go b/ansi/paragraph.go index 20cd00e..2c34a04 100644 --- a/ansi/paragraph.go +++ b/ansi/paragraph.go @@ -26,8 +26,8 @@ func (e *ParagraphElement) Render(w io.Writer, ctx RenderContext) error { } bs.Push(be) - renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockPrefix) - renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, rules.Prefix) + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix) + renderText(bs.Current().Block, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Prefix) return nil } @@ -49,8 +49,8 @@ func (e *ParagraphElement) Finish(w io.Writer, ctx RenderContext) error { _, _ = mw.Write([]byte("\n")) } - renderText(w, bs.Current().Style.StylePrimitive, rules.Suffix) - renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockSuffix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Suffix) + renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix) bs.Current().Block.Reset() bs.Pop() diff --git a/ansi/stylewriter.go b/ansi/stylewriter.go index 289cff5..69a934c 100644 --- a/ansi/stylewriter.go +++ b/ansi/stylewriter.go @@ -7,6 +7,7 @@ import ( // StyleWriter is a Writer that applies styling on whatever you write to it. type StyleWriter struct { + ctx RenderContext w io.Writer buf bytes.Buffer rules StylePrimitive @@ -15,6 +16,7 @@ type StyleWriter struct { // NewStyleWriter returns a new StyleWriter. func NewStyleWriter(ctx RenderContext, w io.Writer, rules StylePrimitive) *StyleWriter { return &StyleWriter{ + ctx: ctx, w: w, rules: rules, } @@ -26,6 +28,6 @@ func (w *StyleWriter) Write(b []byte) (int, error) { // Close must be called when you're finished writing to a StyleWriter. func (w *StyleWriter) Close() error { - renderText(w.w, w.rules, w.buf.String()) + renderText(w.w, w.ctx.colorProfile, w.rules, w.buf.String()) return nil } diff --git a/ansi/table.go b/ansi/table.go index 7947055..78e085a 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -46,7 +46,7 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { iw := &indent.Writer{ Indent: indentation + margin, IndentFunc: func(wr io.Writer) { - renderText(w, bs.Current().Style.StylePrimitive, " ") + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ") }, Forward: &ansi.Writer{ Forward: w, @@ -56,8 +56,8 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { style := bs.With(rules.StylePrimitive) ctx.table.styleWriter = NewStyleWriter(ctx, iw, style) - renderText(w, bs.Current().Style.StylePrimitive, rules.BlockPrefix) - renderText(ctx.table.styleWriter, style, rules.Prefix) + renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) + renderText(ctx.table.styleWriter, ctx.colorProfile, style, rules.Prefix) ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter) return nil } @@ -79,8 +79,8 @@ func (e *TableElement) Finish(w io.Writer, ctx RenderContext) error { ctx.table.writer.Render() ctx.table.writer = nil - renderText(ctx.table.styleWriter, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix) - renderText(ctx.table.styleWriter, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix) + renderText(ctx.table.styleWriter, ctx.colorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix) + renderText(ctx.table.styleWriter, ctx.colorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix) return ctx.table.styleWriter.Close() }