panic explicitly if Add is called after Progress is done
Vladimir Bauer
6 years ago
| 3 | 3 | "bytes" |
| 4 | 4 | "container/heap" |
| 5 | 5 | "context" |
| 6 | "fmt" | |
| 6 | 7 | "io" |
| 7 | 8 | "io/ioutil" |
| 8 | 9 | "log" |
| 96 | 97 | return p |
| 97 | 98 | } |
| 98 | 99 | |
| 99 | // AddBar creates a new progress bar and adds to the container. | |
| 100 | // AddBar creates a new progress bar and adds it to the rendering queue. | |
| 100 | 101 | func (p *Progress) AddBar(total int64, options ...BarOption) *Bar { |
| 101 | 102 | return p.Add(total, NewBarFiller(DefaultBarStyle, false), options...) |
| 102 | 103 | } |
| 103 | 104 | |
| 104 | // AddSpinner creates a new spinner bar and adds to the container. | |
| 105 | // AddSpinner creates a new spinner bar and adds it to the rendering queue. | |
| 105 | 106 | func (p *Progress) AddSpinner(total int64, alignment SpinnerAlignment, options ...BarOption) *Bar { |
| 106 | 107 | return p.Add(total, NewSpinnerFiller(DefaultSpinnerStyle, alignment), options...) |
| 107 | 108 | } |
| 108 | 109 | |
| 109 | 110 | // Add creates a bar which renders itself by provided filler. |
| 110 | 111 | // Set total to 0, if you plan to update it later. |
| 112 | // Panics if *Progress instance is done, i.e. called after *Progress.Wait(). | |
| 111 | 113 | func (p *Progress) Add(total int64, filler Filler, options ...BarOption) *Bar { |
| 112 | 114 | if filler == nil { |
| 113 | 115 | filler = NewBarFiller(DefaultBarStyle, false) |
| 133 | 135 | return bar |
| 134 | 136 | case <-p.done: |
| 135 | 137 | p.bwg.Done() |
| 136 | return nil | |
| 138 | panic(fmt.Sprintf("%T instance can't be reused after it's done!", p)) | |
| 137 | 139 | } |
| 138 | 140 | } |
| 139 | 141 | |