Codebase list golang-github-vbauerster-mpb / 615080e
Adaptive bar resize Vladimir Bauer 8 years ago
4 changed file(s) with 39 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
1212 * __Dynamic Total__: [Set total](https://github.com/vbauerster/mpb/issues/9#issuecomment-344448984) while bar is running
1313 * __Dynamic Addition__: Additional bar could be added at later time
1414 * __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)
1616 * __Cancellation__: Cancel whole rendering process
1717 * __Predefined Decoratros__: Elapsed time, [Ewmaest](https://github.com/dgryski/trifles/tree/master/ewmaest) based ETA, Percentage, Bytes counter
1818 * __Decorator's width sync__: Synchronized decorator's width among multiple bars
354354
355355 func (s *state) draw(termWidth int, prependWs, appendWs *widthSync) {
356356 if termWidth <= 0 {
357 termWidth = s.width
357 termWidth = 2
358358 }
359359
360360 stat := newStatistics(s)
382382 prependCount := utf8.RuneCount(s.bufP.Bytes())
383383 appendCount := utf8.RuneCount(s.bufA.Bytes())
384384
385 s.fillBar(s.width)
385 if termWidth > s.width {
386 s.fillBar(s.width)
387 } else {
388 s.fillBar(termWidth - prependCount - appendCount)
389 }
386390 barCount := utf8.RuneCount(s.bufB.Bytes())
387391 totalCount := prependCount + barCount + appendCount
388392 if totalCount > termWidth {
389 shrinkWidth := termWidth - prependCount - appendCount
390 s.fillBar(shrinkWidth)
393 s.fillBar(termWidth - prependCount - appendCount)
391394 }
392395 s.bufA.WriteByte('\n')
393396 }
394397
395398 func (s *state) fillBar(width int) {
396399 s.bufB.Reset()
400 s.bufB.WriteRune(s.format[rLeft])
397401 if width <= 2 {
402 s.bufB.WriteRune(s.format[rRight])
398403 return
399404 }
400405
402407 barWidth := width - 2
403408
404409 completedWidth := decor.CalcPercentage(s.total, s.current, barWidth)
405
406 s.bufB.WriteRune(s.format[rLeft])
407410
408411 if s.refill != nil {
409412 till := decor.CalcPercentage(s.total, s.refill.till, barWidth)
1616 {
1717 termWidth: 2,
1818 barWidth: 100,
19 want: "",
19 want: "[]",
2020 },
2121 {
2222 termWidth: 3,
33 "fmt"
44 "io"
55 "os"
6 "os/signal"
67 "runtime"
78 "sync"
9 "syscall"
810 "time"
911
1012 "github.com/vbauerster/mpb/cwriter"
176178
177179 // server monitors underlying channels and renders any progress bars
178180 func (p *Progress) server(conf pConf) {
181 winch := make(chan os.Signal, 1)
182 signal.Notify(winch, syscall.SIGWINCH)
183
179184 defer func() {
180185 if conf.shutdownNotifier != nil {
181186 close(conf.shutdownNotifier)
182187 }
188 signal.Stop(winch)
183189 close(p.done)
184190 }()
185191
186192 numP, numA := -1, -1
193
194 var timer *time.Timer
195 var resumeTicker <-chan time.Time
196 resumeDelay := 300 * time.Millisecond
187197
188198 for {
189199 select {
201211 if numA == -1 {
202212 numA = b0.NumOfAppenders()
203213 }
204 err := conf.writeAndFlush(numP, numA)
214 tw, _, _ := cwriter.TermSize()
215 err := conf.writeAndFlush(tw, numP, numA)
205216 if err != nil {
206217 fmt.Fprintln(os.Stderr, err)
207218 }
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
208234 case <-conf.cancel:
209235 conf.ticker.Stop()
210236 conf.cancel = nil
254280 return ws
255281 }
256282
257 func (p *pConf) writeAndFlush(numP, numA int) (err error) {
283 func (p *pConf) writeAndFlush(tw, numP, numA int) (err error) {
258284 if p.beforeRender != nil {
259285 p.beforeRender(p.bars)
260286 }
266292
267293 prependWs := newWidthSync(wSyncTimeout, len(p.bars), numP)
268294 appendWs := newWidthSync(wSyncTimeout, len(p.bars), numA)
269
270 tw, _, _ := cwriter.TermSize()
271295
272296 sequence := make([]<-chan *writeBuf, len(p.bars))
273297 for i, b := range p.bars {