Skip to content

Commit

Permalink
progress: make message rendering work 100% (alt fix for #164) (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Apr 22, 2021
1 parent 43034d9 commit 6d0f408
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions progress/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,18 @@ func (p *Progress) moveCursorToTheTop(out *strings.Builder) {
func (p *Progress) renderTracker(out *strings.Builder, t *Tracker, hint renderHint) {
message := t.message()
if strings.Contains(message, "\t") {
t.Message = strings.Replace(message, "\t", " ", -1)
message = strings.Replace(message, "\t", " ", -1)
}
if strings.Contains(message, "\r") {
t.Message = strings.Replace(message, "\r", "", -1)
message = strings.Replace(message, "\r", "", -1)
}
if p.messageWidth > 0 {
messageLen := text.RuneCount(message)
if messageLen < p.messageWidth {
message = text.Pad(message, p.messageWidth, ' ')
} else {
message = text.Snip(message, p.messageWidth, p.style.Options.SnipIndicator)
}
}

out.WriteString(text.EraseLine.Sprint())
Expand All @@ -211,20 +219,20 @@ func (p *Progress) renderTracker(out *strings.Builder, t *Tracker, hint renderHi
trackerLen += text.RuneCount(p.style.Options.DoneString)
trackerLen += p.lengthProgress + 1
hint := renderHint{hideValue: true, isOverallTracker: true}
p.renderTrackerProgress(out, t, p.generateTrackerStr(t, trackerLen, hint), hint)
p.renderTrackerProgress(out, t, message, p.generateTrackerStr(t, trackerLen, hint), hint)
}
} else {
if t.IsDone() {
p.renderTrackerDone(out, t)
p.renderTrackerDone(out, t, message)
} else {
hint := renderHint{hideTime: p.hideTime, hideValue: p.hideValue}
p.renderTrackerProgress(out, t, p.generateTrackerStr(t, p.lengthProgress, hint), hint)
p.renderTrackerProgress(out, t, message, p.generateTrackerStr(t, p.lengthProgress, hint), hint)
}
}
}

func (p *Progress) renderTrackerDone(out *strings.Builder, t *Tracker) {
out.WriteString(p.style.Colors.Message.Sprint(t.message()))
func (p *Progress) renderTrackerDone(out *strings.Builder, t *Tracker, message string) {
out.WriteString(p.style.Colors.Message.Sprint(message))
out.WriteString(p.style.Colors.Message.Sprint(p.style.Options.Separator))
if !t.IsErrored() {
out.WriteString(p.style.Colors.Message.Sprint(p.style.Options.DoneString))
Expand All @@ -235,23 +243,13 @@ func (p *Progress) renderTrackerDone(out *strings.Builder, t *Tracker) {
out.WriteRune('\n')
}

func (p *Progress) renderTrackerProgress(out *strings.Builder, t *Tracker, trackerStr string, hint renderHint) {
if p.messageWidth > 0 {
message := t.message()
messageLen := text.RuneCount(message)
if messageLen < p.messageWidth {
t.UpdateMessage(text.Pad(message, p.messageWidth, ' '))
} else {
t.UpdateMessage(text.Snip(message, p.messageWidth, p.style.Options.SnipIndicator))
}
}

func (p *Progress) renderTrackerProgress(out *strings.Builder, t *Tracker, message string, trackerStr string, hint renderHint) {
if hint.isOverallTracker {
out.WriteString(p.style.Colors.Tracker.Sprint(trackerStr))
p.renderTrackerStats(out, t, hint)
out.WriteRune('\n')
} else if p.trackerPosition == PositionRight {
p.renderTrackerMessage(out, t)
p.renderTrackerMessage(out, t, message)
out.WriteString(p.style.Colors.Message.Sprint(p.style.Options.Separator))
p.renderTrackerPercentage(out, t)
if !p.hideTracker {
Expand All @@ -266,16 +264,16 @@ func (p *Progress) renderTrackerProgress(out *strings.Builder, t *Tracker, track
}
p.renderTrackerStats(out, t, hint)
out.WriteString(p.style.Colors.Message.Sprint(p.style.Options.Separator))
p.renderTrackerMessage(out, t)
p.renderTrackerMessage(out, t, message)
out.WriteRune('\n')
}
}

func (p *Progress) renderTrackerMessage(out *strings.Builder, t *Tracker) {
func (p *Progress) renderTrackerMessage(out *strings.Builder, t *Tracker, message string) {
if !t.IsErrored() {
out.WriteString(p.style.Colors.Message.Sprint(t.message()))
out.WriteString(p.style.Colors.Message.Sprint(message))
} else {
out.WriteString(p.style.Colors.Error.Sprint(t.message()))
out.WriteString(p.style.Colors.Error.Sprint(message))
}
}

Expand Down

0 comments on commit 6d0f408

Please sign in to comment.