Format the progress bar as output
(state fmt.State, r rune)
| 34 | |
| 35 | // Format the progress bar as output |
| 36 | func (h Bar) Format(state fmt.State, r rune) { |
| 37 | switch r { |
| 38 | case 'r': |
| 39 | default: |
| 40 | panic(fmt.Sprintf("%v: unexpected format character", float64(h))) |
| 41 | } |
| 42 | |
| 43 | if h > 1.0 { |
| 44 | h = 1.0 |
| 45 | } |
| 46 | |
| 47 | if h < 0.0 { |
| 48 | h = 0.0 |
| 49 | } |
| 50 | |
| 51 | if state.Flag('-') { |
| 52 | h = 1.0 - h |
| 53 | } |
| 54 | |
| 55 | width, ok := state.Width() |
| 56 | if !ok { |
| 57 | // default width of 40 |
| 58 | width = 40 |
| 59 | } |
| 60 | |
| 61 | var pad int |
| 62 | |
| 63 | extra := len([]byte(green)) + len([]byte(reset)) |
| 64 | |
| 65 | p := make([]byte, width+extra) |
| 66 | p[0], p[len(p)-1] = '|', '|' |
| 67 | pad += 2 |
| 68 | |
| 69 | positive := int(Bar(width-pad) * h) |
| 70 | negative := width - pad - positive |
| 71 | |
| 72 | n := 1 |
| 73 | n += copy(p[n:], green) |
| 74 | n += copy(p[n:], bytes.Repeat([]byte("+"), positive)) |
| 75 | n += copy(p[n:], reset) |
| 76 | |
| 77 | if negative > 0 { |
| 78 | copy(p[n:len(p)-1], bytes.Repeat([]byte("-"), negative)) |
| 79 | } |
| 80 | |
| 81 | state.Write(p) |
| 82 | } |