RunWithSpinner runs a function while showing animated spinner
(status, detail string, fn func() error)
| 283 | |
| 284 | // RunWithSpinner runs a function while showing animated spinner |
| 285 | func (ui *AnimatedBuildUI) RunWithSpinner(status, detail string, fn func() error) error { |
| 286 | ui.SetStatus(mascot.StateCompiling, status, detail) |
| 287 | |
| 288 | if !ui.isTTY { |
| 289 | // No animation in non-TTY |
| 290 | return fn() |
| 291 | } |
| 292 | |
| 293 | // Start spinner animation |
| 294 | done := make(chan error, 1) |
| 295 | go func() { |
| 296 | done <- fn() |
| 297 | }() |
| 298 | |
| 299 | ticker := time.NewTicker(100 * time.Millisecond) |
| 300 | defer ticker.Stop() |
| 301 | |
| 302 | // Print initial spinner line |
| 303 | spinnerStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#7D56F4")) |
| 304 | statusStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#CDD6F4")) |
| 305 | |
| 306 | for { |
| 307 | select { |
| 308 | case err := <-done: |
| 309 | // Clear spinner line |
| 310 | fmt.Print("\r\033[2K") |
| 311 | return err |
| 312 | case <-ticker.C: |
| 313 | spinner := ui.spinnerFrames[ui.frameIndex] |
| 314 | ui.frameIndex = (ui.frameIndex + 1) % len(ui.spinnerFrames) |
| 315 | fmt.Printf("\r %s %s", spinnerStyle.Render(spinner), statusStyle.Render(status)) |
| 316 | } |
| 317 | } |
| 318 | } |