Documentation ¶
Overview ¶
Package spinner is Command line spinner library.
Have 80+ spinners, Segregated into ASCII(id: 1-999) and Unicode(id >= 1000) spinners and even custom (user-defined) spinners !!!
Support rich color rendering output for the spinners (in both 16bit colors and Hexcodes), universal API method, compatible with Windows system.
Made with concurrency handling in mind.
Source code and other details for the project are available at GitHub:
https://github.com/Yash-Handa/spinner
More usage please see README and tests.
Index ¶
- Constants
- Variables
- func Random16BitColor() string
- func RandomHexColor() string
- type Spinner
- func (s *Spinner) GetDoneText() string
- func (s *Spinner) GetID() uint
- func (s *Spinner) GetInterval() time.Duration
- func (s *Spinner) GetPostText() string
- func (s *Spinner) GetPreText() string
- func (s *Spinner) IsSpinning() bool
- func (s *Spinner) Restart()
- func (s *Spinner) SetColor(forground, background string)
- func (s *Spinner) SetDoneText(done string)
- func (s *Spinner) SetInterval(t time.Duration)
- func (s *Spinner) SetPostText(post string)
- func (s *Spinner) SetPreText(pre string)
- func (s *Spinner) Start()
- func (s *Spinner) Stop()
Constants ¶
const ( Red = "Red" Black = "Black" Green = "Green" Yellow = "Yellow" Blue = "Blue" Magenta = "Magenta" Cyan = "Cyan" White = "White" Normal = "Normal" // Normal represents No color )
Supported 16-bit colors, these are the most supported colors and will render on any version of cmd/ powershell. These can be used as both forground and background colors.
const HexBgNormal = "HexBgNormal"
HexBgNormal is a special color that sets background for HexColor forground to nothing. Basically a spinner with only Hex (256) code forground color
Variables ¶
var SpinnerMap = map[uint][]string{
1: {"|", "/", "-", "\\"},
2: {"[ ]", "[= ]", "[ = ]", "[ =]"},
3: {"[= ]", "[== ]", "[ == ]", "[ ==]", "[ =]"},
4: {"[ ]", "[# ]", "[## ]", "[### ]", "[####]", "[ ###]", "[ ##]", "[ #]"},
5: {" ", ". ", ".. ", "..."},
6: {" ", ". ", ".. ", "...", " ..", " ."},
7: {"[ ]", "[= ]", "[== ]", "[=== ]", "[====]"},
8: {"(-*--)", "(--*-)", "(---*)", "(--*-)", "(-*--)", "(*---)"},
9: {"( * )", "( * )", "( *)", "( * )", "( * )", "(* )"},
10: {"| ", "|| ", "||| ", "||||", " |||", " ||", " |"},
11: {">))'> ", " >))'> ", " >))'> ", " >))'> ", " >))'>", " <'((< ", " <'((< ", " <'((< "},
12: {"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"},
13: {".", "o", "O", "'", "O", "o", "."},
14: {"+", "x"},
15: {"v", "<", "^", ">"},
16: {">>---> ", " >>---> ", " >>---> ", " >>---> ", " >>--->", " <---<<", " <---<< ", " <---<< ", " <---<< ", "<---<< "},
17: {" . . . .", ". . . .", ". . . .", ". . . .", ". . . . ", ". . . . ."},
18: {" ....", ". ...", ".. ..", "... .", ".... ", "....."},
19: {" | ", " / ", " _ ", ` \ `, " | ", ` \ `, " _ ", " / "},
20: {"[ ]", "[ =]", "[ ==]", "[ ===]", "[====]", "[=== ]", "[== ]", "[= ]"},
21: {"[|\\__________]", "[_|\\_________]", "[__|\\________]", "[___|\\_______]", "[____|\\______]", "[_____|\\_____]", "[______|\\____]", "[_______|\\___]", "[________|\\__]", "[_________|\\_]", "[__________|\\]", "[__________/|]", "[_________/|_]", "[________/|__]", "[_______/|___]", "[______/|____]", "[_____/|_____]", "[____/|______]", "[___/|_______]", "[__/|________]", "[_/|_________]", "[/|__________]"},
22: {"d", "q", "p", "b"},
23: {".", "o", "O", "@", "*"},
1000: {"←", "↖", "↑", "↗", "→", "↘", "↓", "↙"},
1001: {"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃", "▁"},
1002: {"▉", "▊", "▋", "▌", "▍", "▎", "▏", "▎", "▍", "▌", "▋", "▊", "▉"},
1003: {"▖", "▘", "▝", "▗"},
1004: {"┤", "┘", "┴", "└", "├", "┌", "┬", "┐"},
1005: {"◢", "◣", "◤", "◥"},
1006: {"◰", "◳", "◲", "◱"},
1007: {"◴", "◷", "◶", "◵"},
1008: {"◐", "◓", "◑", "◒"},
1009: {"◡◡", "⊙⊙", "◠◠"},
1010: {"🌍", "🌎", "🌏"},
1011: {"◜ ", " ◝", " ◞", "◟ "},
1012: {"⇐", "⇖", "⇑", "⇗", "⇒", "⇘", "⇓", "⇙"},
1013: {"Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"},
1014: {"■", "□", "▪", "▫"},
1015: {"←", "↑", "→", "↓"},
1016: {"╫", "╪"},
1017: {"█▒▒▒▒▒▒▒▒▒", "███▒▒▒▒▒▒▒", "█████▒▒▒▒▒", "███████▒▒▒", "██████████"},
1018: {"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
1019: {"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"},
1020: {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
1021: {"⠁", "⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈", "⠈"},
1022: {"⠈", "⠉", "⠋", "⠓", "⠒", "⠐", "⠐", "⠒", "⠖", "⠦", "⠤", "⠠", "⠠", "⠤", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋", "⠉", "⠈"},
1023: {"⠁", "⠉", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠤", "⠄", "⠄", "⠤", "⠴", "⠲", "⠒", "⠂", "⠂", "⠒", "⠚", "⠙", "⠉", "⠁"},
1024: {"⠋", "⠙", "⠚", "⠒", "⠂", "⠂", "⠒", "⠲", "⠴", "⠦", "⠖", "⠒", "⠐", "⠐", "⠒", "⠓", "⠋"},
1025: {"⢹", "⢺", "⢼", "⣸", "⣇", "⡧", "⡗", "⡏"},
1026: {"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"},
1027: {"⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⡈", "⡉", "⡊", "⡋", "⡌", "⡍", "⡎", "⡏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝", "⠞", "⠟", "⡘", "⡙", "⡚", "⡛", "⡜", "⡝", "⡞", "⡟", "⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", "⠨", "⠩", "⠪", "⠫", "⠬", "⠭", "⠮", "⠯", "⡨", "⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", "⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷", "⠸", "⠹", "⠺", "⠻", "⠼", "⠽", "⠾", "⠿", "⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿", "⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆", "⢇", "⣀", "⣁", "⣂", "⣃", "⣄", "⣅", "⣆", "⣇", "⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", "⢐", "⢑", "⢒", "⢓", "⢔", "⢕", "⢖", "⢗", "⣐", "⣑", "⣒", "⣓", "⣔", "⣕", "⣖", "⣗", "⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", "⢠", "⢡", "⢢", "⢣", "⢤", "⢥", "⢦", "⢧", "⣠", "⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯", "⢰", "⢱", "⢲", "⢳", "⢴", "⢵", "⢶", "⢷", "⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾", "⣿"},
1028: {"⢀⠀", "⡀⠀", "⠄⠀", "⢂⠀", "⡂⠀", "⠅⠀", "⢃⠀", "⡃⠀", "⠍⠀", "⢋⠀", "⡋⠀", "⠍⠁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⢈⠩", "⡀⢙", "⠄⡙", "⢂⠩", "⡂⢘", "⠅⡘", "⢃⠨", "⡃⢐", "⠍⡐", "⢋⠠", "⡋⢀", "⠍⡁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⠈⠩", "⠀⢙", "⠀⡙", "⠀⠩", "⠀⢘", "⠀⡘", "⠀⠨", "⠀⢐", "⠀⡐", "⠀⠠", "⠀⢀", "⠀⡀"},
1029: {"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"},
1030: {"⎺", "⎻", "⎼", "⎽", "⎼", "⎻"},
1031: {"ヲ", "ァ", "ィ", "ゥ", "ェ", "ォ", "ャ", "ュ", "ョ", "ッ", "ア", "イ", "ウ", "エ", "オ", "カ", "キ", "ク", "ケ", "コ", "サ", "シ", "ス", "セ", "ソ", "タ", "チ", "ツ", "テ", "ト", "ナ", "ニ", "ヌ", "ネ", "ノ", "ハ", "ヒ", "フ", "ヘ", "ホ", "マ", "ミ", "ム", "メ", "モ", "ヤ", "ユ", "ヨ", "ラ", "リ", "ル", "レ", "ロ", "ワ", "ン"},
1032: {"(● )", "( ● )", "( ● )", "( ● )", "( ●)", "( ● )", "( ● )", "( ● )", "(● )"},
1033: {"✶", "✸", "✹", "✺", "✹", "✷"},
1034: {"¿", "?"},
1035: {"▹▹▹▹▹", "▸▹▹▹▹", "▹▸▹▹▹", "▹▹▸▹▹", "▹▹▹▸▹", "▹▹▹▹▸"},
1036: {"⬒", "⬔", "⬓", "⬕"},
1037: {".", "o", "O", "°", "O", "o", "."},
1038: {"▓", "▒", "░"},
1039: {"▌", "▀", "▐", "▄"},
1040: {"⊶", "⊷"},
1041: {"▪", "▫"},
1042: {"□", "■"},
1043: {"▮", "▯"},
1044: {"-", "=", "≡"},
1045: {"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"},
1046: {"🌑 ", "🌒 ", "🌓 ", "🌔 ", "🌕 ", "🌖 ", "🌗 ", "🌘 "},
1047: {"☗", "☖"},
1048: {"⧇", "⧆"},
1049: {"◉", "◎"},
1050: {"㊂", "㊀", "㊁"},
1051: {"⦾", "⦿"},
1052: {"▌ ", "▀ ", "▐▄"},
1053: {"▐|\\__________▌", "▐_|\\_________▌", "▐__|\\________▌", "▐___|\\_______▌", "▐____|\\______▌", "▐_____|\\_____▌", "▐______|\\____▌", "▐_______|\\___▌", "▐________|\\__▌", "▐_________|\\_▌", "▐__________|\\▌", "▐__________/|▌", "▐_________/|_▌", "▐________/|__▌", "▐_______/|___▌", "▐______/|____▌", "▐_____/|_____▌", "▐____/|______▌", "▐___/|_______▌", "▐__/|________▌", "▐_/|_________▌", "▐/|__________▌"},
1054: {"▐⠂ ▌", "▐⠈ ▌", "▐ ⠂ ▌", "▐ ⠠ ▌", "▐ ⡀ ▌", "▐ ⠠ ▌", "▐ ⠂ ▌", "▐ ⠈ ▌", "▐ ⠂ ▌", "▐ ⠠ ▌", "▐ ⡀ ▌", "▐ ⠠ ▌", "▐ ⠂ ▌", "▐ ⠈ ▌", "▐ ⠂▌", "▐ ⠠▌", "▐ ⡀▌", "▐ ⠠ ▌", "▐ ⠂ ▌", "▐ ⠈ ▌", "▐ ⠂ ▌", "▐ ⠠ ▌", "▐ ⡀ ▌", "▐ ⠠ ▌", "▐ ⠂ ▌", "▐ ⠈ ▌", "▐ ⠂ ▌", "▐ ⠠ ▌", "▐ ⡀ ▌", "▐⠠ ▌"},
1055: {"◇", "◈", "◆"},
1056: {"⬖", "⬘", "⬗", "⬙"},
1057: {"♠", "♣", "♥", "♦"},
1058: {"➞", "➟", "➠", "➡", "➠", "➟"},
}
SpinnerMap maps spinner IDs to spinner symbols. No real use-case other than inspiration and documentation
Functions ¶
func Random16BitColor ¶ added in v0.1.2
func Random16BitColor() string
Random16BitColor returns the string representation of a random 16 Bit color
func RandomHexColor ¶ added in v0.1.2
func RandomHexColor() string
RandomHexColor return the string representation of the hex code ("#rrggbb") of a a random color
Types ¶
type Spinner ¶
type Spinner struct {
// contains filtered or unexported fields
}
Spinner type represents the spinner itself. Note it does not export anything, Use getters and setters on this type to manipulate the spinner.
func Custom ¶ added in v1.1.0
func Custom(symbols []string, interval time.Duration, forground, background string) (*Spinner, error)
Custom creates a new spinner based on user defined symbols (a slice of strings).
Parameters:
symbols : it is slice of stings used to produce a spinner. All strings in the slice should be of same size or an error will be returned interval: it is of type time.Duration and sets the refresh rate of the spinner (the time frame after which spinner will be updated). If 0 is provided the default value of 100 * time.Millisecond will be forground: it could be a 16-Bit type (16-bit colors are defined as constants at the starting of the package like spinner.Red, etc) or Hex type (strings like "00e600", "#00E600", "ccc", etc). Empty string will use spinner.Normal background: it could be a 16-Bit type (16-bit colors are defined as constants at the starting of the package like spinner.Red, etc) or Hex type (strings like "00e600", "#00E600", "ccc", etc). Empty string will use spinner.Normal Note- forground and background should be of the same form either both will be 16-bit colors or both will be hex. Not a combination. In case of any error in resolving colors both forground and background will use spinner.Normal
Example:
package main import ( "time" "log" spinner "github.com/Yash-Handa/spinner" ) func main() { symbols := []string{"N ", "IN ", "PIN ", "SPIN", " SPI", " SP", " S", " "} sp, err := spinner.Custom(symbols, 0, spinner.Red, spinner.Normal) if err != nil { log.Fatal(err) } sp.SetPostText(" A custom spinner") sp.Start() // the spinner starts time.Sleep(3 * time.Second) // after 3 seconds the color changes to lime green sp.SetColor("00e600", spinner.HexBgNormal) // spinner.HexBgNormal is used with Hex forground to indicate that no background color to be used sp.SetPostText(" The color Changed !!") time.Sleep(3 * time.Second) sp.Stop() // the spinner stops }
all custom spinners have ID of 0
func New ¶
New creates a new spinner based on the ID provided.
Parameters:
id: it is a uint mapped a pre-defined spinners. If the id is invalid an error will occur id from 1 to 999 represent ASCII spinners (supported on all terminals) id >= 1000 represent Unicode point spinners (supported by terminals which are UTF-8 encoded) interval: it is of type time.Duration and sets the refresh rate of the spinner (the time frame after which spinner will be updated). If 0 is provided the default value of 100 * time.Millisecond will be forground: it could be a 16-Bit type (16-bit colors are defined as constants at the starting of the package like spinner.Red, etc) or Hex type (strings like "00e600", "#00E600", "ccc", etc). Empty string will use spinner.Normal background: it could be a 16-Bit type (16-bit colors are defined as constants at the starting of the package like spinner.Red, etc) or Hex type (strings like "00e600", "#00E600", "ccc", etc). Empty string will use spinner.Normal Note- forground and background should be of the same form either both will be 16-bit colors or both will be hex. Not a combination. In case of any error in resolving colors both forground and background will use spinner.Normal
Example:
package main import ( "time" "log" spinner "github.com/Yash-Handa/spinner" ) func main() { sp, err := spinner.New(4, 50 * time.Millisecond, spinner.Cyan, spinner.Normal) if err != nil { log.Fatal(err) } sp.SetPostText(" Loading Content") sp.SetDoneText("Hurray spinner worked\n") sp.Start() // the spinner starts time.Sleep(3 * time.Second) // after 3 seconds sp.SetColor(spinner.Magenta, "") // use previous background color sp.SetInterval(100 * time.Millisecond) sp.SetPostText(" The color and speed Changed !!") time.Sleep(3 * time.Second) sp.Stop() // the spinner stops }
func (*Spinner) GetDoneText ¶ added in v0.2.0
GetDoneText returns the current value of the text which will be printed after the spinner has been stopped, by calling stop() method on spinner variable
func (*Spinner) GetID ¶
GetID returns the ID of a spinner.
Note:
If a spinner is ASCII based its ID will be from 1 to 999. If a spinner is Unicode based then its ID will be >= 1000. For custom/ user-defined spinners the ID is 0.
func (*Spinner) GetInterval ¶
GetInterval returns the interval of the spinner
func (*Spinner) GetPostText ¶ added in v0.2.0
GetPostText returns the current value of the text which will be printed after the spinner
func (*Spinner) GetPreText ¶ added in v0.2.0
GetPreText returns the current value of the text which will be printed before the spinner
func (*Spinner) IsSpinning ¶
IsSpinning returns true if the spinner is active i.e., started() has been called but the corresponding stop() method hasn't
func (*Spinner) Restart ¶ added in v0.2.0
func (s *Spinner) Restart()
Restart stops the spinner (if not stopped already) and then restarts it.
func (*Spinner) SetColor ¶ added in v0.2.0
SetColor sets the forground and background of a spinner. It can be called any number of time to update the colors.
Default: The default value or zero values are forground = spinner.Normal and background = spinner.Normal. Incase of any erroneous value these defaults are used.
Note:
i. Both the forground and background should be either of 16-Bit type (Constants are defined at the starting of the package like spinner.Red, etc) or Hex type (like "00e600", "#00E600", "ccc", etc) but not a combination of both
ii. If an empty string is provided as the value of background then the priviously used background value is used
func (*Spinner) SetDoneText ¶ added in v0.2.0
SetDoneText takes a string that will be printed after the spinners has stopped spinning.
func (*Spinner) SetInterval ¶ added in v0.2.0
SetInterval takes a parameter of time.Duration type and sets the interval / refersh rate of the spinner to it. It can be called any number of time to update the interval value.
the spinner symbol, pre-text and post-text are all refreshed at each interval duraton
Default: The default value or zero value is 100 * time.Millisecond
func (*Spinner) SetPostText ¶ added in v0.2.0
SetPostText takes a string that will be printed after the spinner.
SetPostText can be called multiple time during the life-time of the spinner to change the string value (default is an empty string). The spinner will check and print the value of the value of this pre-text at each interval duration (refresh time of the spinner)
Note: any leading or trialing newlines ('\n', '\r\n') will be trimmed
func (*Spinner) SetPreText ¶ added in v0.2.0
SetPreText takes a string that will be printed before the spinner.
SetPreText can be called multiple time during the life-time of the spinner to change the string value (default is an empty string). The spinner will check and print the value of the value of this pre-text at each interval duration (refresh time of the spinner)
Note: any leading or trialing newlines ('\n', '\r\n') will be trimmed