Below is a creative take on a progress indicator for a CLI Go application using ANSI characters. Building on the previous responses, I've introduced new and more imaginative indicators to keep things fresh. This version includes:
- Firework Explosion Indicator: As progress advances, it simulates fireworks bursting with colorful ANSI explosions.
- Matrix Falling Code Indicator: Inspired by The Matrix, it displays falling code-like characters that "fill up" as progress increases.
- Robot Assembly Indicator: A thematic progress bar where a robot is "assembled" stage by stage using emojis and ASCII art.
These indicators use ANSI escape codes for colors, cursor control, and animations. I've made them modular and easy to integrate into your Go application.
Here's the complete Go code:
package main
import (
"fmt"
"strings"
"time"
)
// ANSI Color Codes and Cursor Control
const (
reset = "\x1b[0m"
red = "\x1b[31m"
green = "\x1b[32m"
yellow = "\x1b[33m"
blue = "\x1b[34m"
magenta = "\x1b[35m"
cyan = "\x1b[36m"
white = "\x1b[97m"
hideCursor = "\x1b[?25l"
showCursor = "\x1b[?25h"
)
// Firework Explosion Indicator
// Simulates fireworks bursting as progress increases
func fireworkIndicator(current, total int, iteration int) {
if total <= 0 {
return
}
percent := float64(current) / float64(total) * 100
// Firework stages: Build up and explode based on progress
stages := []string{
".", // Calm
"🌟", // Spark
"💥", // Explosion
}
stageIndex := int(percent / (100.0 / float64(len(stages)))) % len(stages)
fireworkChar := stages[stageIndex]
// Add colorful bursts
colors := []string{red, yellow, blue, magenta, cyan}
color := colors[iteration%len(colors)] // Cycle colors for animation
// Generate trailing sparks
sparks := strings.Repeat("*", int(percent/10)) // More sparks as progress grows
fmt.Printf("\r%sFireworks: %s %s%s (%.1f%%)%s", color, fireworkChar, sparks, reset, percent, strings.Repeat(" ", 20)) // Padding for overwrite
}
// Matrix Falling Code Indicator
// Falling code effect inspired by The Matrix
func matrixIndicator(current, total int, width int) {
if total <= 0 {
return
}
percent := float64(current) / float64(total)
filledWidth := int(percent * float64(width))
// Generate falling code: Random characters in green, filling up
matrixChars := []string{"0", "1", "A", "B", "X", "Y", "#", "@"}
line := ""
for i := 0; i < width; i++ {
if i < filledWidth {
line += green + string(matrixChars[i%len(matrixChars)]) + reset
} else {
line += " "
}
}
fmt.Printf("\r%sMatrix Progress: [%s] %.1f%%%s", cyan, line, percent*100, strings.Repeat(" ", 10))
}
// Robot Assembly Indicator
// Assembles a robot stage by stage with emojis and ASCII art
func robotAssemblyIndicator(current, total int) {
if total <= 0 {
return
}
percent := float64(current) / float64(total) * 100
stages := []string{
"🤖 (Head only)",
"🤖🛠 (Adding body)",
"🤖🛠🦿 (Attaching limbs)",
"🤖 (Fully assembled!)",
}
stageIndex := int(percent / (100.0 / float64(len(stages)))) % len(stages)
robotStage := stages[stageIndex]
// Add some ASCII art flair
art := "Assembling: " + robotStage + " " + strings.Repeat("⚙️", int(percent/25)) // Gears for progress
fmt.Printf("\r%s%s (%.1f%%)%s", green, art, percent, strings.Repeat(" ", 20)) // Padding
}
func main() {
// Hide cursor for smooth animations and ensure it's shown on exit
fmt.Print(hideCursor)
defer fmt.Print(showCursor)
totalTasks := 100
width := 40 // Width for indicators like Matrix
fmt.Println("--- Firework Explosion Indicator ---")
for i := 0; i <= totalTasks; i++ {
fireworkIndicator(i, totalTasks, i) // Pass iteration for color cycling
time.Sleep(50 * time.Millisecond)
}
fmt.Println("\nFireworks Complete!\n")
fmt.Println("--- Matrix Falling Code Indicator ---")
for i := 0; i <= totalTasks; i++ {
matrixIndicator(i, totalTasks, width)
time.Sleep(50 * time.Millisecond)
}
fmt.Println("\nMatrix Simulation Done!\n")
fmt.Println("--- Robot Assembly Indicator ---")
for i := 0; i <= totalTasks; i++ {
robotAssemblyIndicator(i, totalTasks)
time.Sleep(50 * time.Millisecond)
}
fmt.Println("\nRobot Fully Assembled!\n")
fmt.Println("All new indicators demonstrated. Get creative and integrate these into your app!")
}
Explanation:
-
ANSI Usage:
- Colors (e.g.,
red, green) are used to make the indicators visually appealing and thematic.
\r is used to return the cursor to the start of the line, allowing each update to overwrite the previous one for smooth animations.
- Padding with
strings.Repeat(" ", 20) ensures that longer previous outputs are cleared, preventing artifacts.
-
New Indicators:
- Firework Explosion: This creates a fun, explosive effect. As progress increases, the firework "bursts" and generates trailing sparks. Colors cycle for added dynamism.
- Matrix Falling Code: Mimics the green falling code from The Matrix. The bar fills with random characters, giving a high-tech feel.
- Robot Assembly: Uses emojis (e.g., 🤖, 🛠) to show stages of assembly. It's more narrative, making progress feel like a story.
-
Main Function:
- Demonstrates each indicator in a loop, simulating work with
time.Sleep.
- Hides the cursor at the start and shows it at the end for a cleaner experience.
- Each indicator is run sequentially with a newline for separation.
-
Creativity Elements:
- Themed designs: Fireworks for celebration, Matrix for a sci-fi vibe, and Robot for a fun assembly process.
- Emojis and ASCII art: Integrated to make the output more engaging (note: ensure your terminal supports emojis).
- Animations: Parameters like
iteration allow for cycling effects without complex logic.
How to Run:
- Save the code as
progress.go.
- In your terminal, run
go run progress.go.
- Watch the indicators animate in real-time!
This builds on the previous versions by introducing fresh themes. If you'd like modifications, such as adding more indicators or making them customizable, let me know!