Create a progress indicator using ANSI characters for use in a CLI Go application. Be…
- User
Create a progress indicator using ANSI characters for use in a CLI Go application. Be creative.
- Assistant
Here’s a fun “bouncing ball”-style progress indicator for your Go CLI. It uses ANSI escapes for colors, hides the cursor while animating, and makes a little “o” bounce back-and-forth inside a bar as your task advances.
package main import ( "fmt" "os" "os/signal" "syscall" "time" ) // ANSI escape codes const ( reset = "\x1b[0m" green = "\x1b[32m" hideCursor = "\x1b[?25l" showCursor = "\x1b[?25h" clearLine = "\r\x1b[2K" // CR + clear entire line ) // Bouncer holds the state of our bouncing-ball indicator. type Bouncer struct { width int // total width of the track position int // current position of the ball forward bool // direction flag } // NewBouncer creates a new bouncer with given track width. func NewBouncer(width int) *Bouncer { return &Bouncer{ width: width, position: 0, forward: true, } } // Step moves the ball one position (and flips direction at ends). func (b *Bouncer) Step() { if b.forward { b.position++ if b.position >= b.width-1 { b.forward = false } } else { b.position-- if b.position <= 0 { b.forward = true } } } // Render prints the current frame of the bouncing-ball bar. func (b *Bouncer) Render(prefix string, percent float64) { // Build the track with spaces and one 'o' bar := make([]rune, b.width) for i := range bar { bar[i] = ' ' } bar[b.position] = 'o' // Colorize the ball and the filled part filled := int(percent * float64(b.width)) for i := 0; i < filled; i++ { if i == b.position { bar[i] = '◉' // make the bouncing ball bold when "filled" } else { bar[i] = '━' } } // Combine and print fmt.Print(clearLine) // clear the current line fmt.Printf("%s[%s%s%s] %6.2f%%", prefix, green, string(bar), reset, percent*100, ) } func main() { // Hide cursor on start, ensure it's shown on exit fmt.Print(hideCursor) defer fmt.Print(showCursor) // Catch Ctrl+C so we restore the cursor before exit sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) go func() { <-sigs fmt.Print(showCursor) os.Exit(0) }() total := 200 b := NewBouncer(30) prefix := "Processing: " for i := 0; i <= total; i++ { pct := float64(i) / float64(total) b.Render(prefix, pct) b.Step() time.Sleep(30 * time.Millisecond) } // Finish up fmt.Print(clearLine) fmt.Printf("%s[%s%s%s] %6.2f%%\n", prefix, green, string(make([]rune, b.width)), // empty final bar reset, 100.0, ) fmt.Print(showCursor) fmt.Println("Done!") }How it works:
• We define a
Bouncerstruct that tracks a “ball” position and direction. • Each iteration we callStep()to move the ball (it bounces at both ends). •Render()draws a fixed-length bar:
– Filled portion (━) up to the current percentage
– A bold “◉” for the ball if it’s in the filled zone, otherwise a simple “o”
• We use\x1b[2K+\rto clear the line, then re-print the updated bar in place. • The cursor is hidden during animation (\x1b[?25l) and restored on exit.Feel free to tweak the width, colors or the ball characters to suit your taste! 🚀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.