Convert Figma logo to code with AI

briandowns logospinner

Go (golang) package with 90 configurable terminal spinner/progress indicators.

2,327
130
2,327
15

Top Related Projects

2,290

multi progress bar for Go cli applications

A really basic thread-safe progress bar for Golang applications

3,634

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

  1. Basic usage:
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
// Perform some task
time.Sleep(4 * time.Second)
s.Stop()
  1. 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()
  1. 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:

  1. Install the package:

    go get github.com/briandowns/spinner
    
  2. Import the package in your Go file:

    import "github.com/briandowns/spinner"
    
  3. Create and use a spinner:

    s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
    s.Start()
    // Your code here
    s.Stop()
    

Competitor Comparisons

2,290

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.

3,634

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Spinner

GoDoc CircleCI

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)

indexcharacter setsample gif
0←↖↑↗→↘↓↙Sample Gif
1▁▃▄▅▆▇█▇▆▅▄▃▁Sample Gif
2▖▘▝▗Sample Gif
3┤┘┴└├┌┬┐Sample Gif
4◢◣◤◥Sample Gif
5◰◳◲◱Sample Gif
6◴◷◶◵Sample Gif
7◐◓◑◒Sample Gif
8.oO@*Sample Gif
9|/-\Sample Gif
10◡◡⊙⊙◠◠Sample Gif
11⣾⣽⣻⢿⡿⣟⣯⣷Sample Gif
12>))'> >))'> >))'> >))'> >))'> <'((< <'((< <'((<Sample Gif
13⠁⠂⠄⡀⢀⠠⠐⠈Sample Gif
14⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏Sample Gif
15abcdefghijklmnopqrstuvwxyzSample Gif
16▉▊▋▌▍▎▏▎▍▌▋▊▉Sample Gif
17■□▪▫Sample Gif
18←↑→↓Sample Gif
19╫╪Sample Gif
20⇐⇖⇑⇗⇒⇘⇓⇙Sample Gif
21⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈Sample Gif
22⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈Sample Gif
23⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁Sample Gif
24⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋Sample Gif
25ヲァィゥェォャュョッアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワンSample Gif
26. .. ...Sample Gif
27▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▏▎▍▌▋▊▉█▇▆▅▄▃▂▁Sample Gif
28.oO°Oo.Sample Gif
29+xSample Gif
30v<^>Sample Gif
31>>---> >>---> >>---> >>---> >>---> <---<< <---<< <---<< <---<< <---<<Sample Gif
32| || ||| |||| ||||| |||||| ||||| |||| ||| || |Sample Gif
33[] [=] [==] [===] [====] [=====] [======] [=======] [========] [=========] [==========]Sample Gif
34(*---------) (-*--------) (--*-------) (---*------) (----*-----) (-----*----) (------*---) (-------*--) (--------*-) (---------*)Sample Gif
35█▒▒▒▒▒▒▒▒▒ ███▒▒▒▒▒▒▒ █████▒▒▒▒▒ ███████▒▒▒ ██████████Sample Gif
36[ ] [=> ] [===> ] [=====> ] [======> ] [========> ] [==========> ] [============> ] [==============> ] [================> ] [==================> ] [===================>]Sample Gif
37🕐 🕑 🕒 🕓 🕔 🕕 🕖 🕗 🕘 🕙 🕚 🕛Sample Gif
38🕐 🕜 🕑 🕝 🕒 🕞 🕓 🕟 🕔 🕠 🕕 🕡 🕖 🕢 🕗 🕣 🕘 🕤 🕙 🕥 🕚 🕦 🕛 🕧Sample Gif
39🌍 🌎 🌏Sample Gif
40◜ ◝ ◞ ◟Sample Gif
41⬒ ⬔ ⬓ ⬕Sample Gif
42⬖ ⬘ ⬗ ⬙Sample Gif
43[>>> >] []>>>> [] [] >>>> [] [] >>>> [] [] >>>> [] [] >>>>[] [>> >>]Sample Gif

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!