introduce ErrCallAfterStop error
Vladimir Bauer
9 years ago
| 0 | 0 | package mpb |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "errors" | |
| 3 | 4 | "fmt" |
| 4 | 5 | "io" |
| 5 | 6 | "os" |
| 9 | 10 | |
| 10 | 11 | "github.com/vbauerster/mpb/cwriter" |
| 11 | 12 | ) |
| 13 | ||
| 14 | var ErrCallAfterStop = errors.New("method call on stopped Progress instance") | |
| 12 | 15 | |
| 13 | 16 | type opType uint |
| 14 | 17 | |
| 74 | 77 | return p |
| 75 | 78 | } |
| 76 | 79 | |
| 77 | // SetOut sets underlying writer of progress | |
| 78 | // default is os.Stdout | |
| 80 | // SetOut sets underlying writer of progress. Default is os.Stdout | |
| 81 | // pancis, if called on stopped Progress instance, i.e after Stop() | |
| 79 | 82 | func (p *Progress) SetOut(w io.Writer) *Progress { |
| 83 | if p.isAllDone() { | |
| 84 | panic(ErrCallAfterStop) | |
| 85 | } | |
| 80 | 86 | if w == nil { |
| 81 | 87 | return p |
| 82 | 88 | } |
| 85 | 91 | } |
| 86 | 92 | |
| 87 | 93 | // RefreshRate overrides default (30ms) refreshRate value |
| 94 | // pancis, if called on stopped Progress instance, i.e after Stop() | |
| 88 | 95 | func (p *Progress) RefreshRate(d time.Duration) *Progress { |
| 96 | if p.isAllDone() { | |
| 97 | panic(ErrCallAfterStop) | |
| 98 | } | |
| 89 | 99 | p.rrChangeReqCh <- d |
| 90 | 100 | return p |
| 91 | 101 | } |
| 97 | 107 | } |
| 98 | 108 | |
| 99 | 109 | // AddBar creates a new progress bar and adds to the container |
| 110 | // pancis, if called on stopped Progress instance, i.e after Stop() | |
| 100 | 111 | func (p *Progress) AddBar(total int) *Bar { |
| 112 | if p.isAllDone() { | |
| 113 | panic(ErrCallAfterStop) | |
| 114 | } | |
| 101 | 115 | result := make(chan bool) |
| 102 | 116 | bar := newBar(total, p.width, p.wg) |
| 103 | 117 | p.op <- &operation{opBarAdd, bar, result} |
| 108 | 122 | } |
| 109 | 123 | |
| 110 | 124 | // RemoveBar removes bar at any time |
| 125 | // pancis, if called on stopped Progress instance, i.e after Stop() | |
| 111 | 126 | func (p *Progress) RemoveBar(b *Bar) bool { |
| 127 | if p.isAllDone() { | |
| 128 | panic(ErrCallAfterStop) | |
| 129 | } | |
| 112 | 130 | result := make(chan bool) |
| 113 | 131 | p.op <- &operation{opBarRemove, b, result} |
| 114 | 132 | return <-result |
| 115 | 133 | } |
| 116 | 134 | |
| 117 | 135 | // BarsCount returns bars count in the container |
| 136 | // pancis, if called on stopped Progress instance, i.e after Stop() | |
| 118 | 137 | func (p *Progress) BarsCount() int { |
| 138 | if p.isAllDone() { | |
| 139 | panic(ErrCallAfterStop) | |
| 140 | } | |
| 119 | 141 | respCh := make(chan int) |
| 120 | 142 | p.countReqCh <- respCh |
| 121 | 143 | return <-respCh |