Skip to content

Commit

Permalink
fix: add code to check if the progressbar is finished before add detail.
Browse files Browse the repository at this point in the history
  • Loading branch information
chengxilo committed Aug 28, 2024
1 parent b0608d0 commit a829eb6
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,23 +609,31 @@ func (p *ProgressBar) Add64(num int64) error {
}

// AddDetail adds a detail to the progress bar. Only used when maxDetailRow is set to a value greater than 0
func (p *ProgressBar) AddDetail(detail string) {
func (p *ProgressBar) AddDetail(detail string) error {
if p.config.maxDetailRow == 0 {
return
return errors.New("maxDetailRow is set to 0, cannot add detail")
}
if p.IsFinished() {
return errors.New("cannot add detail to a finished progress bar")
}

p.lock.Lock()
defer p.lock.Unlock()
if p.state.details == nil {
// if we add a detail before the first add, it will be weird that we have detail but don't have the progress bar in the top.
// so when we add the first detail, we will render the progress bar first.
p.render()
if err := p.render(); err != nil {
return err
}
}
p.state.details = append(p.state.details, detail)
if len(p.state.details) > p.config.maxDetailRow {
p.state.details = p.state.details[1:]
}
p.renderDetails()
if err := p.renderDetails(); err != nil {
return err
}
return nil
}

// renderDetails renders the details of the progress bar
Expand Down

0 comments on commit a829eb6

Please # to comment.