1package modes
2
3import (
4 "math/rand"
5 "time"
6)
7
8// spinner drives the busy animation shown in the status bar while a
9// turn is streaming. It rotates through a list of playful status
10// messages and a small frame animation.
11type spinner struct {
12 frames []string
13 messages []string
14 startedAt time.Time
15 msgIdx int
16 lastSwap time.Time
17
18 // fixedMsg overrides the rotating funnyWorkingLines message when
19 // set. Used for auto-compaction so the spinner clearly says what's
20 // happening instead of cycling jokes.
21 fixedMsg string
22}
23
24// funnyWorkingLines is the rotating text. Kept deliberately short so it
25// fits next to the token counter on narrow terminals.
26var funnyWorkingLines = []string{
27 "thinking",
28 "reticulating splines",
29 "bribing the tokenizer",
30 "asking the rubber duck",
31 "summoning daemons",
32 "consulting the oracle",
33 "herding tokens",
34 "compiling excuses",
35 "poking the model",
36 "negotiating with rate limits",
37 "picking a fight with syntax",
38 "reading between the bits",
39 "tasting the semicolons",
40 "pretending to understand go generics",
41 "petting the cache",
42 "drafting clever replies",
43 "warming up the GPU choir",
44 "arguing with a stack trace",
45 "googling the answer (not really)",
46 "rewriting history",
47}
48
49// spinnerFrames is the cli-spinners "dots3" preset — a 10-frame
50// single-cell braille spinner that reads as a small box of dots
51// rotating around its perimeter. Source:
52// https://github.com/sindresorhus/cli-spinners (MIT). Used at the
53// preset's recommended 80ms per step.
54var spinnerFrames = []string{
55 "⠋",
56 "⠙",
57 "⠚",
58 "⠞",
59 "⠖",
60 "⠦",
61 "⠴",
62 "⠲",
63 "⠳",
64 "⠓",
65}
66
67// newSpinner constructs a fresh spinner.
68func newSpinner() *spinner {
69 s := &spinner{
70 frames: spinnerFrames,
71 messages: funnyWorkingLines,
72 }
73 return s
74}
75
76// Start resets the spinner to the beginning of its animation and picks
77// a random opening message.
78func (s *spinner) Start() {
79 s.startedAt = time.Now()
80 s.msgIdx = rand.Intn(len(s.messages))
81 s.lastSwap = s.startedAt
82 s.fixedMsg = ""
83}
84
85// StartFixed is like Start but pins the status text to msg for the
86// duration of this spinner run. Cleared by the next Start() call.
87func (s *spinner) StartFixed(msg string) {
88 s.startedAt = time.Now()
89 s.lastSwap = s.startedAt
90 s.fixedMsg = msg
91}
92
93// Frame returns the current spinner glyph for the running animation.
94//
95// 80ms per step matches the dots3 preset's recommended interval.
96func (s *spinner) Frame() string {
97 if s.startedAt.IsZero() {
98 return s.frames[0]
99 }
100 elapsed := time.Since(s.startedAt)
101 idx := int(elapsed/(80*time.Millisecond)) % len(s.frames)
102 return s.frames[idx]
103}
104
105// Message returns the current rotating status text. The text changes
106// every ~2.5 seconds so the spinner doesn't look frozen. When the
107// spinner was started via StartFixed, the pinned message is returned
108// unchanged.
109func (s *spinner) Message() string {
110 if s.fixedMsg != "" {
111 return s.fixedMsg
112 }
113 if time.Since(s.lastSwap) > 2500*time.Millisecond {
114 s.msgIdx = (s.msgIdx + 1) % len(s.messages)
115 s.lastSwap = time.Now()
116 }
117 return s.messages[s.msgIdx]
118}
119
120// Elapsed returns the wall-clock duration the spinner has been running.
121func (s *spinner) Elapsed() time.Duration {
122 if s.startedAt.IsZero() {
123 return 0
124 }
125 return time.Since(s.startedAt).Round(time.Second)
126}