Adaptive bar resize
Vladimir Bauer
8 years ago
| 12 | 12 | * __Dynamic Total__: [Set total](https://github.com/vbauerster/mpb/issues/9#issuecomment-344448984) while bar is running |
| 13 | 13 | * __Dynamic Addition__: Additional bar could be added at later time |
| 14 | 14 | * __Dynamic Removal__: Remove particular bar, before or after completion |
| 15 | * __Dynamic Resize__: Bars are trying to resize at terminal width change, but don't expect much here | |
| 15 | * __Dynamic Resize__: Adaptive bar resize (doesn't work inside tmux) | |
| 16 | 16 | * __Cancellation__: Cancel whole rendering process |
| 17 | 17 | * __Predefined Decoratros__: Elapsed time, [Ewmaest](https://github.com/dgryski/trifles/tree/master/ewmaest) based ETA, Percentage, Bytes counter |
| 18 | 18 | * __Decorator's width sync__: Synchronized decorator's width among multiple bars |
| 354 | 354 | |
| 355 | 355 | func (s *state) draw(termWidth int, prependWs, appendWs *widthSync) { |
| 356 | 356 | if termWidth <= 0 { |
| 357 | termWidth = s.width | |
| 357 | termWidth = 2 | |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | stat := newStatistics(s) |
| 382 | 382 | prependCount := utf8.RuneCount(s.bufP.Bytes()) |
| 383 | 383 | appendCount := utf8.RuneCount(s.bufA.Bytes()) |
| 384 | 384 | |
| 385 | s.fillBar(s.width) | |
| 385 | if termWidth > s.width { | |
| 386 | s.fillBar(s.width) | |
| 387 | } else { | |
| 388 | s.fillBar(termWidth - prependCount - appendCount) | |
| 389 | } | |
| 386 | 390 | barCount := utf8.RuneCount(s.bufB.Bytes()) |
| 387 | 391 | totalCount := prependCount + barCount + appendCount |
| 388 | 392 | if totalCount > termWidth { |
| 389 | shrinkWidth := termWidth - prependCount - appendCount | |
| 390 | s.fillBar(shrinkWidth) | |
| 393 | s.fillBar(termWidth - prependCount - appendCount) | |
| 391 | 394 | } |
| 392 | 395 | s.bufA.WriteByte('\n') |
| 393 | 396 | } |
| 394 | 397 | |
| 395 | 398 | func (s *state) fillBar(width int) { |
| 396 | 399 | s.bufB.Reset() |
| 400 | s.bufB.WriteRune(s.format[rLeft]) | |
| 397 | 401 | if width <= 2 { |
| 402 | s.bufB.WriteRune(s.format[rRight]) | |
| 398 | 403 | return |
| 399 | 404 | } |
| 400 | 405 | |
| 402 | 407 | barWidth := width - 2 |
| 403 | 408 | |
| 404 | 409 | completedWidth := decor.CalcPercentage(s.total, s.current, barWidth) |
| 405 | ||
| 406 | s.bufB.WriteRune(s.format[rLeft]) | |
| 407 | 410 | |
| 408 | 411 | if s.refill != nil { |
| 409 | 412 | till := decor.CalcPercentage(s.total, s.refill.till, barWidth) |
| 16 | 16 | { |
| 17 | 17 | termWidth: 2, |
| 18 | 18 | barWidth: 100, |
| 19 | want: "", | |
| 19 | want: "[]", | |
| 20 | 20 | }, |
| 21 | 21 | { |
| 22 | 22 | termWidth: 3, |
| 3 | 3 | "fmt" |
| 4 | 4 | "io" |
| 5 | 5 | "os" |
| 6 | "os/signal" | |
| 6 | 7 | "runtime" |
| 7 | 8 | "sync" |
| 9 | "syscall" | |
| 8 | 10 | "time" |
| 9 | 11 | |
| 10 | 12 | "github.com/vbauerster/mpb/cwriter" |
| 176 | 178 | |
| 177 | 179 | // server monitors underlying channels and renders any progress bars |
| 178 | 180 | func (p *Progress) server(conf pConf) { |
| 181 | winch := make(chan os.Signal, 1) | |
| 182 | signal.Notify(winch, syscall.SIGWINCH) | |
| 183 | ||
| 179 | 184 | defer func() { |
| 180 | 185 | if conf.shutdownNotifier != nil { |
| 181 | 186 | close(conf.shutdownNotifier) |
| 182 | 187 | } |
| 188 | signal.Stop(winch) | |
| 183 | 189 | close(p.done) |
| 184 | 190 | }() |
| 185 | 191 | |
| 186 | 192 | numP, numA := -1, -1 |
| 193 | ||
| 194 | var timer *time.Timer | |
| 195 | var resumeTicker <-chan time.Time | |
| 196 | resumeDelay := 300 * time.Millisecond | |
| 187 | 197 | |
| 188 | 198 | for { |
| 189 | 199 | select { |
| 201 | 211 | if numA == -1 { |
| 202 | 212 | numA = b0.NumOfAppenders() |
| 203 | 213 | } |
| 204 | err := conf.writeAndFlush(numP, numA) | |
| 214 | tw, _, _ := cwriter.TermSize() | |
| 215 | err := conf.writeAndFlush(tw, numP, numA) | |
| 205 | 216 | if err != nil { |
| 206 | 217 | fmt.Fprintln(os.Stderr, err) |
| 207 | 218 | } |
| 219 | case <-winch: | |
| 220 | tw, _, _ := cwriter.TermSize() | |
| 221 | err := conf.writeAndFlush(tw-tw/6, numP, numA) | |
| 222 | if err != nil { | |
| 223 | fmt.Fprintln(os.Stderr, err) | |
| 224 | } | |
| 225 | if timer != nil && timer.Reset(resumeDelay) { | |
| 226 | break | |
| 227 | } | |
| 228 | conf.ticker.Stop() | |
| 229 | timer = time.NewTimer(resumeDelay) | |
| 230 | resumeTicker = timer.C | |
| 231 | case <-resumeTicker: | |
| 232 | conf.ticker = time.NewTicker(conf.rr) | |
| 233 | resumeTicker = nil | |
| 208 | 234 | case <-conf.cancel: |
| 209 | 235 | conf.ticker.Stop() |
| 210 | 236 | conf.cancel = nil |
| 254 | 280 | return ws |
| 255 | 281 | } |
| 256 | 282 | |
| 257 | func (p *pConf) writeAndFlush(numP, numA int) (err error) { | |
| 283 | func (p *pConf) writeAndFlush(tw, numP, numA int) (err error) { | |
| 258 | 284 | if p.beforeRender != nil { |
| 259 | 285 | p.beforeRender(p.bars) |
| 260 | 286 | } |
| 266 | 292 | |
| 267 | 293 | prependWs := newWidthSync(wSyncTimeout, len(p.bars), numP) |
| 268 | 294 | appendWs := newWidthSync(wSyncTimeout, len(p.bars), numA) |
| 269 | ||
| 270 | tw, _, _ := cwriter.TermSize() | |
| 271 | 295 | |
| 272 | 296 | sequence := make([]<-chan *writeBuf, len(p.bars)) |
| 273 | 297 | for i, b := range p.bars { |