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 a smooth braille spinner.
50var spinnerFrames = []string{"\u280b", "\u2819", "\u2839", "\u2838", "\u283c", "\u2834", "\u2826", "\u2827", "\u2807", "\u280f"}
51
52// newSpinner constructs a fresh spinner.
53func newSpinner() *spinner {
54 s := &spinner{
55 frames: spinnerFrames,
56 messages: funnyWorkingLines,
57 }
58 return s
59}
60
61// Start resets the spinner to the beginning of its animation and picks
62// a random opening message.
63func (s *spinner) Start() {
64 s.startedAt = time.Now()
65 s.msgIdx = rand.Intn(len(s.messages))
66 s.lastSwap = s.startedAt
67 s.fixedMsg = ""
68}
69
70// StartFixed is like Start but pins the status text to msg for the
71// duration of this spinner run. Cleared by the next Start() call.
72func (s *spinner) StartFixed(msg string) {
73 s.startedAt = time.Now()
74 s.lastSwap = s.startedAt
75 s.fixedMsg = msg
76}
77
78// Frame returns the current spinner glyph for the running animation.
79func (s *spinner) Frame() string {
80 if s.startedAt.IsZero() {
81 return s.frames[0]
82 }
83 elapsed := time.Since(s.startedAt)
84 // 120ms per frame matches the interactive tick rate.
85 idx := int(elapsed/(120*time.Millisecond)) % len(s.frames)
86 return s.frames[idx]
87}
88
89// Message returns the current rotating status text. The text changes
90// every ~2.5 seconds so the spinner doesn't look frozen. When the
91// spinner was started via StartFixed, the pinned message is returned
92// unchanged.
93func (s *spinner) Message() string {
94 if s.fixedMsg != "" {
95 return s.fixedMsg
96 }
97 if time.Since(s.lastSwap) > 2500*time.Millisecond {
98 s.msgIdx = (s.msgIdx + 1) % len(s.messages)
99 s.lastSwap = time.Now()
100 }
101 return s.messages[s.msgIdx]
102}
103
104// Elapsed returns the wall-clock duration the spinner has been running.
105func (s *spinner) Elapsed() time.Duration {
106 if s.startedAt.IsZero() {
107 return 0
108 }
109 return time.Since(s.startedAt).Round(time.Second)
110}