-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fancy progress bar for updates
- Loading branch information
1 parent
9b642c5
commit 49c6cae
Showing
4 changed files
with
131 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package lib | ||
|
||
import ( | ||
"github.com/jedib0t/go-pretty/v6/progress" | ||
"github.com/jedib0t/go-pretty/v6/text" | ||
"time" | ||
) | ||
|
||
type Incrementer struct { | ||
doneIncrements int | ||
MaxIncrements int | ||
} | ||
|
||
type IncrementTracker struct { | ||
Tracker *progress.Tracker | ||
incrementer *Incrementer | ||
} | ||
|
||
var CuteColors = progress.StyleColors{ | ||
Message: text.Colors{text.FgWhite}, | ||
Error: text.Colors{text.FgRed}, | ||
Percent: text.Colors{text.FgHiBlue}, | ||
Pinned: text.Colors{text.BgHiBlack, text.FgWhite, text.Bold}, | ||
Stats: text.Colors{text.FgHiBlack}, | ||
Time: text.Colors{text.FgBlue}, | ||
Tracker: text.Colors{text.FgHiBlue}, | ||
Value: text.Colors{text.FgBlue}, | ||
Speed: text.Colors{text.FgBlue}, | ||
} | ||
|
||
func NewProgressWriter() progress.Writer { | ||
pw := progress.NewWriter() | ||
pw.SetTrackerLength(25) | ||
pw.Style().Visibility.TrackerOverall = true | ||
pw.Style().Visibility.Time = true | ||
pw.Style().Visibility.Tracker = true | ||
pw.Style().Visibility.Value = true | ||
pw.SetMessageLength(32) | ||
pw.SetSortBy(progress.SortByPercentDsc) | ||
pw.SetStyle(progress.StyleBlocks) | ||
pw.SetTrackerPosition(progress.PositionRight) | ||
pw.SetUpdateFrequency(time.Millisecond * 100) | ||
pw.Style().Options.PercentFormat = "%4.1f%%" | ||
pw.Style().Colors = CuteColors | ||
return pw | ||
} | ||
|
||
func NewIncrementTracker(tracker *progress.Tracker, max_increments int) *IncrementTracker { | ||
return &IncrementTracker{ | ||
Tracker: tracker, | ||
incrementer: &Incrementer{MaxIncrements: max_increments}, | ||
} | ||
} | ||
|
||
func ChangeTrackerMessageFancy(writer progress.Writer, tracker *IncrementTracker, message string) { | ||
writer.SetMessageLength(len(message)) | ||
tracker.Tracker.UpdateMessage(message) | ||
} | ||
|
||
func (it *IncrementTracker) IncrementSection() { | ||
var increment_step float64 | ||
if it.incrementer.doneIncrements == 0 { | ||
increment_step = 1 | ||
} else { | ||
increment_step = float64(it.Tracker.Total / int64(it.incrementer.MaxIncrements)) | ||
} | ||
it.Tracker.Increment(int64(increment_step)) | ||
it.incrementer.doneIncrements++ | ||
} | ||
|
||
func (it *IncrementTracker) IncrementSectionError() { | ||
var increment_step float64 | ||
if it.incrementer.doneIncrements == 0 { | ||
increment_step = 1 | ||
} else { | ||
increment_step = float64(it.Tracker.Total / int64(it.incrementer.MaxIncrements)) | ||
} | ||
it.Tracker.IncrementWithError(int64(increment_step)) | ||
it.incrementer.doneIncrements++ | ||
} | ||
|
||
func (it *IncrementTracker) CurrentStep() int { | ||
return it.incrementer.doneIncrements | ||
} |