From f6b258ec9d09f464bb0dc0ca94a3dfc9e64554ba Mon Sep 17 00:00:00 2001 From: Naveen Mahalingam Date: Mon, 3 Dec 2018 17:36:26 -0800 Subject: [PATCH] progress tracker: SetValue() to set a custom value (#73) --- progress/tracker.go | 7 +++++++ progress/tracker_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/progress/tracker.go b/progress/tracker.go index 40cc32d..7b6f9b6 100644 --- a/progress/tracker.go +++ b/progress/tracker.go @@ -80,6 +80,13 @@ func (t *Tracker) Reset() { t.value = 0 } +// SetValue sets the value of the tracker and re-calculates if the tracker is +// "done". +func (t *Tracker) SetValue(value int64) { + t.Reset() + t.Increment(value) +} + func (t *Tracker) start() { t.done = false t.timeStart = time.Now() diff --git a/progress/tracker_test.go b/progress/tracker_test.go index 4722a10..7a411b6 100644 --- a/progress/tracker_test.go +++ b/progress/tracker_test.go @@ -101,3 +101,17 @@ func TestTracker_Reset(t *testing.T) { assert.Equal(t, time.Time{}, tracker.timeStop) assert.Equal(t, int64(0), tracker.value) } + +func TestTracker_SetValue(t *testing.T) { + tracker := Tracker{Total: 100} + assert.Equal(t, int64(0), tracker.value) + assert.False(t, tracker.done) + + tracker.SetValue(5) + assert.Equal(t, int64(5), tracker.value) + assert.False(t, tracker.done) + + tracker.SetValue(tracker.Total) + assert.Equal(t, tracker.Total, tracker.value) + assert.True(t, tracker.done) +}