spinner.go 566 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package progress
  2. import "time"
  3. type spinner struct {
  4. time time.Time
  5. index int
  6. chars []string
  7. stop bool
  8. done string
  9. }
  10. func newSpinner() *spinner {
  11. return &spinner{
  12. index: 0,
  13. time: time.Now(),
  14. chars: []string{
  15. "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏",
  16. },
  17. done: "⠿",
  18. }
  19. }
  20. func (s *spinner) String() string {
  21. if s.stop {
  22. return s.done
  23. }
  24. d := time.Since(s.time)
  25. if d.Milliseconds() > 100 {
  26. s.index = (s.index + 1) % len(s.chars)
  27. }
  28. return s.chars[s.index]
  29. }
  30. func (s *spinner) Stop() {
  31. s.stop = true
  32. }