From 6d0f4085eafe8eb1a4e63b9c2861437060b4d67a Mon Sep 17 00:00:00 2001 From: Naveen Mahalingam Date: Thu, 22 Apr 2021 11:15:08 -0700 Subject: [PATCH] progress: make message rendering work 100% (alt fix for #164) (#165) --- progress/render.go | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/progress/render.go b/progress/render.go index 8480df8..2593e8e 100644 --- a/progress/render.go +++ b/progress/render.go @@ -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()) @@ -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)) @@ -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 { @@ -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)) } }