1
0

spinner.go 685 B

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