spinner.go RawHistoryFolder
1package modes
2
3import (
4 "math/rand"
5 "time"
6
7 "github.com/bnema/zut/packages/tui"
8)
9
10// spinner drives the busy animation shown in the status bar while a
11// turn is streaming. It rotates through a list of playful status
12// messages and a small frame animation.
13type spinner struct {
14 frames []string
15 messages []string
16 interval time.Duration
17 startedAt time.Time
18 msgIdx int
19
20 // fixedMsg overrides the rotating funnyWorkingLines message when
21 // set. Used for auto-compaction so the spinner clearly says what's
22 // happening instead of cycling jokes.
23 fixedMsg string
24}
25
26// newSpinner constructs a fresh spinner.
27func newSpinner(th tui.Theme) *spinner {
28 s := &spinner{}
29 s.Configure(th)
30 return s
31}
32
33func (s *spinner) Configure(th tui.Theme) {
34 s.frames = append([]string(nil), th.SpinnerFrames...)
35 if len(s.frames) == 0 {
36 s.frames = []string{"⠋", "⠙", "⠚", "⠞", "⠖", "⠦", "⠴", "⠲", "⠳", "⠓"}
37 }
38 s.messages = append([]string(nil), th.SpinnerMessages...)
39 if len(s.messages) == 0 {
40 s.messages = []string{"thinking"}
41 }
42 interval := th.SpinnerIntervalMS
43 if interval <= 0 {
44 interval = 80
45 }
46 s.interval = time.Duration(interval) * time.Millisecond
47 if s.msgIdx >= len(s.messages) {
48 s.msgIdx = 0
49 }
50}
51
52// Start resets the spinner to the beginning of its animation and
53// picks a random message that stays fixed for the whole run. A
54// rotating rollodex of quips during a single turn felt noisy in
55// practice — you'd see five different phrases for one
56// long-running response, which implies progress that isn't
57// actually happening. One stable phrase per turn reads calmer
58// and the variety across turns (next Start picks another index)
59// still keeps the set fresh over a session.
60func (s *spinner) Start() {
61 s.startedAt = time.Now()
62 if len(s.messages) == 0 {
63 s.messages = []string{"thinking"}
64 }
65 s.msgIdx = rand.Intn(len(s.messages))
66 s.fixedMsg = ""
67}
68
69// StartFixed is like Start but pins the status text to msg for the
70// duration of this spinner run. Cleared by the next Start() call.
71func (s *spinner) StartFixed(msg string) {
72 s.startedAt = time.Now()
73 s.fixedMsg = msg
74}
75
76// Frame returns the current spinner glyph for the running animation.
77func (s *spinner) Frame() string {
78 if len(s.frames) == 0 {
79 return ""
80 }
81 if s.startedAt.IsZero() {
82 return s.frames[0]
83 }
84 interval := s.interval
85 if interval <= 0 {
86 interval = 80 * time.Millisecond
87 }
88 elapsed := time.Since(s.startedAt)
89 idx := int(elapsed/interval) % len(s.frames)
90 return s.frames[idx]
91}
92
93// Message returns the spinner's status text. One random phrase
94// per Start call, pinned until the next turn. When the spinner
95// was started via StartFixed, the pinned message is returned
96// unchanged.
97func (s *spinner) Message() string {
98 if s.fixedMsg != "" {
99 return s.fixedMsg
100 }
101 if len(s.messages) == 0 {
102 return "thinking"
103 }
104 if s.msgIdx < 0 || s.msgIdx >= len(s.messages) {
105 s.msgIdx = 0
106 }
107 return s.messages[s.msgIdx]
108}
109
110// Elapsed returns the wall-clock duration the spinner has been running.
111func (s *spinner) Elapsed() time.Duration {
112 if s.startedAt.IsZero() {
113 return 0
114 }
115 return time.Since(s.startedAt).Round(time.Second)
116}