-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBar.go
143 lines (113 loc) · 3.16 KB
/
Bar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package percbar
import (
"fmt"
"math"
"sort"
"strings"
"github.com/fatih/color"
"golang.org/x/exp/constraints"
)
// Bar is filled with sectors
type Bar struct {
sectors sectors
options Options
// processed
chars []rune
colors []*color.Color
sum float64
// cached
cache string
}
// Number -- (c) https://stackoverflow.com/questions/67678331/how-to-write-a-generic-function-that-accepts-any-numerical-type
type Number interface {
constraints.Integer | constraints.Float
}
// New bar as exported func
func New[T Number](values map[string]T) *Bar {
bar := &Bar{
options: *OptionsDefault,
}
for label, count := range values {
bar.sectors = append(bar.sectors, newSector(label, float64(count)))
bar.sum += float64(count)
}
sort.Sort(bar.sectors)
return bar
}
// SetOptions - chain func to apply options
func (bar *Bar) SetOptions(opt *Options) *Bar {
bar.options = *opt
bar.cache = ""
return bar
}
// func (bar *Bar) useBlock(i int) *CharBlock {
// return bar.options.Blocks[i%len(bar.options.Blocks)]
// }
// String to Stringer interface
// Example: fmt.Println(bar)
func (bar *Bar) String() string {
if bar.cache != "" {
return bar.cache
}
if bar.options.Chars == "" {
bar.options.Chars = string([]rune(OptionsDefault.Chars)[0])
}
// chars to runes
for _, c := range bar.options.Chars {
bar.chars = append(bar.chars, c)
}
bar.colors = textToColors(bar.options.Colors)
var s string
var footer string
// loop and make this bar
for i, sector := range bar.sectors {
sector.char = bar.chars[i%len(bar.chars)]
sector.color = bar.colors[i%len(bar.colors)]
sector.percents = int(math.Floor((sector.count / bar.sum) * 100.0))
sectOut := strings.Repeat(string(sector.char), sector.percents)
// if there is no colors mode (or same color) and same block used
// use separator to know where sectors begin/end.
useSeparator := i > 0 && sector.percents > 0
useSeparator = useSeparator && sector.color == bar.sectors[i-1].color
useSeparator = useSeparator && sector.char == bar.sectors[i-1].char
if useSeparator {
sectOut = " " + strings.Repeat(string(sector.char), sector.percents-1)
}
// [MyLabel 34% (234)]
labelParts := []string{
" " + sector.label, // MyLabel
fmt.Sprintf("%d%%", sector.percents), // 34%
fmt.Sprintf("(%.0f)", sector.count), // (234)
}
// choose longest possible for display
sectFooter := ""
for i := len(labelParts); i >= 0; i-- {
if label := strings.Join(labelParts[:i], " "); len(label) < sector.percents {
sectFooter = label + strings.Repeat(" ", sector.percents-len(label))
break
}
}
if sector.color != nil {
sectOut = sector.color.Sprintf("%s", sectOut)
}
if bar.options.AllowFooterColors {
sectFooter = sector.color.Sprintf("%s", sectFooter)
}
s += sectOut
if bar.options.HaveFooter {
footer += sectFooter
}
}
// prepend if necessary
if header := bar.options.Header; header != "" {
ssum := fmt.Sprintf("%.2f", bar.sum)
ssum = strings.TrimSuffix(ssum, ".00")
header = strings.ReplaceAll(header, "{SUM}", ssum)
s = fmt.Sprintf(" %s\n%s\n", header, s)
}
if footer != "" {
s += footer + "\n"
}
// ready for use
return s
}