Skip to content

Commit 13d3a1d

Browse files
authored
progress: render never started trackers properly (#273)
1 parent f16973f commit 13d3a1d

28 files changed

+221
-235
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Checkout Code
2222
uses: actions/checkout@v2
2323

24-
# Set up the GoLang enviroment
24+
# Set up the GoLang environment
2525
- name: Set up Go
2626
uses: actions/setup-go@v2
2727
with:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,4 @@ the following from within your code folder to do the same:
111111
```
112112
find . -type f -name "*.go" | grep -v vendor | xargs sed -i 's/jedib0t\/go-pretty\//jedib0t\/go-pretty\/v6\//g'
113113
```
114-
If you are on MacOS, you'll have to use `sed -i ''` instead of `sed -i`.
114+
If you are on macOS, you'll have to use `sed -i ''` instead of `sed -i`.

cmd/demo-progress/demo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func trackSomething(pw progress.Writer, idx int64, updateMessage bool) {
9797
}
9898
pw.SetPinnedMessages(
9999
fmt.Sprintf(">> Current Time: %-32s", time.Now().Format(time.RFC3339)),
100-
fmt.Sprintf(">> Total Time: %-32s", time.Now().Sub(timeStart).Round(time.Millisecond)),
100+
fmt.Sprintf(">> Total Time: %-32s", time.Since(timeStart).Round(time.Millisecond)),
101101
)
102102
case <-updateTicker:
103103
if updateMessage {

list/list.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type listItem struct {
1919
Text string
2020
}
2121

22-
// List helps print a 2-dimensional array in a human readable pretty-List.
22+
// List helps print a 2-dimensional array in a human-readable pretty-List.
2323
type List struct {
2424
// approxSize stores the approximate output length/size
2525
approxSize int
@@ -71,7 +71,7 @@ func (l *List) Reset() {
7171
l.style = nil
7272
}
7373

74-
// SetHTMLCSSClass sets the the HTML CSS Class to use on the <ul> node
74+
// SetHTMLCSSClass sets the HTML CSS Class to use on the <ul> node
7575
// when rendering the List in HTML format. Recursive lists would use a numbered
7676
// index suffix. For ex., if the cssClass is set as "foo"; the <ul> for level 0
7777
// would have the class set as "foo"; the <ul> for level 1 would have "foo-1".
@@ -101,12 +101,8 @@ func (l *List) Style() *Style {
101101

102102
func (l *List) analyzeAndStringify(item interface{}) *listItem {
103103
itemStr := fmt.Sprint(item)
104-
if strings.Contains(itemStr, "\t") {
105-
itemStr = strings.Replace(itemStr, "\t", " ", -1)
106-
}
107-
if strings.Contains(itemStr, "\r") {
108-
itemStr = strings.Replace(itemStr, "\r", "", -1)
109-
}
104+
itemStr = strings.ReplaceAll(itemStr, "\t", " ")
105+
itemStr = strings.ReplaceAll(itemStr, "\r", "")
110106
return &listItem{
111107
Level: l.level,
112108
Text: itemStr,

list/render.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func (l *List) Render() string {
2222
out.Grow(l.approxSize)
2323
for idx, item := range l.items {
2424
hint := renderHint{
25-
isTopItem: bool(idx == 0),
26-
isFirstItem: bool(idx == 0 || item.Level > l.items[idx-1].Level),
25+
isTopItem: idx == 0,
26+
isFirstItem: idx == 0 || item.Level > l.items[idx-1].Level,
2727
isLastItem: !l.hasMoreItemsInLevel(item.Level, idx),
28-
isBottomItem: bool(idx == len(l.items)-1),
28+
isBottomItem: idx == len(l.items)-1,
2929
}
3030
if hint.isFirstItem && hint.isLastItem {
3131
hint.isOnlyItem = true
@@ -56,7 +56,7 @@ func (l *List) renderItem(out *strings.Builder, idx int, item *listItem, hint re
5656
}
5757

5858
// render the prefix or the leading text before the actual item
59-
l.renderItemBulletPrefix(out, idx, item.Level, lineIdx, hint)
59+
l.renderItemBulletPrefix(out, idx, item.Level)
6060
l.renderItemBullet(out, lineIdx, hint)
6161

6262
// render the actual item
@@ -73,11 +73,11 @@ func (l *List) renderItemBullet(out *strings.Builder, lineIdx int, hint renderHi
7373
out.WriteString(l.style.CharItemVertical)
7474
}
7575
} else {
76-
l.renderItemBulletSingleLine(out, lineIdx, hint)
76+
l.renderItemBulletSingleLine(out, hint)
7777
}
7878
}
7979

80-
func (l *List) renderItemBulletSingleLine(out *strings.Builder, lineIdx int, hint renderHint) {
80+
func (l *List) renderItemBulletSingleLine(out *strings.Builder, hint renderHint) {
8181
// single-line item.Text (or first line of a multi-line item.Text)
8282
if hint.isOnlyItem {
8383
if hint.isTopItem {
@@ -97,7 +97,7 @@ func (l *List) renderItemBulletSingleLine(out *strings.Builder, lineIdx int, hin
9797
out.WriteRune(' ')
9898
}
9999

100-
func (l *List) renderItemBulletPrefix(out *strings.Builder, itemIdx int, itemLevel int, lineIdx int, hint renderHint) {
100+
func (l *List) renderItemBulletPrefix(out *strings.Builder, itemIdx int, itemLevel int) {
101101
// write a prefix if one has been set in l.style
102102
if l.style.LinePrefix != "" {
103103
out.WriteString(l.style.LinePrefix)

list/render_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,4 @@ func TestList_Render_UnindentAll(t *testing.T) {
331331
* Way
332332
* Right?`
333333
assert.Equal(t, expectedOut, lw.Render())
334-
335334
}

list/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package list
22

33
import "io"
44

5-
// Writer declares the interfaces that can be used to setup and render a list.
5+
// Writer declares the interfaces that can be used to set up and render a list.
66
type Writer interface {
77
AppendItem(item interface{})
88
AppendItems(items []interface{})

progress/progress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (p *Progress) SetPinnedMessages(messages ...string) {
199199
}
200200

201201
// SetSortBy defines the sorting mechanism to use to sort the Active Trackers
202-
// before rendering the. Default: no-sorting == sort-by-insertion-order.
202+
// before rendering. Default: no-sorting == sort-by-insertion-order.
203203
func (p *Progress) SetSortBy(sortBy SortBy) {
204204
p.sortBy = sortBy
205205
}
@@ -221,7 +221,7 @@ func (p *Progress) SetTrackerPosition(position Position) {
221221
}
222222

223223
// SetUpdateFrequency sets the update frequency while rendering the trackers.
224-
// the lower the value, the more number of times the Trackers get refreshed. A
224+
// the lower the value, the more frequently the Trackers get refreshed. A
225225
// sane value would be 250ms.
226226
func (p *Progress) SetUpdateFrequency(frequency time.Duration) {
227227
p.updateFrequency = frequency

progress/progress_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func TestProgress_SetUpdateFrequency(t *testing.T) {
169169
p.initForRender()
170170
assert.Equal(t, DefaultUpdateFrequency, p.updateFrequency)
171171

172-
p.SetUpdateFrequency(time.Duration(time.Second))
173-
assert.Equal(t, time.Duration(time.Second), p.updateFrequency)
172+
p.SetUpdateFrequency(time.Second)
173+
assert.Equal(t, time.Second, p.updateFrequency)
174174
}
175175

176176
func TestProgress_ShowETA(t *testing.T) {

progress/render.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (p *Progress) generateTrackerStrIndeterminate(maxLen int) string {
149149
}
150150

151151
return p.style.Colors.Tracker.Sprintf("%s%s%s",
152-
p.style.Chars.BoxLeft, string(pUnfinished), p.style.Chars.BoxRight,
152+
p.style.Chars.BoxLeft, pUnfinished, p.style.Chars.BoxRight,
153153
)
154154
}
155155

@@ -185,12 +185,8 @@ func (p *Progress) renderPinnedMessages(out *strings.Builder) {
185185

186186
func (p *Progress) renderTracker(out *strings.Builder, t *Tracker, hint renderHint) {
187187
message := t.message()
188-
if strings.Contains(message, "\t") {
189-
message = strings.Replace(message, "\t", " ", -1)
190-
}
191-
if strings.Contains(message, "\r") {
192-
message = strings.Replace(message, "\r", "", -1)
193-
}
188+
message = strings.ReplaceAll(message, "\t", " ")
189+
message = strings.ReplaceAll(message, "\r", "")
194190
if p.messageWidth > 0 {
195191
messageLen := text.RuneWidthWithoutEscSequences(message)
196192
if messageLen < p.messageWidth {
@@ -412,10 +408,12 @@ func (p *Progress) renderTrackerStatsSpeedInternal(out *strings.Builder, speed s
412408

413409
func (p *Progress) renderTrackerStatsTime(outStats *strings.Builder, t *Tracker, hint renderHint) {
414410
var td, tp time.Duration
415-
if t.IsDone() {
416-
td = t.timeStop.Sub(t.timeStart)
417-
} else if !t.timeStart.IsZero() {
418-
td = time.Since(t.timeStart)
411+
if !t.timeStart.IsZero() {
412+
if t.IsDone() {
413+
td = t.timeStop.Sub(t.timeStart)
414+
} else {
415+
td = time.Since(t.timeStart)
416+
}
419417
}
420418
if hint.isOverallTracker {
421419
tp = p.style.Options.TimeOverallPrecision

0 commit comments

Comments
 (0)