Some refactoring and better names
Vladimir Bauer
8 years ago
| 22 | 22 | etaAlpha = 0.25 |
| 23 | 23 | ) |
| 24 | 24 | |
| 25 | type barFmtRunes [formatLen]rune | |
| 26 | type barFmtBytes [formatLen][]byte | |
| 25 | type fmtRunes [formatLen]rune | |
| 26 | type fmtByteSegments [formatLen][]byte | |
| 27 | 27 | |
| 28 | 28 | // Bar represents a progress Bar |
| 29 | 29 | type Bar struct { |
| 45 | 45 | state struct { |
| 46 | 46 | id int |
| 47 | 47 | width int |
| 48 | format barFmtRunes | |
| 48 | format fmtRunes | |
| 49 | 49 | etaAlpha float64 |
| 50 | 50 | total int64 |
| 51 | 51 | current int64 |
| 353 | 353 | |
| 354 | 354 | var barBlock []byte |
| 355 | 355 | buf := make([]byte, 0, termWidth) |
| 356 | fmtBytes := convertFmtRunesToBytes(s.format) | |
| 356 | segments := fmtRunesToByteSegments(s.format) | |
| 357 | 357 | |
| 358 | 358 | 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]} { | |
| 360 | 360 | barBlock = append(barBlock, block...) |
| 361 | 361 | } |
| 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 | } | |
| 371 | 370 | } |
| 372 | 371 | |
| 373 | 372 | return concatenateBlocks(buf, prependBlock, leftSpace, barBlock, rightSpace, appendBlock) |
| 380 | 379 | return buf |
| 381 | 380 | } |
| 382 | 381 | |
| 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 { | |
| 384 | 383 | if width < 2 || total <= 0 { |
| 385 | 384 | return []byte{} |
| 386 | 385 | } |
| 438 | 437 | } |
| 439 | 438 | } |
| 440 | 439 | |
| 441 | func convertFmtRunesToBytes(format barFmtRunes) barFmtBytes { | |
| 442 | var fmtBytes barFmtBytes | |
| 440 | func fmtRunesToByteSegments(format fmtRunes) fmtByteSegments { | |
| 441 | var segments fmtByteSegments | |
| 443 | 442 | for i, r := range format { |
| 444 | 443 | buf := make([]byte, utf8.RuneLen(r)) |
| 445 | 444 | utf8.EncodeRune(buf, r) |
| 446 | fmtBytes[i] = buf | |
| 447 | } | |
| 448 | return fmtBytes | |
| 445 | segments[i] = buf | |
| 446 | } | |
| 447 | return segments | |
| 449 | 448 | } |
| 450 | 449 | |
| 451 | 450 | func getSpinner() func() byte { |