syft/internal/ui/common/spinner.go
Alex Goodman c5390264b0 split UI from event handling
Signed-off-by: Alex Goodman <wagoodman@gmail.com>
2021-06-26 23:32:28 -04:00

43 lines
625 B
Go

package common
import (
"strings"
"sync"
)
// TODO: move me to a common module (used in multiple repos)
const (
SpinnerDotSet = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
)
type Spinner struct {
index int
charset []string
lock sync.Mutex
}
func NewSpinner(charset string) Spinner {
return Spinner{
charset: strings.Split(charset, ""),
}
}
func (s *Spinner) Current() string {
s.lock.Lock()
defer s.lock.Unlock()
return s.charset[s.index]
}
func (s *Spinner) Next() string {
s.lock.Lock()
defer s.lock.Unlock()
c := s.charset[s.index]
s.index++
if s.index >= len(s.charset) {
s.index = 0
}
return c
}