From ed330e574d14c5cc761c6790e260fae584bd992e Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 14 Sep 2023 15:57:46 +0300 Subject: [PATCH] [spinner] Add initial spinner animation --- CHANGELOG.md | 3 ++- spinner/spinner.go | 42 ++++++++++++++++++++++++----------------- spinner/spinner_test.go | 2 +- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd96341..a181bf4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/spinner/spinner.go b/spinner/spinner.go index e6a22b77..3ad22ce1 100644 --- a/spinner/spinner.go +++ b/spinner/spinner.go @@ -58,9 +58,11 @@ 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, @@ -68,7 +70,7 @@ var framesDelay = []time.Duration{ 75 * time.Millisecond, 75 * time.Millisecond, 75 * time.Millisecond, - 75 * time.Millisecond, + 95 * time.Millisecond, } var desc string @@ -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 } } } diff --git a/spinner/spinner_test.go b/spinner/spinner_test.go index 903f9ba4..5ca0b35a 100644 --- a/spinner/spinner_test.go +++ b/spinner/spinner_test.go @@ -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)