6119
My
mysis / files
mysis / internal / tui / statusbar.go
10.8 KB
1package tui 2 3import ( 4 "time" 5 6 tea "github.com/charmbracelet/bubbletea" 7 "github.com/charmbracelet/lipgloss" 8 "github.com/xonecas/mysis/internal/styles" 9) 10 11// StatusBar manages the bottom status bar with animated icons and status text. 12type StatusBar struct { 13 width int 14 15 // Icon animation state 16 autoplayFrames int // Remaining animation frames for autoplay icon 17 infoFrames int // Remaining animation frames for info icon 18 warningFrames int // Remaining animation frames for warning icon 19 errorFrames int // Remaining animation frames for error icon 20 llmFrames int // Remaining animation frames for LLM connection icon 21 mcpFrames int // Remaining animation frames for MCP connection icon 22 23 currentFrame int // Current animation frame (0-11 for 12 frames @ 8 FPS) 24 25 // Status text 26 errorText string 27 warningText string 28 autoplayText string 29} 30 31const ( 32 animationFPSFast = 8 // 8 frames per second (fast animation at start) 33 animationFPSSlow = 2 // 2 frames per second (slow animation during deceleration) 34 framesPerCycle = 12 // 12 frames in one complete cycle 35 fastCycles = 2 // Number of fast cycles before deceleration 36 decelerationFrames = 12 // Number of frames for deceleration phase 37 totalAnimationFrames = (fastCycles * framesPerCycle) + decelerationFrames // 24 + 12 = 36 38) 39 40// StatusBarTickMsg is sent every animation frame. 41type StatusBarTickMsg struct{} 42 43// NewStatusBar creates a new status bar. 44func NewStatusBar(width int) StatusBar { 45 return StatusBar{ 46 width: width, 47 } 48} 49 50// Init initializes the status bar. 51func (s StatusBar) Init() tea.Cmd { 52 return s.tick() 53} 54 55// maxFrames returns the maximum frame count across all icons. 56func (s StatusBar) maxFrames() int { 57 return max(s.autoplayFrames, s.infoFrames, s.warningFrames, 58 s.errorFrames, s.llmFrames, s.mcpFrames) 59} 60 61// tick returns a command that sends a tick message after the animation interval. 62// The tick rate varies: fast during initial cycles, slow during deceleration, stops when idle. 63func (s StatusBar) tick() tea.Cmd { 64 // Find the maximum frame count across all icons to determine animation phase 65 maxFrames := max(s.autoplayFrames, s.infoFrames, s.warningFrames, 66 s.errorFrames, s.llmFrames, s.mcpFrames) 67 68 // No animation needed if all icons are idle 69 if maxFrames == 0 { 70 return nil // Stop ticking when idle (icons at baseline/thinnest frame) 71 } 72 73 // Calculate tick rate based on animation phase 74 var tickRate time.Duration 75 76 if maxFrames > decelerationFrames { 77 // Fast phase: quick animation for first 24 frames (2 cycles) 78 tickRate = time.Second / animationFPSFast // 125ms (8 FPS) 79 } else { 80 // Deceleration phase: slow down for last 12 frames 81 tickRate = time.Second / animationFPSSlow // 500ms (2 FPS) 82 } 83 84 return tea.Tick(tickRate, func(time.Time) tea.Msg { 85 return StatusBarTickMsg{} 86 }) 87} 88 89// Update handles status bar updates. 90func (s StatusBar) Update(msg tea.Msg) (StatusBar, tea.Cmd) { 91 if _, ok := msg.(StatusBarTickMsg); ok { 92 // Advance animation frame 93 s.currentFrame = (s.currentFrame + 1) % framesPerCycle 94 95 // Decrement frame counters 96 if s.autoplayFrames > 0 { 97 s.autoplayFrames-- 98 } 99 if s.infoFrames > 0 { 100 s.infoFrames-- 101 } 102 if s.warningFrames > 0 { 103 s.warningFrames-- 104 } 105 if s.errorFrames > 0 { 106 s.errorFrames-- 107 } 108 if s.llmFrames > 0 { 109 s.llmFrames-- 110 } 111 if s.mcpFrames > 0 { 112 s.mcpFrames-- 113 } 114 115 return s, s.tick() 116 } 117 118 return s, nil 119} 120 121// SetWidth updates the status bar width. 122func (s *StatusBar) SetWidth(width int) { 123 s.width = width 124} 125 126// AnimateAutoplay triggers the autoplay icon animation. 127// Resets to full animation cycle on each event. 128// Returns a command to start/restart the animation tick if needed. 129func (s *StatusBar) AnimateAutoplay() tea.Cmd { 130 wasIdle := s.maxFrames() == 0 131 s.autoplayFrames = totalAnimationFrames 132 if wasIdle { 133 return s.tick() // Restart ticking when transitioning from idle 134 } 135 return nil 136} 137 138// AnimateInfo triggers the info icon animation. 139// Resets to full animation cycle on each event. 140// Returns a command to start/restart the animation tick if needed. 141func (s *StatusBar) AnimateInfo() tea.Cmd { 142 wasIdle := s.maxFrames() == 0 143 s.infoFrames = totalAnimationFrames 144 if wasIdle { 145 return s.tick() // Restart ticking when transitioning from idle 146 } 147 return nil 148} 149 150// AnimateWarning triggers the warning icon animation. 151// Resets to full animation cycle on each event. 152// Returns a command to start/restart the animation tick if needed. 153func (s *StatusBar) AnimateWarning() tea.Cmd { 154 wasIdle := s.maxFrames() == 0 155 s.warningFrames = totalAnimationFrames 156 if wasIdle { 157 return s.tick() // Restart ticking when transitioning from idle 158 } 159 return nil 160} 161 162// SetWarning sets the warning text. 163func (s *StatusBar) SetWarning(text string) tea.Cmd { 164 s.warningText = text 165 return s.AnimateWarning() 166} 167 168// ClearWarning clears the warning text. 169func (s *StatusBar) ClearWarning() { 170 s.warningText = "" 171} 172 173// AnimateError triggers the error icon animation. 174// Resets to full animation cycle on each event. 175// Returns a command to start/restart the animation tick if needed. 176func (s *StatusBar) AnimateError() tea.Cmd { 177 wasIdle := s.maxFrames() == 0 178 s.errorFrames = totalAnimationFrames 179 if wasIdle { 180 return s.tick() // Restart ticking when transitioning from idle 181 } 182 return nil 183} 184 185// AnimateLLM triggers the LLM connection icon animation. 186// Resets to full animation cycle on each event. 187// Returns a command to start/restart the animation tick if needed. 188func (s *StatusBar) AnimateLLM() tea.Cmd { 189 wasIdle := s.maxFrames() == 0 190 s.llmFrames = totalAnimationFrames 191 if wasIdle { 192 return s.tick() // Restart ticking when transitioning from idle 193 } 194 return nil 195} 196 197// AnimateMCP triggers the MCP connection icon animation. 198// Resets to full animation cycle on each event. 199// Returns a command to start/restart the animation tick if needed. 200func (s *StatusBar) AnimateMCP() tea.Cmd { 201 wasIdle := s.maxFrames() == 0 202 s.mcpFrames = totalAnimationFrames 203 if wasIdle { 204 return s.tick() // Restart ticking when transitioning from idle 205 } 206 return nil 207} 208 209// SetError sets the error text. 210func (s *StatusBar) SetError(text string) tea.Cmd { 211 s.errorText = text 212 return s.AnimateError() 213} 214 215// ClearError clears the error text. 216func (s *StatusBar) ClearError() { 217 s.errorText = "" 218} 219 220// SetAutoplayText sets the autoplay status text and animates the icon. 221func (s *StatusBar) SetAutoplayText(text string) tea.Cmd { 222 s.autoplayText = text 223 return s.AnimateAutoplay() 224} 225 226// ClearAutoplayText clears the autoplay text. 227func (s *StatusBar) ClearAutoplayText() { 228 s.autoplayText = "" 229} 230 231// View renders the status bar. 232func (s StatusBar) View() string { 233 // Left side: Status icon column (4 icons × 3 chars each = 12 chars) 234 autoplayIcon := s.renderIcon(s.autoplayFrames, autoplayIcons) 235 infoIcon := s.renderIcon(s.infoFrames, infoIcons) 236 warningIcon := s.renderIcon(s.warningFrames, warningIcons) 237 errorIcon := s.renderIcon(s.errorFrames, errorIcons) 238 239 leftIconColumn := IconAutoplayStyle.Render(autoplayIcon) + 240 IconInfoStyle.Render(infoIcon) + 241 IconWarningStyle.Render(warningIcon) + 242 IconErrorStyle.Render(errorIcon) 243 244 // Right side: Connection icon column (2 icons × 3 chars each = 6 chars) 245 llmIcon := s.renderIcon(s.llmFrames, llmIcons) 246 mcpIcon := s.renderIcon(s.mcpFrames, mcpIcons) 247 248 rightIconColumn := IconLLMStyle.Render(llmIcon) + 249 IconMCPStyle.Render(mcpIcon) 250 251 // Build icon parts with spacing - spaces need background too 252 spaceStyle := lipgloss.NewStyle().Background(styles.ColorBg) 253 leftIconsPart := spaceStyle.Render(" ") + leftIconColumn + spaceStyle.Render(" ") // 14 chars (1 + 12 + 1) 254 rightIconsPart := spaceStyle.Render(" ") + rightIconColumn // 7 chars (1 + 6) 255 256 // Middle: Status text (fills remaining width) 257 statusTextPlain, statusTextStyle := s.renderStatusText() 258 259 // Calculate available width for status text 260 // Total width - left icons (14) - right icons (7) = available 261 availableWidth := s.width - 14 - 7 262 if availableWidth < 0 { 263 availableWidth = 0 264 } 265 266 // Truncate text if too long (before styling) 267 // Need at least 3 chars for "..." truncation 268 if availableWidth < 3 { 269 statusTextPlain = "" 270 } else if len(statusTextPlain) > availableWidth { 271 statusTextPlain = statusTextPlain[:availableWidth-3] + "..." 272 } 273 274 // Apply style with full width so background fills 275 textStyle := statusTextStyle. 276 Background(styles.ColorBg). 277 Width(availableWidth) 278 textPart := textStyle.Render(statusTextPlain) 279 280 bar := leftIconsPart + textPart + rightIconsPart 281 282 // Apply status bar style (border) without width constraint 283 // We've already built the content to exact width 284 return StatusBarStyle.Render(bar) 285} 286 287// renderIcon renders an icon based on animation state. 288// When idle (frames=0), shows the baseline/thinnest frame (last in sequence). 289// When animating (frames>0), cycles through frames based on currentFrame. 290func (s StatusBar) renderIcon(frames int, icons []string) string { 291 if frames <= 0 { 292 // Idle: return the baseline/thinnest frame (last frame in sequence) 293 return icons[len(icons)-1] 294 } 295 296 // Animating: cycle through frames 297 iconIndex := s.currentFrame % len(icons) 298 return icons[iconIndex] 299} 300 301// renderStatusText returns the appropriate status text and style. 302// Priority: Error > Warning > Autoplay > Default 303// Returns plain text and the style to apply 304func (s StatusBar) renderStatusText() (string, lipgloss.Style) { 305 if s.errorText != "" { 306 return s.errorText, StatusTextErrorStyle 307 } 308 if s.warningText != "" { 309 return s.warningText, StatusTextStyle 310 } 311 if s.autoplayText != "" { 312 return "⟳ " + s.autoplayText, StatusTextStyle 313 } 314 return "All systems operational", StatusTextOKStyle 315} 316 317// Icon animation sequences 318// Each sequence progresses from full/thick to thin/empty, ending at the baseline frame 319var ( 320 // Autoplay: rotating circle, ends at ◔ (quarter circle) 321 autoplayIcons = []string{"●", "◕", "◑", "◔", "○", "◌", "○", "◔", "◑", "◕", "●", "◔"} 322 323 // Info: pulsing dot, ends at ◌ (empty circle) 324 infoIcons = []string{"●", "●", "◉", "◉", "◎", "◎", "○", "○", "◌", "◌", "○", "◌"} 325 326 // Warning: pulsing diamond, ends at ◇ (hollow diamond) 327 warningIcons = []string{"◆", "◆", "◈", "◈", "◇", "◇", "◈", "◈", "◆", "◆", "◈", "◇"} 328 329 // Error: flashing X, ends at space (empty) 330 errorIcons = []string{"✖", "✖", "✖", "✕", "✕", "✕", "✖", "✕", "✕", " ", "✕", " "} 331 332 // LLM: pulsing brain/network, ends at ◌ (empty circle) - cyan/teal color for thinking 333 llmIcons = []string{"●", "◉", "◉", "◎", "◎", "○", "○", "◌", "◌", "○", "◌", "◌"} 334 335 // MCP: rotating connection, ends at ○ (circle/dot) - purple color for server communication 336 mcpIcons = []string{"◐", "◓", "◑", "◒", "◐", "◓", "◑", "◒", "○", "◌", "○", "○"} 337)