File tree Expand file tree Collapse file tree 2 files changed +13
-8
lines changed
Expand file tree Collapse file tree 2 files changed +13
-8
lines changed Original file line number Diff line number Diff line change 77a terminal spinner for golang
88> Author:Alan Chen
99
10- > Date: 2020/4/20
10+ > Date: 2020/4/22
1111
1212## features
13131 . 使用goroutine来运行spinner。
Original file line number Diff line number Diff line change @@ -2,10 +2,11 @@ package spin
22
33import (
44 "fmt"
5+ "os"
56 "time"
67)
78
8- const threadsNum uint = 1
9+ const lockThreadsNum uint = 1
910
1011// spin character
1112type character struct {
@@ -15,10 +16,10 @@ type character struct {
1516
1617// spin content
1718type Spin struct {
18- shouldWork bool
19- threadsNum uint
20- character character
21- ch chan bool
19+ shouldWork bool
20+ lockThreadsNum uint
21+ character character
22+ ch chan bool
2223}
2324
2425func (s * Spin ) run () {
@@ -43,10 +44,14 @@ func (s *Spin) waitForRun() {
4344
4445// start spinning
4546func (s * Spin ) Start () {
46- if s .threadsNum < = 1 {
47+ if s .lockThreadsNum = = 1 {
4748 go s .waitForRun ()
4849 go s .run ()
50+ } else {
51+ fmt .Println ("current spin locks only one goroutine, so don't call start method multiple times" )
52+ os .Exit (1 )
4953 }
54+ s .lockThreadsNum ++
5055}
5156
5257// stop spinning
@@ -83,7 +88,7 @@ func (s *Spin) Fail(text string) {
8388func New (spinType character ) * Spin {
8489 s := new (Spin )
8590 s .shouldWork = true
86- s .threadsNum = threadsNum
91+ s .lockThreadsNum = lockThreadsNum
8792 s .character = spinType
8893 s .ch = make (chan bool )
8994 return s
You can’t perform that action at this time.
0 commit comments