Skip to content

Commit

Permalink
[spinner] Add initial spinner animation
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Sep 14, 2023
1 parent 1484c20 commit ed330e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

### 12.76.0

* `[knf]` Add modificator support for `GetD`
* `[knf]` Added modificator support for `GetD`
* `[spinner]` Added initial spinner animation

### 12.75.1

Expand Down
42 changes: 25 additions & 17 deletions spinner/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ var DisableAnimation = false

// ////////////////////////////////////////////////////////////////////////////////// //

var spinnerFrames = []string{"", "⠴", "⠤", "⠦", "⠇", "⠋", "⠉", "⠙"}
var spinnerFrames = []string{"", "⠲", "⠴", "⠤", "⠦", "⠇", "⠋", "⠉", "⠙", "⠸"}

var framesDelay = []time.Duration{
105 * time.Millisecond,
95 * time.Millisecond,
75 * time.Millisecond,
55 * time.Millisecond,
35 * time.Millisecond,
55 * time.Millisecond,
75 * time.Millisecond,
75 * time.Millisecond,
75 * time.Millisecond,
75 * time.Millisecond,
95 * time.Millisecond,
}

var desc string
Expand Down Expand Up @@ -138,22 +140,28 @@ func Skip() {
// ////////////////////////////////////////////////////////////////////////////////// //

func showSpinner() {
var i int

for {
for i, frame := range spinnerFrames {
mu.RLock()
fmtc.Printf(
SpinnerColorTag+"%s {!}"+desc+"… "+TimeColorTag+"[%s]{!}",
frame, timeutil.ShortDuration(time.Since(start)),
)
mu.RUnlock()

time.Sleep(framesDelay[i])
fmt.Print("\033[2K\r")

if !isActive.Load() {
isHidden.Store(true)
return
}
mu.RLock()
fmtc.Printf(
SpinnerColorTag+"%s {!}"+desc+"… "+TimeColorTag+"[%s]{!}",
spinnerFrames[i], timeutil.ShortDuration(time.Since(start)),
)
mu.RUnlock()

i++

if i == 10 {
i = 2
}

time.Sleep(framesDelay[i])
fmt.Print("\033[2K\r")

if !isActive.Load() {
isHidden.Store(true)
return
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion spinner/spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *SpinnerSuite) TestSpinner(c *C) {
Done(true) // skipped
Show("ABCD")
Show("ABCD") // skipped
time.Sleep(time.Millisecond * 100)
time.Sleep(time.Millisecond * 800)
Update("ABCD")
time.Sleep(time.Millisecond * 100)
Done(true)
Expand Down

0 comments on commit ed330e5

Please sign in to comment.