Skip to content

Commit

Permalink
Merge pull request #42 from chelnak/format_update_message
Browse files Browse the repository at this point in the history
Add UpdateMessagef() method
  • Loading branch information
chelnak authored Sep 1, 2022
2 parents 1b611e5 + 5631124 commit 242aa18
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions examples/advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ func main() {
}

func downloader(wg *sync.WaitGroup, s *ysmrr.Spinner) {
s.UpdateMessage("Downloading...")
time.Sleep(2 * time.Second)
files := []string{"file1", "file2", "file3", "file4", "file5"}
for _, file := range files {
s.UpdateMessagef("Downloading %s...", file)
time.Sleep(1 * time.Second)
}

s.UpdateMessage("Downloading complete...")
s.Complete()
}

Expand Down
5 changes: 5 additions & 0 deletions spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (s *Spinner) UpdateMessage(message string) {
s.notifyHasUpdate()
}

// UpdateMessagef updates the spinner message with a formatted string.
func (s *Spinner) UpdateMessagef(format string, a ...interface{}) {
s.UpdateMessage(fmt.Sprintf(format, a...))
}

// IsComplete returns true if the spinner is complete.
func (s *Spinner) IsComplete() bool {
s.mutex.Lock()
Expand Down
8 changes: 8 additions & 0 deletions spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func TestSpinnerUpdateMessage(t *testing.T) {
assert.Equal(t, updatedMessage, spinner.GetMessage())
}

func TestSpinnerUpdateMessagef(t *testing.T) {
expectedMessage := "updated message test"
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
spinner.UpdateMessagef("updated message %s", "test")
assert.Equal(t, expectedMessage, spinner.GetMessage())
}

func TestSpinnerComplete(t *testing.T) {
opts := initialOpts
spinner := ysmrr.NewSpinner(opts)
Expand Down

0 comments on commit 242aa18

Please sign in to comment.