github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/interactive/highlighter.go (about)

     1  package interactive
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/alecthomas/chroma"
     7  	"github.com/c-bata/go-prompt"
     8  )
     9  
    10  type Highlighter struct {
    11  	lexer     chroma.Lexer
    12  	formatter chroma.Formatter
    13  	style     *chroma.Style
    14  }
    15  
    16  func newHighlighter(lexer chroma.Lexer, formatter chroma.Formatter, style *chroma.Style) *Highlighter {
    17  	h := new(Highlighter)
    18  	h.formatter = formatter
    19  	h.lexer = lexer
    20  	h.style = style
    21  	return h
    22  }
    23  
    24  func (h *Highlighter) Highlight(d prompt.Document) ([]byte, error) {
    25  	buffer := bytes.NewBuffer([]byte{})
    26  	tokens, err := h.lexer.Tokenise(nil, d.Text)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	h.formatter.Format(buffer, h.style, tokens)
    31  	return buffer.Bytes(), nil
    32  }