|
| 1 | +package spinner |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand/v2" |
| 5 | + "sync/atomic" |
| 6 | + "time" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + "charm.land/lipgloss/v2" |
| 10 | + |
| 11 | + "github.com/docker/cagent/pkg/tui/core/layout" |
| 12 | + "github.com/docker/cagent/pkg/tui/styles" |
| 13 | +) |
| 14 | + |
| 15 | +type Mode int |
| 16 | + |
| 17 | +const ( |
| 18 | + ModeBoth Mode = iota |
| 19 | + ModeSpinnerOnly |
| 20 | + ModeMessageOnly |
| 21 | +) |
| 22 | + |
| 23 | +var lastID int64 |
| 24 | + |
| 25 | +func nextID() int { |
| 26 | + return int(atomic.AddInt64(&lastID, 1)) |
| 27 | +} |
| 28 | + |
| 29 | +type tickMsg struct { |
| 30 | + Time time.Time |
| 31 | + tag int |
| 32 | + ID int |
| 33 | +} |
| 34 | + |
| 35 | +type Spinner struct { |
| 36 | + messages []string |
| 37 | + mode Mode |
| 38 | + currentMessage string |
| 39 | + lightPosition int |
| 40 | + frame int |
| 41 | + id int |
| 42 | + tag int |
| 43 | + direction int // 1 for forward, -1 for backward |
| 44 | + pauseFrames int |
| 45 | +} |
| 46 | + |
| 47 | +// Default messages for the spinner |
| 48 | +var defaultMessages = []string{ |
| 49 | + "Working", |
| 50 | + "Reticulating splines", |
| 51 | + "Computing", |
| 52 | + "Thinking", |
| 53 | + "Processing", |
| 54 | + "Analyzing", |
| 55 | + "Calibrating", |
| 56 | + "Initializing", |
| 57 | + "Generating", |
| 58 | + "Evaluating", |
| 59 | + "Synthesizing", |
| 60 | + "Optimizing", |
| 61 | + "Consulting the oracle", |
| 62 | + "Summoning electrons", |
| 63 | + "Warming up the flux capacitor", |
| 64 | + "Reversing the polarity", |
| 65 | + "Spinning up the hamster wheels", |
| 66 | + "Dividing by zero", |
| 67 | + "Herding cats", |
| 68 | + "Untangling yarn", |
| 69 | +} |
| 70 | + |
| 71 | +func New(mode Mode) Spinner { |
| 72 | + return Spinner{ |
| 73 | + messages: defaultMessages, |
| 74 | + mode: mode, |
| 75 | + currentMessage: defaultMessages[rand.IntN(len(defaultMessages))], |
| 76 | + lightPosition: -3, |
| 77 | + frame: 0, |
| 78 | + id: nextID(), |
| 79 | + direction: 1, |
| 80 | + pauseFrames: 0, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (s Spinner) Init() tea.Cmd { |
| 85 | + return s.Tick() |
| 86 | +} |
| 87 | + |
| 88 | +func (s Spinner) Update(msg tea.Msg) (layout.Model, tea.Cmd) { |
| 89 | + if msg, ok := msg.(tickMsg); ok { |
| 90 | + if msg.ID > 0 && msg.ID != s.id { |
| 91 | + return s, nil |
| 92 | + } |
| 93 | + if msg.tag > 0 && msg.tag != s.tag { |
| 94 | + return s, nil |
| 95 | + } |
| 96 | + s.tag++ |
| 97 | + |
| 98 | + s.frame++ |
| 99 | + |
| 100 | + if s.pauseFrames > 0 { |
| 101 | + s.pauseFrames-- |
| 102 | + if s.pauseFrames == 0 { |
| 103 | + s.direction = -1 |
| 104 | + } |
| 105 | + } else { |
| 106 | + s.lightPosition += s.direction |
| 107 | + |
| 108 | + if s.direction == 1 && s.lightPosition > len(s.currentMessage)+2 { |
| 109 | + s.pauseFrames = 6 |
| 110 | + } |
| 111 | + |
| 112 | + if s.direction == -1 && s.lightPosition < -3 { |
| 113 | + s.direction = 1 |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return s, s.Tick() |
| 118 | + } |
| 119 | + return s, nil |
| 120 | +} |
| 121 | + |
| 122 | +func (s Spinner) View() string { |
| 123 | + return s.render() |
| 124 | +} |
| 125 | + |
| 126 | +func (s Spinner) SetSize(_, _ int) tea.Cmd { |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func (s Spinner) Tick() tea.Cmd { |
| 131 | + return tea.Tick(50*time.Millisecond, func(t time.Time) tea.Msg { |
| 132 | + return tickMsg{ |
| 133 | + Time: t, |
| 134 | + ID: s.id, |
| 135 | + tag: s.tag, |
| 136 | + } |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +func (s Spinner) render() string { |
| 141 | + message := s.currentMessage |
| 142 | + output := make([]rune, 0, len(message)) |
| 143 | + |
| 144 | + for i, char := range message { |
| 145 | + distance := abs(i - s.lightPosition) |
| 146 | + |
| 147 | + var style lipgloss.Style |
| 148 | + switch distance { |
| 149 | + case 0: |
| 150 | + style = styles.SpinnerTextBrightestStyle |
| 151 | + case 1: |
| 152 | + style = styles.SpinnerTextBrightStyle |
| 153 | + case 2: |
| 154 | + style = styles.SpinnerTextDimStyle |
| 155 | + default: |
| 156 | + style = styles.SpinnerTextDimmestStyle |
| 157 | + } |
| 158 | + |
| 159 | + output = append(output, []rune(style.Render(string(char)))...) |
| 160 | + } |
| 161 | + |
| 162 | + spinnerChars := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} |
| 163 | + spinnerChar := spinnerChars[s.frame%len(spinnerChars)] |
| 164 | + spinnerStyled := styles.SpinnerCharStyle.Render(spinnerChar) |
| 165 | + |
| 166 | + switch s.mode { |
| 167 | + case ModeSpinnerOnly: |
| 168 | + return spinnerStyled |
| 169 | + case ModeMessageOnly: |
| 170 | + return string(output) |
| 171 | + } |
| 172 | + |
| 173 | + return spinnerStyled + " " + string(output) |
| 174 | +} |
| 175 | + |
| 176 | +func (s *Spinner) Render() string { |
| 177 | + return s.render() |
| 178 | +} |
| 179 | + |
| 180 | +func (s *Spinner) SetMessage(message string) { |
| 181 | + s.currentMessage = message |
| 182 | +} |
| 183 | + |
| 184 | +func abs(x int) int { |
| 185 | + if x < 0 { |
| 186 | + return -x |
| 187 | + } |
| 188 | + return x |
| 189 | +} |
0 commit comments