File: README.md

package info (click to toggle)
golang-github-briandowns-spinner 1.18.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 548 kB
  • sloc: makefile: 17
file content (285 lines) | stat: -rw-r--r-- 20,550 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Spinner

[![GoDoc](https://godoc.org/github.com/briandowns/spinner?status.svg)](https://godoc.org/github.com/briandowns/spinner) [![CircleCI](https://circleci.com/gh/briandowns/spinner.svg?style=svg)](https://circleci.com/gh/briandowns/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

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

```Go
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

```Go
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

```Go
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

```Go
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)

```Go
someSet := []string{"+", "-"}
s := spinner.New(someSet, 100*time.Millisecond)
```

## Prefix or append text to the spinner

```Go
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.

```Go
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`.

```Go
s.Color("red", "bold") // Set the spinner color to a bold red
```

To set the background to black, the foreground to a bold red:

```Go
s.Color("bgBlack", "bold", "fgRed")
```

Below is the full color and attribute list:

```Go
// 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

```Go
setOfDigits := spinner.GenerateNumberSequence(25)    // Generate a 25 digit string of numbers
s := spinner.New(setOfDigits, 100*time.Millisecond)
```

## Get spinner status

```Go
fmt.Println(s.Active())
```

## Unix pipe and redirect

Feature suggested and write up by [dekz](https://github.com/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.

```go
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)
```

```sh
> myprog encrypt "Secret text" > encrypted.txt
⣯ Encrypting data...
```

```sh
> 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.

```Go
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
```sh
Complete!
New line!
Another one!
```