├── LICENSE ├── Readme.md ├── example └── spin.go ├── go-spin.gif └── spin.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2021 TJ Holowaychuk tj@tjholowaychuk.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # go-spin 3 | 4 | Little terminal spinner lib. 5 | 6 | View the [docs](http://godoc.org/github.com/tj/go-spin). 7 | 8 | ## Installation 9 | 10 | ``` 11 | $ go get github.com/tj/go-spin 12 | ``` 13 | 14 | ## Example 15 | 16 | ```go 17 | s := spin.New() 18 | for i := 0; i < 30; i++ { 19 | fmt.Printf("\r \033[36mcomputing\033[m %s ", s.Next()) 20 | time.Sleep(100 * time.Millisecond) 21 | } 22 | ``` 23 | ## GIF FTW 24 | ![](./go-spin.gif) 25 | 26 | # License 27 | 28 | MIT -------------------------------------------------------------------------------- /example/spin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | 7 | "github.com/tj/go-spin" 8 | ) 9 | 10 | func main() { 11 | s := spin.New() 12 | 13 | show(s, "Default", spin.Default) 14 | 15 | show(s, "Box1", spin.Box1) 16 | show(s, "Box2", spin.Box2) 17 | show(s, "Box3", spin.Box3) 18 | show(s, "Box4", spin.Box4) 19 | show(s, "Box5", spin.Box5) 20 | show(s, "Box6", spin.Box6) 21 | show(s, "Box7", spin.Box7) 22 | 23 | show(s, "Spin1", spin.Spin1) 24 | show(s, "Spin2", spin.Spin2) 25 | show(s, "Spin3", spin.Spin3) 26 | show(s, "Spin4", spin.Spin4) 27 | show(s, "Spin5", spin.Spin5) 28 | show(s, "Spin6", spin.Spin6) 29 | show(s, "Spin7", spin.Spin7) 30 | show(s, "Spin8", spin.Spin8) 31 | show(s, "Spin9", spin.Spin9) 32 | } 33 | 34 | func show(s *spin.Spinner, name, frames string) { 35 | s.Set(frames) 36 | fmt.Printf("\n\n %s: %s\n\n", name, frames) 37 | for i := 0; i < 30; i++ { 38 | fmt.Printf("\r \033[36mcomputing\033[m %s ", s.Next()) 39 | time.Sleep(100 * time.Millisecond) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /go-spin.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/go-spin/721f604c567004b82bb196abe227ab60360184ba/go-spin.gif -------------------------------------------------------------------------------- /spin.go: -------------------------------------------------------------------------------- 1 | package spin 2 | 3 | import "sync" 4 | 5 | // Spinner types. 6 | var ( 7 | Box1 = `⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏` 8 | Box2 = `⠋⠙⠚⠞⠖⠦⠴⠲⠳⠓` 9 | Box3 = `⠄⠆⠇⠋⠙⠸⠰⠠⠰⠸⠙⠋⠇⠆` 10 | Box4 = `⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋` 11 | Box5 = `⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁` 12 | Box6 = `⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈` 13 | Box7 = `⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈` 14 | Spin1 = `|/-\` 15 | Spin2 = `◴◷◶◵` 16 | Spin3 = `◰◳◲◱` 17 | Spin4 = `◐◓◑◒` 18 | Spin5 = `▉▊▋▌▍▎▏▎▍▌▋▊▉` 19 | Spin6 = `▌▄▐▀` 20 | Spin7 = `╫╪` 21 | Spin8 = `■□▪▫` 22 | Spin9 = `←↑→↓` 23 | Default = Box1 24 | ) 25 | 26 | // Spinner is exactly what you think it is. 27 | type Spinner struct { 28 | mu sync.Mutex 29 | frames []rune 30 | length int 31 | pos int 32 | } 33 | 34 | // New returns a spinner initialized with Default frames. 35 | func New() *Spinner { 36 | s := &Spinner{} 37 | s.Set(Default) 38 | return s 39 | } 40 | 41 | // Set frames to the given string which must not use spaces. 42 | func (s *Spinner) Set(frames string) { 43 | s.mu.Lock() 44 | defer s.mu.Unlock() 45 | s.frames = []rune(frames) 46 | s.length = len(s.frames) 47 | } 48 | 49 | // Current returns the current rune in the sequence. 50 | func (s *Spinner) Current() string { 51 | s.mu.Lock() 52 | defer s.mu.Unlock() 53 | r := s.frames[s.pos%s.length] 54 | return string(r) 55 | } 56 | 57 | // Next returns the next rune in the sequence. 58 | func (s *Spinner) Next() string { 59 | s.mu.Lock() 60 | defer s.mu.Unlock() 61 | r := s.frames[s.pos%s.length] 62 | s.pos++ 63 | return string(r) 64 | } 65 | 66 | // Reset the spinner to its initial frame. 67 | func (s *Spinner) Reset() { 68 | s.mu.Lock() 69 | defer s.mu.Unlock() 70 | s.pos = 0 71 | } 72 | --------------------------------------------------------------------------------