Add syntax highlighting
This commit is contained in:
@@ -14,6 +14,10 @@ type Config struct {
|
||||
DefaultModel *string `yaml:"defaultModel" default:"gpt-4"`
|
||||
DefaultMaxLength *int `yaml:"defaultMaxLength" default:"256"`
|
||||
} `yaml:"openai"`
|
||||
Chroma *struct {
|
||||
Style *string `yaml:"style" default:"onedark"`
|
||||
Formatter *string `yaml:"formatter" default:"terminal16m"`
|
||||
} `yaml:"chroma"`
|
||||
}
|
||||
|
||||
func getConfigDir() string {
|
||||
|
||||
@@ -2,9 +2,11 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/chroma/v2/quick"
|
||||
"github.com/gookit/color"
|
||||
)
|
||||
|
||||
@@ -60,9 +62,9 @@ func HandleDelayedResponse(response chan string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// RenderConversation renders the given messages, with optional space for a
|
||||
// subsequent message. spaceForResponse controls how many newlines are printed
|
||||
// after the final message (1 newline if false, 2 if true)
|
||||
// RenderConversation renders the given messages to TTY, with optional space
|
||||
// for a subsequent message. spaceForResponse controls how many '\n' characters
|
||||
// are printed immediately after the final message (1 if false, 2 if true)
|
||||
func RenderConversation(messages []Message, spaceForResponse bool) {
|
||||
l := len(messages)
|
||||
for i, message := range messages {
|
||||
@@ -74,6 +76,12 @@ func RenderConversation(messages []Message, spaceForResponse bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// HighlightMarkdown applies syntax highlighting to the provided markdown text
|
||||
// and writes it to stdout.
|
||||
func HighlightMarkdown(markdownText string) error {
|
||||
return quick.Highlight(os.Stdout, markdownText, "md", *config.Chroma.Formatter, *config.Chroma.Style)
|
||||
}
|
||||
|
||||
func (m *Message) RenderTTY() {
|
||||
var messageAge string
|
||||
if m.CreatedAt.IsZero() {
|
||||
@@ -103,6 +111,7 @@ func (m *Message) RenderTTY() {
|
||||
|
||||
fmt.Printf("%s %s - %s %s\n\n", separator, role, timestamp, separator)
|
||||
if m.OriginalContent != "" {
|
||||
fmt.Println(m.OriginalContent)
|
||||
HighlightMarkdown(m.OriginalContent)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user