Add mutex lock within Add function

This commit is contained in:
Shane C. 2024-12-03 08:34:17 -05:00
parent f50cbdf595
commit 0cf4a5dc48
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9

View file

@ -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++ {