Documentation
¶
Overview ¶
Package yacspin provides Yet Another CLi Spinner for Go, taking inspiration (and some utility code) from the https://github.com/briandowns/spinner project. Specifically this project borrows the default character sets, and color mappings to github.com/fatih/color colors, from that project.
This spinner should support all major operating systems, and is tested against Linux, MacOS, and Windows.
This spinner also supports an alternate mode of operation when the TERM environment variable is set to "dumb". This is discovered automatically when constructing the spinner.
Within the yacspin package there are some default spinners stored in the yacspin.CharSets variable, and you can also provide your own. There is also a list of known colors in the yacspin.ValidColors variable, if you'd like to see what's supported. If you've used github.com/fatih/color before, they should look familiar.
cfg := yacspin.Config{ Frequency: 100 * time.Millisecond, CharSet: yacspin.CharSets[59], Suffix: " backing up database to S3", Message: "exporting data", StopCharacter: "✓", StopColors: []string{"fgGreen"}, } spinner, err := yacspin.New(cfg) // handle the error spinner.Start() // doing some work time.Sleep(2 * time.Second) spinner.Message("uploading data") // upload... time.Sleep(2 * time.Second) spinner.Stop()
Check out the Config struct to see all of the possible configuration options supported by the Spinner.
Index ¶
- Variables
- type Config
- type Spinner
- func (s *Spinner) CharSet(cs []string) error
- func (s *Spinner) Colors(colors ...string) error
- func (s *Spinner) Frequency(d time.Duration) error
- func (s *Spinner) Message(message string)
- func (s *Spinner) Pause() error
- func (s *Spinner) Prefix(prefix string)
- func (s *Spinner) Reverse()
- func (s *Spinner) Start() error
- func (s *Spinner) Status() SpinnerStatus
- func (s *Spinner) Stop() error
- func (s *Spinner) StopCharacter(char string)
- func (s *Spinner) StopColors(colors ...string) error
- func (s *Spinner) StopFail() error
- func (s *Spinner) StopFailCharacter(char string)
- func (s *Spinner) StopFailColors(colors ...string) error
- func (s *Spinner) StopFailMessage(message string)
- func (s *Spinner) StopMessage(message string)
- func (s *Spinner) Suffix(suffix string)
- func (s *Spinner) Unpause() error
- type SpinnerStatus
Constants ¶
This section is empty.
Variables ¶
var CharSets = map[int][]string{
0: {"←", "↖", "↑", "↗", "→", "↘", "↓", "↙"},
1: {"▁", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃", "▁"},
2: {"▖", "▘", "▝", "▗"},
3: {"┤", "┘", "┴", "└", "├", "┌", "┬", "┐"},
4: {"◢", "◣", "◤", "◥"},
5: {"◰", "◳", "◲", "◱"},
6: {"◴", "◷", "◶", "◵"},
7: {"◐", "◓", "◑", "◒"},
8: {".", "o", "O", "@", "*"},
9: {"|", "/", "-", "\\"},
10: {"◡◡", "⊙⊙", "◠◠"},
11: {"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
12: {">))'>", " >))'>", " >))'>", " >))'>", " >))'>", " <'((<", " <'((<", " <'((<"},
13: {"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"},
14: {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
15: {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"},
16: {"▉", "▊", "▋", "▌", "▍", "▎", "▏", "▎", "▍", "▌", "▋", "▊", "▉"},
17: {"■", "□", "▪", "▫"},
18: {"←", "↑", "→", "↓"},
19: {"╫", "╪"},
20: {"⇐", "⇖", "⇑", "⇗", "⇒", "⇘", "⇓", "⇙"},
21: {"⠁", "⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈", "⠈"},
22: {"⠈", "⠉", "⠋", "⠓", "⠒", "⠐", "⠐", "⠒", "⠖", "⠦", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈"},
23: {"⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠴", "⠲", "⠒", "⠂", "⠂", "⠒", "⠚", "⠙", "⠉", "⠁"},
24: {"⠋", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋"},
25: {"ヲ", "ァ", "ィ", "ゥ", "ェ", "ォ", "ャ", "ュ", "ョ", "ッ", "ア", "イ", "ウ", "エ", "オ", "カ", "キ", "ク", "ケ", "コ", "サ", "シ", "ス", "セ", "ソ", "タ", "チ", "ツ", "テ", "ト", "ナ", "ニ", "ヌ", "ネ", "ノ", "ハ", "ヒ", "フ", "ヘ", "ホ", "マ", "ミ", "ム", "メ", "モ", "ヤ", "ユ", "ヨ", "ラ", "リ", "ル", "レ", "ロ", "ワ", "ン"},
26: {".", "..", "..."},
27: {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "▉", "▊", "▋", "▌", "▍", "▎", "▏", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█", "▇", "▆", "▅", "▄", "▃", "▂", "▁"},
28: {".", "o", "O", "°", "O", "o", "."},
29: {"+", "x"},
30: {"v", "<", "^", ">"},
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: {".", "o", "O", "°", "O", "o", "."},
61: {"▓", "▒", "░"},
62: {"▌", "▀", "▐", "▄"},
63: {"⊶", "⊷"},
64: {"▪", "▫"},
65: {"□", "■"},
66: {"▮", "▯"},
67: {"-", "=", "≡"},
68: {"d", "q", "p", "b"},
69: {"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"},
70: {"🌑 ", "🌒 ", "🌓 ", "🌔 ", "🌕 ", "🌖 ", "🌗 ", "🌘 "},
71: {"☗", "☖"},
72: {"⧇", "⧆"},
73: {"◉", "◎"},
74: {"㊂", "㊀", "㊁"},
75: {"⦾", "⦿"},
76: {"ဝ", "၀"},
77: {"▌", "▀", "▐▄"},
78: {"⠈⠁", "⠈⠑", "⠈⠱", "⠈⡱", "⢀⡱", "⢄⡱", "⢄⡱", "⢆⡱", "⢎⡱", "⢎⡰", "⢎⡠", "⢎⡀", "⢎⠁", "⠎⠁", "⠊⠁"},
79: {"________", "-_______", "_-______", "__-_____", "___-____", "____-___", "_____-__", "______-_", "_______-", "________", "_______-", "______-_", "_____-__", "____-___", "___-____", "__-_____", "_-______", "-_______", "________"},
80: {"|_______", "_/______", "__-_____", "___\\____", "____|___", "_____/__", "______-_", "_______\\", "_______|", "______\\_", "_____-__", "____/___", "___|____", "__\\_____", "_-______"},
}
CharSets contains the default character sets from https://github.com/briandowns/spinner.
var ValidColors = map[string]struct{}{
"black": {},
"red": {},
"green": {},
"yellow": {},
"blue": {},
"magenta": {},
"cyan": {},
"white": {},
"reset": {},
"bold": {},
"faint": {},
"italic": {},
"underline": {},
"blinkslow": {},
"blinkrapid": {},
"reversevideo": {},
"concealed": {},
"crossedout": {},
"fgBlack": {},
"fgRed": {},
"fgGreen": {},
"fgYellow": {},
"fgBlue": {},
"fgMagenta": {},
"fgCyan": {},
"fgWhite": {},
"fgHiBlack": {},
"fgHiRed": {},
"fgHiGreen": {},
"fgHiYellow": {},
"fgHiBlue": {},
"fgHiMagenta": {},
"fgHiCyan": {},
"fgHiWhite": {},
"bgBlack": {},
"bgRed": {},
"bgGreen": {},
"bgYellow": {},
"bgBlue": {},
"bgMagenta": {},
"bgCyan": {},
"bgWhite": {},
"bgHiBlack": {},
"bgHiRed": {},
"bgHiGreen": {},
"bgHiYellow": {},
"bgHiBlue": {},
"bgHiMagenta": {},
"bgHiCyan": {},
"bgHiWhite": {},
}
ValidColors holds the list of the strings that are mapped to github.com/fatih/color color attributes. Any of these colors / attributes can be used with the *Spinner type, and it should be reflected in the output.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Frequency specifies how often to animate the spinner. Optimal value // depends on the character set you use. // // Note: This is a required value (cannot be 0). Frequency time.Duration // Writer is the place where we are outputting the spinner, and can't be // changed after the *Spinner has been constructed. If omitted (nil), this // defaults to os.Stdout. Writer io.Writer // HideCursor describes whether the cursor should be hidden by the spinner // while animating. If it is hidden, it will be restored when the spinner // stops. This can't be changed after the *Spinner has been constructed. // // Please note, if the program crashes or is killed you may need to reset // your terminal for the cursor to appear again. HideCursor bool // SpinnerAtEnd configures the spinner to render the animation at the end of // the line instead of the beginning. The default behavior is to render the // animated spinner at the beginning of the line. SpinnerAtEnd bool // ColorAll describes whether to color everything (all) or just the spinner // character(s). This cannot be changed after the *Spinner has been // constructed. ColorAll bool // Colors are the colors used for the different printed messages. This // respects the ColorAll field. Colors []string // CharSet is the list of characters to iterate through to draw the spinner. CharSet []string // Prefix is the string printed immediately before the spinner. // // If SpinnerAtEnd is set to true, it's recommended that this string start // with a space character (` `). Prefix string // Suffix is the string printed immediately after the spinner and before the // message. // // If SpinnerAtEnd is set to false, it's recommended that this string starts // with an space character (` `). Suffix string // SuffixAutoColon configures whether the spinner adds a colon after the // suffix automatically. If there is a message, a colon followed by a space // is added to the suffix. Otherwise, if there is no message the colon is // omitted. // // If SpinnerAtEnd is set to true, this option is ignored. SuffixAutoColon bool // Message is the message string printed by the spinner. If SpinnerAtEnd is // set to false and SuffixAutoColon is set to true, the printed line will // look like: // // <prefix><spinner><suffix>: <message> // // If SpinnerAtEnd is set to true, the printed line will instead look like // this: // // <message><prefix><spinner><suffix> // // In this case, it may be preferred to set the Prefix to empty space (` `). Message string // StopMessage is the message used when Stop() is called. StopMessage string // StopCharacter is spinner character used when Stop() is called. // Recommended character is ✓, and can be more than just one character. StopCharacter string // StopColors are the colors used for the Stop() printed line. This respects // the ColorAll field. StopColors []string // StopFailMessage is the message used when StopFail() is called. StopFailMessage string // StopFailCharacter is the spinner character used when StopFail() is // called. Recommended character is ✗, and can be more than just one // character. StopFailCharacter string // StopFailColors are the colors used for the StopFail() printed line. This // respects the ColorAll field. StopFailColors []string }
Config is the configuration structure for the Spinner.
type Spinner ¶
type Spinner struct {
// contains filtered or unexported fields
}
Spinner is a type representing an animated CLi terminal spinner. It's configured via the Config struct type, and controlled via its methods. Some of its configuration can also be updated via methods.
Note: You need to use New() to construct a *Spinner.
func (*Spinner) CharSet ¶
CharSet updates the set of characters (strings) to use for the spinner. You can provide your own, or use one from the yacspin.CharSets variable.
The character sets available in the CharSets variable are from the https://github.com/briandowns/spinner project.
func (*Spinner) Colors ¶
Colors updates the github.com/fatih/colors for printing the spinner line. ColorAll config parameter controls whether only the spinner character is printed with these colors, or the whole line.
StopColors() is the method to control the colors in the stop message.
func (*Spinner) Frequency ¶ added in v0.8.0
Frequency updates the frequency of the spinner being animated.
func (*Spinner) Pause ¶ added in v0.8.0
Pause puts the spinner in a state where it no longer animates or renders updates to data. This function blocks until the spinner's internal painting goroutine enters a paused state.
If you want to make a few configuration changes and have them to appear at the same time, like changing the suffix, message, and color, you can Pause() the spinner first and then Unpause() after making the changes.
If the spinner is not running (stopped, paused, or in transition to another state) this returns an error.
func (*Spinner) Reverse ¶
func (s *Spinner) Reverse()
Reverse flips the character set order of the spinner characters.
func (*Spinner) Start ¶
Start begins the spinner on the Writer in the Config provided to New(). Only possible error is if the spinner is already runninng.
func (*Spinner) Status ¶ added in v0.8.0
func (s *Spinner) Status() SpinnerStatus
Status returns the current status of the spinner. The returned value is of type SpinnerStatus, which can be compared against the exported Spinner* package-level constants (e.g., SpinnerRunning).
func (*Spinner) Stop ¶
Stop disables the spinner, and prints the StopCharacter with the StopMessage using the StopColors. This blocks until the stopped message is printed. Only possible error is if the spinner is not running.
func (*Spinner) StopCharacter ¶
StopCharacter sets the single "character" to use for the spinner when stopping. Recommended character is ✓.
func (*Spinner) StopColors ¶
StopColors updates the colors used for the stop message. See Colors() method documentation for more context.
StopFailColors() is the method to control the colors in the failed stop message.
func (*Spinner) StopFail ¶ added in v0.2.0
StopFail disables the spinner, and prints the StopFailCharacter with the StopFailMessage using the StopFailColors. This blocks until the stopped message is printed. Only possible error is if the spinner is not running.
func (*Spinner) StopFailCharacter ¶ added in v0.2.0
StopFailCharacter sets the single "character" to use for the spinner when stopping for a failure. Recommended character is ✗.
func (*Spinner) StopFailColors ¶ added in v0.2.0
StopFailColors updates the colors used for the StopFail message. See Colors() method documentation for more context.
func (*Spinner) StopFailMessage ¶ added in v0.2.0
StopFailMessage updates the Message used when StopFail() is called.
func (*Spinner) StopMessage ¶
StopMessage updates the Message used when Stop() is called.
func (*Spinner) Suffix ¶
Suffix updates the Suffix printed after the spinner character and before the message. It's recommended that this start with an empty space.
func (*Spinner) Unpause ¶ added in v0.8.0
Unpause returns the spinner back to a running state after pausing. See Pause() documentation for more detail. This function blocks until the spinner's internal painting goroutine acknowledges the request to unpause.
If the spinner is not paused this returns an error.
type SpinnerStatus ¶ added in v0.8.0
type SpinnerStatus uint32
SpinnerStatus describes the status of the spinner. See the package constants for the list of all possible statuses
const ( // SpinnerStopped is a stopped spinner SpinnerStopped SpinnerStatus = iota // SpinnerStarting is a starting spinner SpinnerStarting // SpinnerRunning is a running spinner SpinnerRunning // SpinnerStopping is a stopping spinner SpinnerStopping // SpinnerPausing is a pausing spinner SpinnerPausing // SpinnerPaused is a paused spinner SpinnerPaused // SpinnerUnpausing is an unpausing spinner SpinnerUnpausing )
func (SpinnerStatus) String ¶ added in v0.8.0
func (s SpinnerStatus) String() string