From f2c7d2bdd0f44c9dcbe77ed60001af2d8ce4629a Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 12 Mar 2024 07:19:16 +0000 Subject: [PATCH] Store ChromaHighlighter in lmcli.Context and use it In preparation for TUI --- pkg/cmd/util/util.go | 14 +------------- pkg/lmcli/lmcli.go | 7 ++++++- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/util/util.go b/pkg/cmd/util/util.go index d7d3de4..2fc5980 100644 --- a/pkg/cmd/util/util.go +++ b/pkg/cmd/util/util.go @@ -2,7 +2,6 @@ package util import ( "fmt" - "io" "os" "strings" "time" @@ -11,7 +10,6 @@ import ( "git.mlow.ca/mlow/lmcli/pkg/lmcli/model" "git.mlow.ca/mlow/lmcli/pkg/lmcli/tools" "git.mlow.ca/mlow/lmcli/pkg/util" - "github.com/alecthomas/chroma/v2/quick" "github.com/charmbracelet/lipgloss" ) @@ -240,12 +238,6 @@ func RenderConversation(ctx *lmcli.Context, messages []model.Message, spaceForRe } } -// HighlightMarkdown applies syntax highlighting to the provided markdown text -// and writes it to stdout. -func HighlightMarkdown(w io.Writer, markdownText string, formatter string, style string) error { - return quick.Highlight(w, markdownText, "md", formatter, style) -} - func RenderMessage(ctx *lmcli.Context, m *model.Message) { var messageAge string if m.CreatedAt.IsZero() { @@ -274,11 +266,7 @@ func RenderMessage(ctx *lmcli.Context, m *model.Message) { fmt.Printf("%s %s - %s %s\n\n", separator, role, timestamp, separator) if m.Content != "" { - HighlightMarkdown( - os.Stdout, m.Content, - *ctx.Config.Chroma.Formatter, - *ctx.Config.Chroma.Style, - ) + ctx.Chroma.Highlight(os.Stdout, m.Content) fmt.Println() } } diff --git a/pkg/lmcli/lmcli.go b/pkg/lmcli/lmcli.go index ab6ce16..b2e99d9 100644 --- a/pkg/lmcli/lmcli.go +++ b/pkg/lmcli/lmcli.go @@ -8,6 +8,7 @@ import ( "git.mlow.ca/mlow/lmcli/pkg/lmcli/provider" "git.mlow.ca/mlow/lmcli/pkg/lmcli/provider/anthropic" "git.mlow.ca/mlow/lmcli/pkg/lmcli/provider/openai" + "git.mlow.ca/mlow/lmcli/pkg/util/tty" "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -15,6 +16,8 @@ import ( type Context struct { Config Config Store ConversationStore + + Chroma *tty.ChromaHighlighter } func NewContext() (*Context, error) { @@ -34,7 +37,9 @@ func NewContext() (*Context, error) { Fatal("%v\n", err) } - return &Context{*config, s}, nil + hl := tty.NewChromaHighlighter("markdown", *config.Chroma.Formatter, *config.Chroma.Style) + + return &Context{*config, s, hl}, nil } func (c *Context) GetCompletionProvider(model string) (provider.ChatCompletionClient, error) {