github.com/charmbracelet/glamour@v0.7.0/ansi/stylewriter.go (about)

     1  package ansi
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  )
     7  
     8  // StyleWriter is a Writer that applies styling on whatever you write to it.
     9  type StyleWriter struct {
    10  	ctx   RenderContext
    11  	w     io.Writer
    12  	buf   bytes.Buffer
    13  	rules StylePrimitive
    14  }
    15  
    16  // NewStyleWriter returns a new StyleWriter.
    17  func NewStyleWriter(ctx RenderContext, w io.Writer, rules StylePrimitive) *StyleWriter {
    18  	return &StyleWriter{
    19  		ctx:   ctx,
    20  		w:     w,
    21  		rules: rules,
    22  	}
    23  }
    24  
    25  func (w *StyleWriter) Write(b []byte) (int, error) {
    26  	return w.buf.Write(b)
    27  }
    28  
    29  // Close must be called when you're finished writing to a StyleWriter.
    30  func (w *StyleWriter) Close() error {
    31  	renderText(w.w, w.ctx.options.ColorProfile, w.rules, w.buf.String())
    32  	return nil
    33  }