package cli import ( "fmt" "strings" "time" "github.com/gookit/color" ) // 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) } } } // HandledDelayedResponse writes a waiting animation (abusing \r) and the // (possibly chunked) content received on the response channel to stdout. // Blocks until the channel is closed. func HandleDelayedResponse(response chan string) string { waitSignal := make(chan any) go ShowWaitAnimation(waitSignal) sb := strings.Builder{} firstChunk := true for chunk := range response { if firstChunk { // notify wait animation that we've received data waitSignal <- "" // wait for signal that wait animation has completed <-waitSignal firstChunk = false } fmt.Print(chunk) sb.WriteString(chunk) } return sb.String() } func (m *Message) RenderTTY(paddingDown bool) { 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 { case "system": roleStyle = color.Style{color.HiRed} case "user": roleStyle = color.Style{color.HiGreen} case "assistant": 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) if m.OriginalContent != "" { fmt.Print(m.OriginalContent) } if paddingDown { fmt.Print("\n\n") } }