| 1 |
# Spinner |
| 2 |
|
| •3 |
[](https://godoc.org/github.com/briandowns/spinner) [](https://circleci.com/gh/briandowns/spinner) |
| 4 |
|
| 5 |
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. |
| 6 |
|
| 7 |
For more detail about the library and its features, reference your local godoc once installed. |
| 8 |
|
| 9 |
Contributions welcome! |
| 10 |
|
| 11 |
## Installation |
| 12 |
|
| 13 |
```bash |
| 14 |
go get github.com/briandowns/spinner |
| 15 |
``` |
| 16 |
|
| 17 |
## Available Character Sets |
| 18 |
|
| 19 |
90 Character Sets. Some examples below: |
| 20 |
|
| 21 |
(Numbered by their slice index) |
| 22 |
|
| 23 |
| index | character set | sample gif | |
| 24 |
| ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------- | |
| 25 |
| 0 | ```←↖↑↗→↘↓↙``` |  | |
| 26 |
| 1 | ```▁▃▄▅▆▇█▇▆▅▄▃▁``` |  | |
| 27 |
| 2 | ```▖▘▝▗``` |  | |
| 28 |
| 3 | ```┤┘┴└├┌┬┐``` |  | |
| 29 |
| 4 | ```◢◣◤◥``` |  | |
| 30 |
| 5 | ```◰◳◲◱``` |  | |
| 31 |
| 6 | ```◴◷◶◵``` |  | |
| 32 |
| 7 | ```◐◓◑◒``` |  | |
| 33 |
| 8 | ```.oO@*``` |  | |
| 34 |
| 9 | ```\|/-\``` |  | |
| 35 |
| 10 | ```◡◡⊙⊙◠◠``` |  | |
| 36 |
| 11 | ```⣾⣽⣻⢿⡿⣟⣯⣷``` |  | |
| 37 |
| 12 | ```>))'> >))'> >))'> >))'> >))'> <'((< <'((< <'((<``` |  | |
| 38 |
| 13 | ```⠁⠂⠄⡀⢀⠠⠐⠈``` |  | |
| 39 |
| 14 | ```⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏``` |  | |
| 40 |
| 15 | ```abcdefghijklmnopqrstuvwxyz``` |  | |
| 41 |
| 16 | ```▉▊▋▌▍▎▏▎▍▌▋▊▉``` |  | |
| 42 |
| 17 | ```■□▪▫``` |  | |
| 43 |
| 18 | ```←↑→↓``` |  | |
| 44 |
| 19 | ```╫╪``` |  | |
| 45 |
| 20 | ```⇐⇖⇑⇗⇒⇘⇓⇙``` |  | |
| 46 |
| 21 | ```⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈``` |  | |
| 47 |
| 22 | ```⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈``` |  | |
| 48 |
| 23 | ```⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁``` |  | |
| 49 |
| 24 | ```⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋``` |  | |
| 50 |
| 25 | ```ヲァィゥェォャュョッアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン``` |  | |
| 51 |
| 26 | ```. .. ...``` |  | |
| 52 |
| 27 | ```▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▏▎▍▌▋▊▉█▇▆▅▄▃▂▁``` |  | |
| 53 |
| 28 | ```.oO°Oo.``` |  | |
| 54 |
| 29 | ```+x``` |  | |
| 55 |
| 30 | ```v<^>``` |  | |
| 56 |
| 31 | ```>>---> >>---> >>---> >>---> >>---> <---<< <---<< <---<< <---<< <---<<``` |  | |
| 57 |
| 32 | ```\| \|\| \|\|\| \|\|\|\| \|\|\|\|\| \|\|\|\|\|\| \|\|\|\|\| \|\|\|\| \|\|\| \|\| \|``` |  | |
| 58 |
| 33 | ```[] [=] [==] [===] [====] [=====] [======] [=======] [========] [=========] [==========]``` |  | |
| 59 |
| 34 | ```(*---------) (-*--------) (--*-------) (---*------) (----*-----) (-----*----) (------*---) (-------*--) (--------*-) (---------*)``` |  | |
| 60 |
| 35 | ```█▒▒▒▒▒▒▒▒▒ ███▒▒▒▒▒▒▒ █████▒▒▒▒▒ ███████▒▒▒ ██████████``` |  | |
| 61 |
| 36 | ```[ ] [=> ] [===> ] [=====> ] [======> ] [========> ] [==========> ] [============> ] [==============> ] [================> ] [==================> ] [===================>]``` |  | |
| 62 |
| 37 | ```🕐 🕑 🕒 🕓 🕔 🕕 🕖 🕗 🕘 🕙 🕚 🕛``` |  | |
| 63 |
| 38 | ```🕐 🕜 🕑 🕝 🕒 🕞 🕓 🕟 🕔 🕠 🕕 🕡 🕖 🕢 🕗 🕣 🕘 🕤 🕙 🕥 🕚 🕦 🕛 🕧``` |  | |
| 64 |
| 39 | ```🌍 🌎 🌏``` |  | |
| 65 |
| 40 | ```◜ ◝ ◞ ◟``` |  | |
| 66 |
| 41 | ```⬒ ⬔ ⬓ ⬕``` |  | |
| 67 |
| 42 | ```⬖ ⬘ ⬗ ⬙``` |  | |
| 68 |
| 43 | ```[>>> >] []>>>> [] [] >>>> [] [] >>>> [] [] >>>> [] [] >>>>[] [>> >>]``` |  | |
| 69 |
|
| 70 |
## Features |
| 71 |
|
| 72 |
* Start |
| 73 |
* Stop |
| 74 |
* Restart |
| 75 |
* Reverse direction |
| 76 |
* Update the spinner character set |
| 77 |
* Update the spinner speed |
| 78 |
* Prefix or append text |
| 79 |
* Change spinner color, background, and text attributes such as bold / italics |
| 80 |
* Get spinner status |
| 81 |
* Chain, pipe, redirect output |
| 82 |
* Output final string on spinner/indicator completion |
| 83 |
|
| 84 |
## Examples |
| 85 |
|
| 86 |
```Go |
| 87 |
package main |
| 88 |
|
| 89 |
import ( |
| 90 |
"github.com/briandowns/spinner" |
| 91 |
"time" |
| 92 |
) |
| 93 |
|
| 94 |
func main() { |
| 95 |
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner |
| 96 |
s.Start() // Start the spinner |
| 97 |
time.Sleep(4 * time.Second) // Run for some time to simulate work |
| 98 |
s.Stop() |
| 99 |
} |
| 100 |
``` |
| 101 |
|
| 102 |
## Update the character set and restart the spinner |
| 103 |
|
| 104 |
```Go |
| 105 |
s.UpdateCharSet(spinner.CharSets[1]) // Update spinner to use a different character set |
| 106 |
s.Restart() // Restart the spinner |
| 107 |
time.Sleep(4 * time.Second) |
| 108 |
s.Stop() |
| 109 |
``` |
| 110 |
|
| 111 |
## Update spin speed and restart the spinner |
| 112 |
|
| 113 |
```Go |
| 114 |
s.UpdateSpeed(200 * time.Millisecond) // Update the speed the spinner spins at |
| 115 |
s.Restart() |
| 116 |
time.Sleep(4 * time.Second) |
| 117 |
s.Stop() |
| 118 |
``` |
| 119 |
|
| 120 |
## Reverse the direction of the spinner |
| 121 |
|
| 122 |
```Go |
| 123 |
s.Reverse() // Reverse the direction the spinner is spinning |
| 124 |
s.Restart() |
| 125 |
time.Sleep(4 * time.Second) |
| 126 |
s.Stop() |
| 127 |
``` |
| 128 |
|
| 129 |
## Provide your own spinner |
| 130 |
|
| 131 |
(or send me an issue or pull request to add to the project) |
| 132 |
|
| 133 |
```Go |
| 134 |
someSet := []string{"+", "-"} |
| 135 |
s := spinner.New(someSet, 100*time.Millisecond) |
| 136 |
``` |
| 137 |
|
| 138 |
## Prefix or append text to the spinner |
| 139 |
|
| 140 |
```Go |
| 141 |
s.Prefix = "prefixed text: " // Prefix text before the spinner |
| 142 |
s.Suffix = " :appended text" // Append text after the spinner |
| 143 |
``` |
| 144 |
|
| 145 |
## Set or change the color of the spinner. Default color is white. The spinner will need to be restarted to pick up the change. |
| 146 |
|
| 147 |
```Go |
| 148 |
s.Color("red") // Set the spinner color to red |
| 149 |
``` |
| 150 |
|
| 151 |
You can specify both the background and foreground color, as well as additional attributes such as `bold` or `underline`. |
| 152 |
|
| 153 |
```Go |
| 154 |
s.Color("red", "bold") // Set the spinner color to a bold red |
| 155 |
``` |
| 156 |
|
| 157 |
To set the background to black, the foreground to a bold red: |
| 158 |
|
| 159 |
```Go |
| 160 |
s.Color("bgBlack", "bold", "fgRed") |
| 161 |
``` |
| 162 |
|
| 163 |
Below is the full color and attribute list: |
| 164 |
|
| 165 |
```Go |
| 166 |
// default colors |
| 167 |
red |
| 168 |
black |
| 169 |
green |
| 170 |
yellow |
| 171 |
blue |
| 172 |
magenta |
| 173 |
cyan |
| 174 |
white |
| 175 |
|
| 176 |
// attributes |
| 177 |
reset |
| 178 |
bold |
| 179 |
faint |
| 180 |
italic |
| 181 |
underline |
| 182 |
blinkslow |
| 183 |
blinkrapid |
| 184 |
reversevideo |
| 185 |
concealed |
| 186 |
crossedout |
| 187 |
|
| 188 |
// foreground text |
| 189 |
fgBlack |
| 190 |
fgRed |
| 191 |
fgGreen |
| 192 |
fgYellow |
| 193 |
fgBlue |
| 194 |
fgMagenta |
| 195 |
fgCyan |
| 196 |
fgWhite |
| 197 |
|
| 198 |
// foreground Hi-Intensity text |
| 199 |
fgHiBlack |
| 200 |
fgHiRed |
| 201 |
fgHiGreen |
| 202 |
fgHiYellow |
| 203 |
fgHiBlue |
| 204 |
fgHiMagenta |
| 205 |
fgHiCyan |
| 206 |
fgHiWhite |
| 207 |
|
| 208 |
// background text |
| 209 |
bgBlack |
| 210 |
bgRed |
| 211 |
bgGreen |
| 212 |
bgYellow |
| 213 |
bgBlue |
| 214 |
bgMagenta |
| 215 |
bgCyan |
| 216 |
bgWhite |
| 217 |
|
| 218 |
// background Hi-Intensity text |
| 219 |
bgHiBlack |
| 220 |
bgHiRed |
| 221 |
bgHiGreen |
| 222 |
bgHiYellow |
| 223 |
bgHiBlue |
| 224 |
bgHiMagenta |
| 225 |
bgHiCyan |
| 226 |
bgHiWhite |
| 227 |
``` |
| 228 |
|
| 229 |
## Generate a sequence of numbers |
| 230 |
|
| 231 |
```Go |
| 232 |
setOfDigits := spinner.GenerateNumberSequence(25) // Generate a 25 digit string of numbers |
| 233 |
s := spinner.New(setOfDigits, 100*time.Millisecond) |
| 234 |
``` |
| 235 |
|
| 236 |
## Get spinner status |
| 237 |
|
| 238 |
```Go |
| 239 |
fmt.Println(s.Active()) |
| 240 |
``` |
| 241 |
|
| 242 |
## Unix pipe and redirect |
| 243 |
|
| 244 |
Feature suggested and write up by [dekz](https://github.com/dekz) |
| 245 |
|
| 246 |
Setting the Spinner Writer to Stderr helps show progress to the user, with the enhancement to chain, pipe or redirect the output. |
| 247 |
|
| 248 |
This is the preferred method of setting a Writer at this time. |
| 249 |
|
| 250 |
```go |
| 251 |
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(os.Stderr)) |
| 252 |
s.Suffix = " Encrypting data..." |
| 253 |
s.Start() |
| 254 |
// Encrypt the data into ciphertext |
| 255 |
fmt.Println(os.Stdout, ciphertext) |
| 256 |
``` |
| 257 |
|
| 258 |
```sh |
| 259 |
> myprog encrypt "Secret text" > encrypted.txt |
| 260 |
⣯ Encrypting data... |
| 261 |
``` |
| 262 |
|
| 263 |
```sh |
| 264 |
> cat encrypted.txt |
| 265 |
1243hjkbas23i9ah27sj39jghv237n2oa93hg83 |
| 266 |
``` |
| 267 |
|
| 268 |
## Final String Output |
| 269 |
|
| 270 |
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. |
| 271 |
|
| 272 |
```Go |
| 273 |
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) |
| 274 |
s.FinalMSG = "Complete!\nNew line!\nAnother one!\n" |
| 275 |
s.Start() |
| 276 |
time.Sleep(4 * time.Second) |
| 277 |
s.Stop() |
| 278 |
``` |
| 279 |
|
| 280 |
Output |
| 281 |
```sh |
| 282 |
Complete! |
| 283 |
New line! |
| 284 |
Another one! |
| 285 |
``` |