spinners.go 603 B

1234567891011121314151617181920212223242526272829303132
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package tui
  3. import (
  4. "fmt"
  5. "time"
  6. )
  7. var _ = fmt.Print
  8. type Spinner struct {
  9. Name string
  10. interval time.Duration
  11. frames []string
  12. current_frame int
  13. last_change_at time.Time
  14. }
  15. func (self Spinner) Interval() time.Duration {
  16. return self.interval
  17. }
  18. func (self *Spinner) Tick() string {
  19. now := time.Now()
  20. if now.Sub(self.last_change_at) >= self.interval {
  21. self.last_change_at = now
  22. self.current_frame = (self.current_frame + 1) % len(self.frames)
  23. }
  24. return self.frames[self.current_frame]
  25. }