Skip to content

Commit

Permalink
progress: Overall ETA never less than max ETA (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Aug 28, 2023
1 parent 13d3a1d commit 05c0986
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
5 changes: 5 additions & 0 deletions progress/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()
}
Expand Down
7 changes: 6 additions & 1 deletion progress/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
13 changes: 6 additions & 7 deletions table/render_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
15 changes: 7 additions & 8 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions text/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 05c0986

Please sign in to comment.