spinner
Go (golang) package with 90 configurable terminal spinner/progress indicators.
Top Related Projects
multi progress bar for Go cli applications
A really basic thread-safe progress bar for Golang applications
Console progress bar for Golang
Quick Overview
briandowns/spinner is a Go package that provides a simple and customizable terminal spinner for command-line applications. It offers a variety of spinner types and options to display progress or loading indicators in the console, enhancing user experience during long-running operations.
Pros
- Easy to use and integrate into existing Go projects
- Supports multiple spinner styles and customization options
- Concurrent-safe, allowing use in multi-threaded applications
- Lightweight with minimal dependencies
Cons
- Limited to terminal/console applications
- May not be suitable for complex progress reporting scenarios
- Requires manual management of spinner state (start/stop)
- Limited cross-platform support for some advanced features
Code Examples
- Basic usage:
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
// Perform some task
time.Sleep(4 * time.Second)
s.Stop()
- Customizing spinner appearance:
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond)
s.Color("red")
s.Suffix = " Loading..."
s.FinalMSG = "Complete!\n"
s.Start()
time.Sleep(4 * time.Second)
s.Stop()
- Using spinner with a channel:
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Suffix = " Processing..."
s.Start()
ch := make(chan bool)
go func() {
// Perform task
time.Sleep(4 * time.Second)
ch <- true
}()
<-ch
s.Stop()
Getting Started
To use the spinner in your Go project:
-
Install the package:
go get github.com/briandowns/spinner
-
Import the package in your Go file:
import "github.com/briandowns/spinner"
-
Create and use a spinner:
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) s.Start() // Your code here s.Stop()
Competitor Comparisons
multi progress bar for Go cli applications
Pros of mpb
- Supports multiple progress bars simultaneously
- Offers more customization options and styling features
- Provides better support for concurrent operations
Cons of mpb
- More complex API, potentially steeper learning curve
- Larger codebase and dependencies
Code Comparison
spinner:
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
time.Sleep(4 * time.Second)
s.Stop()
mpb:
p := mpb.New(mpb.WithWidth(64))
bar := p.AddBar(100,
mpb.PrependDecorators(
decor.Name("MyTask"),
decor.Percentage(decor.WCSyncSpace),
),
)
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(50 * time.Millisecond)
}
p.Wait()
Summary
spinner is simpler and more lightweight, focusing on basic spinner functionality. mpb offers more advanced features for progress bars, including multi-bar support and extensive customization options. Choose spinner for simple, single-threaded tasks, and mpb for complex, concurrent operations requiring detailed progress tracking.
A really basic thread-safe progress bar for Golang applications
Pros of progressbar
- More feature-rich, offering multiple progress bar styles and customization options
- Supports concurrent progress bars for tracking multiple tasks simultaneously
- Includes built-in ETA calculation and display
Cons of progressbar
- Slightly more complex API, potentially requiring more setup for basic use cases
- Larger codebase and dependencies, which may impact performance in some scenarios
Code Comparison
progressbar:
bar := progressbar.Default(100)
for i := 0; i < 100; i++ {
bar.Add(1)
time.Sleep(10 * time.Millisecond)
}
spinner:
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
time.Sleep(4 * time.Second)
s.Stop()
progressbar offers a more traditional progress bar with percentage completion, while spinner provides a simple spinning animation for indeterminate progress. progressbar is better suited for tasks with known total steps, whereas spinner is ideal for operations with unknown duration or progress.
Both libraries are easy to use and integrate into Go projects, but progressbar offers more advanced features at the cost of slightly increased complexity. spinner is more lightweight and straightforward, making it a good choice for simple progress indicators or loading animations.
Console progress bar for Golang
Pros of pb
- More comprehensive progress bar functionality, including ETA and speed
- Supports multiple concurrent progress bars
- Offers customizable output formats and styles
Cons of pb
- Slightly more complex API compared to spinner
- Focused primarily on progress bars, less versatile for other types of indicators
Code Comparison
pb:
bar := pb.New(100)
bar.Start()
for i := 0; i < 100; i++ {
bar.Increment()
time.Sleep(time.Millisecond * 10)
}
bar.Finish()
spinner:
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
time.Sleep(4 * time.Second)
s.Stop()
Summary
pb is a more feature-rich library for progress bars, offering advanced functionality like ETA and speed calculations. It's ideal for scenarios requiring detailed progress tracking. spinner, on the other hand, is simpler and more focused on providing animated spinners, making it a good choice for indeterminate progress indicators or loading animations.
While pb excels in progress bar functionality, spinner offers a wider variety of spinner styles and is easier to implement for simple loading indicators. The choice between the two depends on the specific requirements of your project and the type of visual feedback you want to provide to users.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Spinner
spinner is a simple package to add a spinner / progress indicator to any terminal application. Examples can be found below as well as full examples in the examples directory.
For more detail about the library and its features, reference your local godoc once installed.
Contributions welcome!
Installation
go get github.com/briandowns/spinner
Available Character Sets
90 Character Sets. Some examples below:
(Numbered by their slice index)
index | character set | sample gif |
---|---|---|
0 | ââââââââ | |
1 | ââââ
ââââââ
âââ | |
2 | ââââ | |
3 | â¤ââ´ââââ¬â | |
4 | â¢â£â¤â¥ | |
5 | â°â³â²â± | |
6 | â´â·â¶âµ | |
7 | ââââ | |
8 | .oO@* | |
9 | |/-\ | |
10 | â¡â¡âââ â | |
11 | ⣾⣽⣻⢿⡿â£â£¯â£· | |
12 | >))'> >))'> >))'> >))'> >))'> <'((< <'((< <'((< | |
13 | â â â â¡â¢â â â | |
14 | â â ⠹⠸⠼⠴⠦⠧â â | |
15 | abcdefghijklmnopqrstuvwxyz | |
16 | âââââââââââââ | |
17 | â â¡âªâ« | |
18 | ââââ | |
19 | â«âª | |
20 | ââââââââ | |
21 | â â â â â â â â â ⠲⠴⠤â â â ¤â â ⠤⠦â â â â â â â â â â | |
22 | â â â â â â â â â ⠦⠤â â ⠤⠦â â â â â â â â â | |
23 | â â â â â â â â ⠲⠴⠤â â ⠤⠴⠲â â â â â â â â | |
24 | â â â â â â â ⠲⠴⠦â â â â â â â | |
25 | ヲァィゥェォャï½ï½®ï½¯ï½±ï½²ï½³ï½´ï½µï½¶ï½·ï½¸ï½¹ï½ºï½»ï½¼ï½½ï½¾ï½¿ï¾ï¾ï¾ï¾ï¾ï¾
ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ | |
26 | . .. ... | |
27 | âââââ
âââââââââââââââââââââ
ââââ | |
28 | .oO°Oo. | |
29 | +x | |
30 | v<^> | |
31 | >>---> >>---> >>---> >>---> >>---> <---<< <---<< <---<< <---<< <---<< | |
32 | | || ||| |||| ||||| |||||| ||||| |||| ||| || | | |
33 | [] [=] [==] [===] [====] [=====] [======] [=======] [========] [=========] [==========] | |
34 | (*---------) (-*--------) (--*-------) (---*------) (----*-----) (-----*----) (------*---) (-------*--) (--------*-) (---------*) | |
35 | ââââââââââ ââââââââââ ââââââââââ ââââââââââ ââââââââââ | |
36 | [ ] [=> ] [===> ] [=====> ] [======> ] [========> ] [==========> ] [============> ] [==============> ] [================> ] [==================> ] [===================>] | |
37 | ð ð ð ð ð ð ð ð ð ð ð ð | |
38 | ð ð ð ð ð ð ð ð ð ð ð ð¡ ð ð¢ ð ð£ ð ð¤ ð ð¥ ð ð¦ ð ð§ | |
39 | ð ð ð | |
40 | â â â â | |
41 | ⬠⬠⬠⬠| |
42 | ⬠⬠⬠⬠| |
43 | [>>> >] []>>>> [] [] >>>> [] [] >>>> [] [] >>>> [] [] >>>>[] [>> >>] |
Features
- Start
- Stop
- Restart
- Reverse direction
- Update the spinner character set
- Update the spinner speed
- Prefix or append text
- Change spinner color, background, and text attributes such as bold / italics
- Get spinner status
- Chain, pipe, redirect output
- Output final string on spinner/indicator completion
Examples
package main
import (
"github.com/briandowns/spinner"
"time"
)
func main() {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner
s.Start() // Start the spinner
time.Sleep(4 * time.Second) // Run for some time to simulate work
s.Stop()
}
Update the character set and restart the spinner
s.UpdateCharSet(spinner.CharSets[1]) // Update spinner to use a different character set
s.Restart() // Restart the spinner
time.Sleep(4 * time.Second)
s.Stop()
Update spin speed and restart the spinner
s.UpdateSpeed(200 * time.Millisecond) // Update the speed the spinner spins at
s.Restart()
time.Sleep(4 * time.Second)
s.Stop()
Reverse the direction of the spinner
s.Reverse() // Reverse the direction the spinner is spinning
s.Restart()
time.Sleep(4 * time.Second)
s.Stop()
Provide your own spinner
(or send me an issue or pull request to add to the project)
someSet := []string{"+", "-"}
s := spinner.New(someSet, 100*time.Millisecond)
Prefix or append text to the spinner
s.Prefix = "prefixed text: " // Prefix text before the spinner
s.Suffix = " :appended text" // Append text after the spinner
Set or change the color of the spinner. Default color is white. The spinner will need to be restarted to pick up the change.
s.Color("red") // Set the spinner color to red
You can specify both the background and foreground color, as well as additional attributes such as bold
or underline
.
s.Color("red", "bold") // Set the spinner color to a bold red
To set the background to black, the foreground to a bold red:
s.Color("bgBlack", "bold", "fgRed")
Below is the full color and attribute list:
// default colors
red
black
green
yellow
blue
magenta
cyan
white
// attributes
reset
bold
faint
italic
underline
blinkslow
blinkrapid
reversevideo
concealed
crossedout
// foreground text
fgBlack
fgRed
fgGreen
fgYellow
fgBlue
fgMagenta
fgCyan
fgWhite
// foreground Hi-Intensity text
fgHiBlack
fgHiRed
fgHiGreen
fgHiYellow
fgHiBlue
fgHiMagenta
fgHiCyan
fgHiWhite
// background text
bgBlack
bgRed
bgGreen
bgYellow
bgBlue
bgMagenta
bgCyan
bgWhite
// background Hi-Intensity text
bgHiBlack
bgHiRed
bgHiGreen
bgHiYellow
bgHiBlue
bgHiMagenta
bgHiCyan
bgHiWhite
Generate a sequence of numbers
setOfDigits := spinner.GenerateNumberSequence(25) // Generate a 25 digit string of numbers
s := spinner.New(setOfDigits, 100*time.Millisecond)
Get spinner status
fmt.Println(s.Active())
Unix pipe and redirect
Feature suggested and write up by dekz
Setting the Spinner Writer to Stderr helps show progress to the user, with the enhancement to chain, pipe or redirect the output.
This is the preferred method of setting a Writer at this time.
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
s.Suffix = " Encrypting data..."
s.Start()
// Encrypt the data into ciphertext
fmt.Println(os.Stdout, ciphertext)
> myprog encrypt "Secret text" > encrypted.txt
⣯ Encrypting data...
> cat encrypted.txt
1243hjkbas23i9ah27sj39jghv237n2oa93hg83
Final String Output
Add additional output when the spinner/indicator has completed. The "final" output string can be multi-lined and will be written to wherever the io.Writer
has been configured for.
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.FinalMSG = "Complete!\nNew line!\nAnother one!\n"
s.Start()
time.Sleep(4 * time.Second)
s.Stop()
Output
Complete!
New line!
Another one!
Top Related Projects
multi progress bar for Go cli applications
A really basic thread-safe progress bar for Golang applications
Console progress bar for Golang
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot