Skip to content

Commit

Permalink
Merge pull request #3 from adhocore/2-tput-fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore authored Oct 28, 2022
2 parents 55b3078 + 609584a commit 43f4d9a
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions chin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

var chars = []string{"+", "\\", "|", "!", "/", "-", "x", "+", "\\", "|", "!", "/", "-", "x"}
var chars = []string{"+", "\\", "|", "!", "/", "-", "x"}

// Chin is the spinner struct
type Chin struct {
Expand All @@ -31,7 +31,9 @@ func (s *Chin) WithWait(wg *sync.WaitGroup) *Chin {

// Start starts the spinner
func (s *Chin) Start() {
tput("civis")
if nil != tput("civis") {
fmt.Print("\033[?25l")
}
s.doSpin()
}

Expand All @@ -41,11 +43,13 @@ func (s *Chin) Stop() {
defer s.wg.Done()
}
s.stop = true
tput("cvvis")
if nil != tput("cvvis") {
fmt.Print("\033[?25h")
}
}

func (s *Chin) doSpin() {
for !s.stop {
for {
outer:
select {
case _, ok := <-s.stch:
Expand All @@ -57,9 +61,10 @@ func (s *Chin) doSpin() {
for _, c := range chars {
if s.stop {
s.stch <- true
} else {
fmt.Print(c, "\010")
time.Sleep(50 * time.Millisecond)
}
fmt.Print(c, "\010")
time.Sleep(50 * time.Millisecond)
}
}
}
Expand Down

0 comments on commit 43f4d9a

Please sign in to comment.