Codebase list golang-github-vbauerster-mpb / 383d26a
Some refactoring and better names Vladimir Bauer 8 years ago
1 changed file(s) with 19 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
2222 etaAlpha = 0.25
2323 )
2424
25 type barFmtRunes [formatLen]rune
26 type barFmtBytes [formatLen][]byte
25 type fmtRunes [formatLen]rune
26 type fmtByteSegments [formatLen][]byte
2727
2828 // Bar represents a progress Bar
2929 type Bar struct {
4545 state struct {
4646 id int
4747 width int
48 format barFmtRunes
48 format fmtRunes
4949 etaAlpha float64
5050 total int64
5151 current int64
353353
354354 var barBlock []byte
355355 buf := make([]byte, 0, termWidth)
356 fmtBytes := convertFmtRunesToBytes(s.format)
356 segments := fmtRunesToByteSegments(s.format)
357357
358358 if s.simpleSpinner != nil {
359 for _, block := range [...][]byte{fmtBytes[rLeft], {s.simpleSpinner()}, fmtBytes[rRight]} {
359 for _, block := range [...][]byte{segments[rLeft], {s.simpleSpinner()}, segments[rRight]} {
360360 barBlock = append(barBlock, block...)
361361 }
362 return concatenateBlocks(buf, prependBlock, leftSpace, barBlock, rightSpace, appendBlock)
363 }
364
365 barBlock = fillBar(s.total, s.current, s.width, fmtBytes, s.refill)
366 barCount := utf8.RuneCount(barBlock)
367 totalCount := prependCount + barCount + appendCount
368 if totalCount > termWidth {
369 shrinkWidth := termWidth - prependCount - appendCount
370 barBlock = fillBar(s.total, s.current, shrinkWidth, fmtBytes, s.refill)
362 } else {
363 barBlock = fillBar(s.total, s.current, s.width, segments, s.refill)
364 barCount := utf8.RuneCount(barBlock)
365 totalCount := prependCount + barCount + appendCount
366 if totalCount > termWidth {
367 shrinkWidth := termWidth - prependCount - appendCount
368 barBlock = fillBar(s.total, s.current, shrinkWidth, segments, s.refill)
369 }
371370 }
372371
373372 return concatenateBlocks(buf, prependBlock, leftSpace, barBlock, rightSpace, appendBlock)
380379 return buf
381380 }
382381
383 func fillBar(total, current int64, width int, fmtBytes barFmtBytes, rf *refill) []byte {
382 func fillBar(total, current int64, width int, fmtBytes fmtByteSegments, rf *refill) []byte {
384383 if width < 2 || total <= 0 {
385384 return []byte{}
386385 }
438437 }
439438 }
440439
441 func convertFmtRunesToBytes(format barFmtRunes) barFmtBytes {
442 var fmtBytes barFmtBytes
440 func fmtRunesToByteSegments(format fmtRunes) fmtByteSegments {
441 var segments fmtByteSegments
443442 for i, r := range format {
444443 buf := make([]byte, utf8.RuneLen(r))
445444 utf8.EncodeRune(buf, r)
446 fmtBytes[i] = buf
447 }
448 return fmtBytes
445 segments[i] = buf
446 }
447 return segments
449448 }
450449
451450 func getSpinner() func() byte {