Skip to content
This repository was archived by the owner on Mar 24, 2021. It is now read-only.

Fix race condition #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions cmd/spin_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,42 @@ func (s *SpinStatus) getDescription() string {
}
}

func (s *SpinStatus) print() {
fmt.Fprintf(os.Stderr, "\r %s %s %s", s.spin.Current(), s.info, s.getDescription())
func (s *SpinStatus) printStatus(status, end string) {
fmt.Fprintf(os.Stderr, "\033[2K\r %s %s %s%s", status, s.info, s.getDescription(), end)
}

func (s *SpinStatus) refresh() {
s.printStatus(s.spin.Current(), "")
}

func (s *SpinStatus) Start() {
s.ticker = time.NewTicker(100 * time.Millisecond)
go func() {
s.ticker = time.NewTicker(100 * time.Millisecond)
for range s.ticker.C {
s.spin.Next()
s.mu.Lock()
s.print()
s.refresh()
s.mu.Unlock()
}
}()
s.print()
s.refresh()
}

func (s *SpinStatus) Done() {
s.ticker.Stop()
fmt.Fprintf(os.Stderr, "\r \033[32m✓\033[0m %s %s\n", s.info, s.getDescription())
s.printStatus("\033[32m✓\033[0m", "\n")
}

func (s *SpinStatus) Update(delta int) {
s.mu.Lock()
defer s.mu.Unlock()
s.current += delta
s.print()
s.refresh()
}

func (s *SpinStatus) UpdateDescription(newDesc string) {
s.mu.Lock()
defer s.mu.Unlock()
s.description = newDesc
s.print()
s.refresh()
}