github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/internal/ui/components/spinner.go (about)

     1  package components
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  )
     7  
     8  // TODO: move me to a common module (used in multiple repos)
     9  
    10  const (
    11  	SpinnerDotSet = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
    12  )
    13  
    14  type Spinner struct {
    15  	index   int
    16  	charset []string
    17  	lock    sync.Mutex
    18  }
    19  
    20  func NewSpinner(charset string) Spinner {
    21  	return Spinner{
    22  		charset: strings.Split(charset, ""),
    23  	}
    24  }
    25  
    26  func (s *Spinner) Current() string {
    27  	s.lock.Lock()
    28  	defer s.lock.Unlock()
    29  
    30  	return s.charset[s.index]
    31  }
    32  
    33  func (s *Spinner) Next() string {
    34  	s.lock.Lock()
    35  	defer s.lock.Unlock()
    36  	c := s.charset[s.index]
    37  	s.index++
    38  	if s.index >= len(s.charset) {
    39  		s.index = 0
    40  	}
    41  	return c
    42  }