Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

progress: Overall ETA never less than max ETA #274

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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