2023-11-05 00:44:06 -06:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-18 22:00:59 -07:00
|
|
|
"os"
|
2023-11-05 00:44:06 -06:00
|
|
|
"time"
|
2023-11-15 22:02:43 -07:00
|
|
|
|
2023-11-18 22:00:59 -07:00
|
|
|
"github.com/alecthomas/chroma/v2/quick"
|
2023-11-15 22:02:43 -07:00
|
|
|
"github.com/gookit/color"
|
2023-11-05 00:44:06 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// ShowWaitAnimation "draws" an animated ellipses to stdout until something is
|
|
|
|
// received on the signal channel. An empty string sent to the channel to
|
|
|
|
// noftify the caller that the animation has completed (carriage returned).
|
|
|
|
func ShowWaitAnimation(signal chan any) {
|
|
|
|
animationStep := 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _ = <-signal:
|
|
|
|
fmt.Print("\r")
|
|
|
|
signal <- ""
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
modSix := animationStep % 6
|
|
|
|
if modSix == 3 || modSix == 0 {
|
|
|
|
fmt.Print("\r")
|
|
|
|
}
|
|
|
|
if modSix < 3 {
|
|
|
|
fmt.Print(".")
|
|
|
|
} else {
|
|
|
|
fmt.Print(" ")
|
|
|
|
}
|
|
|
|
animationStep++
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-23 20:45:43 -07:00
|
|
|
// HandleDelayedContent displays a waiting animation to stdout while waiting
|
|
|
|
// for content to be received on the provided channel. As soon as any (possibly
|
|
|
|
// chunked) content is received on the channel, the waiting animation is
|
|
|
|
// replaced by the content.
|
2023-11-05 01:40:55 -06:00
|
|
|
// Blocks until the channel is closed.
|
2023-11-23 20:45:43 -07:00
|
|
|
func HandleDelayedContent(content <-chan string) {
|
2023-11-05 00:44:06 -06:00
|
|
|
waitSignal := make(chan any)
|
|
|
|
go ShowWaitAnimation(waitSignal)
|
|
|
|
|
|
|
|
firstChunk := true
|
2023-11-23 20:45:43 -07:00
|
|
|
for chunk := range content {
|
2023-11-05 00:44:06 -06:00
|
|
|
if firstChunk {
|
2023-11-05 11:19:30 -07:00
|
|
|
// notify wait animation that we've received data
|
2023-11-05 00:44:06 -06:00
|
|
|
waitSignal <- ""
|
2023-11-05 11:19:30 -07:00
|
|
|
// wait for signal that wait animation has completed
|
2023-11-05 00:44:06 -06:00
|
|
|
<-waitSignal
|
|
|
|
firstChunk = false
|
|
|
|
}
|
|
|
|
fmt.Print(chunk)
|
|
|
|
}
|
|
|
|
}
|
2023-11-05 01:50:07 -07:00
|
|
|
|
2023-11-18 22:00:59 -07:00
|
|
|
// 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)
|
2023-11-18 08:57:03 -07:00
|
|
|
func RenderConversation(messages []Message, spaceForResponse bool) {
|
|
|
|
l := len(messages)
|
|
|
|
for i, message := range messages {
|
|
|
|
message.RenderTTY()
|
|
|
|
if i < l-1 || spaceForResponse {
|
|
|
|
// print an additional space before the next message
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:00:59 -07:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-11-18 08:57:03 -07:00
|
|
|
func (m *Message) RenderTTY() {
|
2023-11-15 22:02:43 -07:00
|
|
|
var messageAge string
|
|
|
|
if m.CreatedAt.IsZero() {
|
|
|
|
messageAge = "now"
|
|
|
|
} else {
|
|
|
|
now := time.Now()
|
|
|
|
messageAge = humanTimeElapsedSince(now.Sub(m.CreatedAt))
|
|
|
|
}
|
|
|
|
|
|
|
|
var roleStyle color.Style
|
|
|
|
switch m.Role {
|
2023-11-28 22:57:38 -07:00
|
|
|
case MessageRoleSystem:
|
2023-11-15 22:02:43 -07:00
|
|
|
roleStyle = color.Style{color.HiRed}
|
2023-11-28 22:57:38 -07:00
|
|
|
case MessageRoleUser:
|
2023-11-15 22:02:43 -07:00
|
|
|
roleStyle = color.Style{color.HiGreen}
|
2023-11-28 22:57:38 -07:00
|
|
|
case MessageRoleAssistant:
|
2023-11-15 22:02:43 -07:00
|
|
|
roleStyle = color.Style{color.HiBlue}
|
|
|
|
default:
|
|
|
|
roleStyle = color.Style{color.FgWhite}
|
|
|
|
}
|
|
|
|
roleStyle.Add(color.Bold)
|
|
|
|
|
|
|
|
headerColor := color.FgYellow
|
|
|
|
separator := headerColor.Sprint("===")
|
|
|
|
timestamp := headerColor.Sprint(messageAge)
|
|
|
|
role := roleStyle.Sprint(m.FriendlyRole())
|
|
|
|
|
|
|
|
fmt.Printf("%s %s - %s %s\n\n", separator, role, timestamp, separator)
|
2023-11-05 01:50:07 -07:00
|
|
|
if m.OriginalContent != "" {
|
2023-11-18 22:00:59 -07:00
|
|
|
HighlightMarkdown(m.OriginalContent)
|
|
|
|
fmt.Println()
|
2023-11-05 01:50:07 -07:00
|
|
|
}
|
|
|
|
}
|