A very simple terminal spinner
0

Configure Feed

Select the types of activity you want to include in your feed.

Add tests for double start stop (#3)

authored by

Tom Fleet and committed by
GitHub
(Apr 18, 2025, 10:20 AM +0100) e10d26b8 50c777bb

+56 -5
+2 -2
spin.go
··· 69 69 s.wg.Add(1) 70 70 go func() { 71 71 // Store the frames and the index locally so no need for synchronisation 72 - frames := [...]rune{'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'} 72 + frames := [...]string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} 73 73 current := 0 74 74 defer s.wg.Done() 75 75 for { ··· 77 77 case <-s.stop: 78 78 return 79 79 case <-time.Tick(frameRate): 80 - fmt.Fprintf(s.w, "%s%s %s...", erase, s.frameStyle.Text(string(frames[current])), s.messageStyle.Text(s.msg)) 80 + fmt.Fprintf(s.w, "%s%s %s...", erase, s.frameStyle.Text(frames[current]), s.messageStyle.Text(s.msg)) 81 81 current = (current + 1) % len(frames) 82 82 } 83 83 }
+54 -3
spin_test.go
··· 6 6 "testing" 7 7 "time" 8 8 9 + "github.com/FollowTheProcess/hue" 9 10 "github.com/FollowTheProcess/spin" 10 11 ) 12 + 13 + var frames = [...]string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} 11 14 12 15 func TestSpinner(t *testing.T) { 13 16 buf := &bytes.Buffer{} 14 17 15 - spinner := spin.New(buf, "Testing") 18 + spinner := spin.New(buf, "Testing", spin.FrameStyle(hue.Yellow), spin.MessageStyle(hue.Blue)) 16 19 spinner.Start() 17 20 time.Sleep(300 * time.Millisecond) 18 21 spinner.Stop() 19 22 20 23 got := buf.String() 21 24 22 - frames := [...]rune{'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'} 25 + found := false 26 + for _, frame := range frames { 27 + if strings.Contains(got, frame) { 28 + found = true 29 + break 30 + } 31 + } 32 + 33 + if !found { 34 + t.Fatalf("expected output to contain a spinner frame:\n\n%s\n", got) 35 + } 36 + } 37 + 38 + func TestDoubleStart(t *testing.T) { 39 + buf := &bytes.Buffer{} 40 + 41 + spinner := spin.New(buf, "Working") 42 + spinner.Start() 43 + spinner.Start() // This one shouldn't do anything 44 + 45 + time.Sleep(300 * time.Millisecond) 46 + spinner.Stop() 47 + 48 + got := buf.String() 23 49 24 50 found := false 25 51 for _, frame := range frames { 26 - if strings.Contains(got, string(frame)) { 52 + if strings.Contains(got, frame) { 53 + found = true 54 + break 55 + } 56 + } 57 + 58 + if !found { 59 + t.Fatalf("expected output to contain a spinner frame:\n\n%s\n", got) 60 + } 61 + } 62 + 63 + func TestDoubleStop(t *testing.T) { 64 + buf := &bytes.Buffer{} 65 + 66 + spinner := spin.New(buf, "Working") 67 + spinner.Start() 68 + 69 + time.Sleep(300 * time.Millisecond) 70 + spinner.Stop() 71 + spinner.Stop() // This one shouldn't do anything 72 + 73 + got := buf.String() 74 + 75 + found := false 76 + for _, frame := range frames { 77 + if strings.Contains(got, frame) { 27 78 found = true 28 79 break 29 80 }