From 05c09865424e99fba0288a09a07107a5510b1a6b Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Mon, 28 Aug 2023 11:41:51 +1000 Subject: [PATCH] progress: Overall ETA never less than max ETA (#274) --- progress/render.go | 5 +++++ progress/tracker.go | 7 ++++++- table/render_markdown.go | 13 ++++++------- table/table.go | 15 +++++++-------- text/transformer.go | 6 +++--- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/progress/render.go b/progress/render.go index 30fcd77..fe77501 100644 --- a/progress/render.go +++ b/progress/render.go @@ -70,10 +70,14 @@ func (p *Progress) extractDoneAndActiveTrackers() ([]*Tracker, []*Tracker) { var trackersActive, trackersDone []*Tracker var activeTrackersProgress int64 p.trackersActiveMutex.RLock() + var maxETA time.Duration for _, tracker := range p.trackersActive { if !tracker.IsDone() { trackersActive = append(trackersActive, tracker) activeTrackersProgress += int64(tracker.PercentDone()) + if eta := tracker.ETA(); eta > maxETA { + maxETA = eta + } } else { trackersDone = append(trackersDone, tracker) } @@ -85,6 +89,7 @@ func (p *Progress) extractDoneAndActiveTrackers() ([]*Tracker, []*Tracker) { // calculate the overall tracker's progress value p.overallTracker.value = int64(p.LengthDone()+len(trackersDone)) * 100 p.overallTracker.value += activeTrackersProgress + p.overallTracker.minETA = maxETA if len(trackersActive) == 0 { p.overallTracker.MarkAsDone() } diff --git a/progress/tracker.go b/progress/tracker.go index 78b5dce..30cb57c 100644 --- a/progress/tracker.go +++ b/progress/tracker.go @@ -35,6 +35,7 @@ type Tracker struct { timeStart time.Time timeStop time.Time value int64 + minETA time.Duration } // ETA returns the expected time of "arrival" or completion of this tracker. It @@ -56,7 +57,11 @@ func (t *Tracker) ETA() time.Duration { if pDone == 0 { return time.Duration(0) } - return time.Duration((int64(timeTaken) / pDone) * (100 - pDone)) + eta := time.Duration((int64(timeTaken) / pDone) * (100 - pDone)) + if eta < t.minETA { + eta = t.minETA + } + return eta } // Increment updates the current value of the task being tracked. diff --git a/table/render_markdown.go b/table/render_markdown.go index adf573f..1a8c488 100644 --- a/table/render_markdown.go +++ b/table/render_markdown.go @@ -6,13 +6,12 @@ import ( ) // RenderMarkdown renders the Table in Markdown format. Example: -// -// | # | First Name | Last Name | Salary | | -// | ---:| --- | --- | ---:| --- | -// | 1 | Arya | Stark | 3000 | | -// | 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! | -// | 300 | Tyrion | Lannister | 5000 | | -// | | | Total | 10000 | | +// | # | First Name | Last Name | Salary | | +// | ---:| --- | --- | ---:| --- | +// | 1 | Arya | Stark | 3000 | | +// | 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! | +// | 300 | Tyrion | Lannister | 5000 | | +// | | | Total | 10000 | | func (t *Table) RenderMarkdown() string { t.initForRender() diff --git a/table/table.go b/table/table.go index c04fa06..1801c1c 100644 --- a/table/table.go +++ b/table/table.go @@ -164,15 +164,14 @@ func (t *Table) AppendRows(rows []Row, config ...RowConfig) { // append is a separator, it will not be rendered in addition to the usual table // separator. // -// ****************************************************************************** +//****************************************************************************** // Please note the following caveats: -// 1. SetPageSize(): this may end up creating consecutive separator rows near -// the end of a page or at the beginning of a page -// 2. SortBy(): since SortBy could inherently alter the ordering of rows, the -// separators may not appear after the row it was originally intended to -// follow -// -// ****************************************************************************** +// 1. SetPageSize(): this may end up creating consecutive separator rows near +// the end of a page or at the beginning of a page +// 2. SortBy(): since SortBy could inherently alter the ordering of rows, the +// separators may not appear after the row it was originally intended to +// follow +//****************************************************************************** func (t *Table) AppendSeparator() { if t.separators == nil { t.separators = make(map[int]bool) diff --git a/text/transformer.go b/text/transformer.go index 193a721..4551436 100644 --- a/text/transformer.go +++ b/text/transformer.go @@ -37,9 +37,9 @@ var ( type Transformer func(val interface{}) string // NewNumberTransformer returns a number Transformer that: -// - transforms the number as directed by 'format' (ex.: %.2f) -// - colors negative values Red -// - colors positive values Green +// * transforms the number as directed by 'format' (ex.: %.2f) +// * colors negative values Red +// * colors positive values Green func NewNumberTransformer(format string) Transformer { return func(val interface{}) string { if valStr := transformInt(format, val); valStr != "" {