From 0cf4a5dc48cda59c27b73d4857d3517207764984 Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 3 Dec 2024 08:34:17 -0500 Subject: [PATCH] Add mutex lock within Add function --- progress/progress.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/progress/progress.go b/progress/progress.go index cfeb845..c83caab 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -44,9 +44,6 @@ func New(info ProgressInfo) *ProgressBar { func (p *ProgressBar) render(final bool) { - p.lock.Lock() - defer p.lock.Unlock() - var sb strings.Builder numFilled := math.Round((float64(p.currentBytes) / float64(p.maxBytes)) * 40) @@ -121,7 +118,7 @@ func (p *ProgressBar) render(final bool) { if width == 0 { lineNum = 1 } else { - lineNum = ((len(removeANSIEscapeCodes(sb.String())) + width - 1) / width) + lineNum = (len(removeANSIEscapeCodes(sb.String())) + width - 1) / width } if p.hasStarted { @@ -142,9 +139,11 @@ func (p *ProgressBar) render(final bool) { } func (p *ProgressBar) Add(num int64) { + p.lock.Lock() p.currentBytes = p.currentBytes + num p.render(false) + p.lock.Unlock() } func (p *ProgressBar) Close() (err error) { @@ -161,7 +160,7 @@ func (p *ProgressBar) Close() (err error) { if width == 0 { lineNum = 1 } else { - lineNum = ((p.textWidth + width - 1) / width) + lineNum = (p.textWidth + width - 1) / width } for i := 0; i < lineNum; i++ {