forked from johnmikee/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
barchart.go
424 lines (351 loc) · 12.9 KB
/
barchart.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package pterm
import (
"io"
"strconv"
"strings"
"github.com/mattn/go-runewidth"
"github.com/gemini/pterm/internal"
)
// BarChartPrinter is used to print bar charts.
type BarChartPrinter struct {
Writer io.Writer
Bars Bars
Horizontal bool
ShowValue bool
// Height sets the maximum height of a vertical bar chart.
// The default is calculated to fit into the terminal.
// Ignored if Horizontal is set to true.
Height int
// Width sets the maximum width of a horizontal bar chart.
// The default is calculated to fit into the terminal.
// Ignored if Horizontal is set to false.
Width int
VerticalBarCharacter string
HorizontalBarCharacter string
}
var (
// DefaultBarChart is the default BarChartPrinter.
DefaultBarChart = BarChartPrinter{
Horizontal: false,
VerticalBarCharacter: "██",
HorizontalBarCharacter: "█",
// keep in sync with RecalculateTerminalSize()
Height: GetTerminalHeight() * 2 / 3,
Width: GetTerminalWidth() * 2 / 3,
}
)
// WithBars returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithBars(bars Bars) *BarChartPrinter {
p.Bars = bars
return &p
}
// WithVerticalBarCharacter returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithVerticalBarCharacter(char string) *BarChartPrinter {
p.VerticalBarCharacter = char
return &p
}
// WithHorizontalBarCharacter returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithHorizontalBarCharacter(char string) *BarChartPrinter {
p.HorizontalBarCharacter = char
return &p
}
// WithHorizontal returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithHorizontal(b ...bool) *BarChartPrinter {
b2 := internal.WithBoolean(b)
p.Horizontal = b2
return &p
}
// WithHeight returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithHeight(value int) *BarChartPrinter {
p.Height = value
return &p
}
// WithWidth returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithWidth(value int) *BarChartPrinter {
p.Width = value
return &p
}
// WithShowValue returns a new BarChartPrinter with a specific option.
func (p BarChartPrinter) WithShowValue(b ...bool) *BarChartPrinter {
p.ShowValue = internal.WithBoolean(b)
return &p
}
// WithWriter sets the custom Writer.
func (p BarChartPrinter) WithWriter(writer io.Writer) *BarChartPrinter {
p.Writer = writer
return &p
}
func (p BarChartPrinter) getRawOutput() string {
var ret string
for _, bar := range p.Bars {
ret += Sprintfln("%s: %d", bar.Label, bar.Value)
}
return ret
}
// Srender renders the BarChart as a string.
func (p BarChartPrinter) Srender() (string, error) {
maxAbsValue := func(value1 int, value2 int) int {
min := value1
max := value2
if value1 > value2 {
min = value2
max = value1
}
maxAbs := max
if min < 0 && -min > max { // This is to avoid something like "int(math.Abs(float64(minBarValue)))"
maxAbs = -min // (--) == (+)
}
return maxAbs
}
abs := func(value int) int {
if value < 0 {
return -value
}
return value
}
// =================================== VERTICAL BARS RENDERER ======================================================
type renderParams struct {
repeatCount int
bar Bar
positiveChartPartHeight int
negativeChartPartHeight int
positiveChartPartWidth int
negativeChartPartWidth int
indent string
showValue bool
moveUp bool
moveRight bool
}
renderPositiveVerticalBar := func(renderedBarRef *string, rParams renderParams) {
if rParams.showValue {
*renderedBarRef += Sprint(rParams.indent + strconv.Itoa(rParams.bar.Value) + rParams.indent + "\n")
}
for i := rParams.positiveChartPartHeight; i > 0; i-- {
if i > rParams.repeatCount {
*renderedBarRef += rParams.indent + " " + rParams.indent + " \n"
} else {
*renderedBarRef += rParams.indent + rParams.bar.Style.Sprint(p.VerticalBarCharacter) + rParams.indent + " \n"
}
}
// Used when we draw diagram with both POSITIVE and NEGATIVE values.
// In such case we separately draw top and bottom half of chart.
// And we need MOVE UP positive part to top part of chart,
// technically by adding empty pillars with height == height of chart's bottom part.
if rParams.moveUp {
for i := 0; i <= rParams.negativeChartPartHeight; i++ {
*renderedBarRef += rParams.indent + " " + rParams.indent + " \n"
}
}
}
renderNegativeVerticalBar := func(renderedBarRef *string, rParams renderParams) {
for i := 0; i > -rParams.negativeChartPartHeight; i-- {
if i > rParams.repeatCount {
*renderedBarRef += rParams.indent + rParams.bar.Style.Sprint(p.VerticalBarCharacter) + rParams.indent + " \n"
} else {
*renderedBarRef += rParams.indent + " " + rParams.indent + " \n"
}
}
if rParams.showValue {
*renderedBarRef += Sprint(rParams.indent + strconv.Itoa(rParams.bar.Value) + rParams.indent + "\n")
}
}
// =================================== HORIZONTAL BARS RENDERER ====================================================
renderPositiveHorizontalBar := func(renderedBarRef *string, rParams renderParams) {
if rParams.moveRight {
for i := 0; i < rParams.negativeChartPartWidth; i++ {
*renderedBarRef += " "
}
}
for i := 0; i < rParams.positiveChartPartWidth; i++ {
if i < rParams.repeatCount {
*renderedBarRef += rParams.bar.Style.Sprint(p.HorizontalBarCharacter)
} else {
*renderedBarRef += " "
}
}
if rParams.showValue {
// For positive horizontal bars we add one more space before adding value,
// so they will be well aligned with negative values, which have "-" sign before them
*renderedBarRef += " "
*renderedBarRef += " " + strconv.Itoa(rParams.bar.Value)
}
}
renderNegativeHorizontalBar := func(renderedBarRef *string, rParams renderParams) {
for i := -rParams.negativeChartPartWidth; i < 0; i++ {
if i < rParams.repeatCount {
*renderedBarRef += " "
} else {
*renderedBarRef += rParams.bar.Style.Sprint(p.HorizontalBarCharacter)
}
}
// In order to print values well-aligned (in case when we have both - positive and negative part of chart),
// we should insert an indent with width == width of positive chart part
if rParams.positiveChartPartWidth > 0 {
for i := 0; i < rParams.positiveChartPartWidth; i++ {
*renderedBarRef += " "
}
}
if rParams.showValue {
/*
This is in order to achieve this effect:
0
-15
0
-19
INSTEAD OF THIS:
0
-15
0
-19
*/
if rParams.repeatCount == 0 {
*renderedBarRef += " "
}
*renderedBarRef += " " + strconv.Itoa(rParams.bar.Value)
}
}
// =================================================================================================================
if RawOutput {
return p.getRawOutput(), nil
}
for i, bar := range p.Bars {
if bar.Style == nil {
p.Bars[i].Style = &ThemeDefault.BarStyle
}
if bar.LabelStyle == nil {
p.Bars[i].LabelStyle = &ThemeDefault.BarLabelStyle
}
p.Bars[i].Label = p.Bars[i].LabelStyle.Sprint(bar.Label)
}
var ret string
var maxLabelHeight int
var maxBarValue int
var minBarValue int
var maxAbsBarValue int
var rParams renderParams
for _, bar := range p.Bars {
if bar.Value > maxBarValue {
maxBarValue = bar.Value
}
if bar.Value < minBarValue {
minBarValue = bar.Value
}
labelHeight := len(strings.Split(bar.Label, "\n"))
if labelHeight > maxLabelHeight {
maxLabelHeight = labelHeight
}
}
maxAbsBarValue = maxAbsValue(maxBarValue, minBarValue)
if p.Horizontal {
panels := Panels{[]Panel{{}, {}}}
rParams.showValue = p.ShowValue
rParams.positiveChartPartWidth = p.Width
rParams.negativeChartPartWidth = p.Width
// If chart will consist of two parts - positive and negative - we should recalculate max bars WIDTH in LEFT and RIGHT parts
if minBarValue < 0 && maxBarValue > 0 {
rParams.positiveChartPartWidth = abs(internal.MapRangeToRange(-float32(maxAbsBarValue), float32(maxAbsBarValue), -float32(p.Width)/2, float32(p.Width)/2, float32(maxBarValue)))
rParams.negativeChartPartWidth = abs(internal.MapRangeToRange(-float32(maxAbsBarValue), float32(maxAbsBarValue), -float32(p.Width)/2, float32(p.Width)/2, float32(minBarValue)))
}
for _, bar := range p.Bars {
rParams.bar = bar
panels[0][0].Data += "\n" + bar.Label
panels[0][1].Data += "\n"
if minBarValue >= 0 {
// As we don't have negative values, draw only positive (right) part of the chart:
rParams.repeatCount = internal.MapRangeToRange(0, float32(maxAbsBarValue), 0, float32(p.Width), float32(bar.Value))
rParams.moveRight = false
renderPositiveHorizontalBar(&panels[0][1].Data, rParams)
} else if maxBarValue <= 0 {
// As we have only negative values, draw only negative (left) part of the chart:
rParams.repeatCount = internal.MapRangeToRange(-float32(maxAbsBarValue), 0, -float32(p.Width), 0, float32(bar.Value))
rParams.positiveChartPartWidth = 0
renderNegativeHorizontalBar(&panels[0][1].Data, rParams)
} else {
// We have positive and negative values, so draw both (left+right) parts of the chart:
rParams.repeatCount = internal.MapRangeToRange(-float32(maxAbsBarValue), float32(maxAbsBarValue), -float32(p.Width)/2, float32(p.Width)/2, float32(bar.Value))
if bar.Value >= 0 {
rParams.moveRight = true
renderPositiveHorizontalBar(&panels[0][1].Data, rParams)
}
if bar.Value < 0 {
renderNegativeHorizontalBar(&panels[0][1].Data, rParams)
}
}
}
ret, _ = DefaultPanel.WithPanels(panels).Srender()
return ret, nil
} else {
renderedBars := make([]string, len(p.Bars))
rParams.showValue = p.ShowValue
rParams.positiveChartPartHeight = p.Height
rParams.negativeChartPartHeight = p.Height
// If chart will consist of two parts - positive and negative - we should recalculate max bars height in top and bottom parts
if minBarValue < 0 && maxBarValue > 0 {
rParams.positiveChartPartHeight = abs(internal.MapRangeToRange(-float32(maxAbsBarValue), float32(maxAbsBarValue), -float32(p.Height)/2, float32(p.Height)/2, float32(maxBarValue)))
rParams.negativeChartPartHeight = abs(internal.MapRangeToRange(-float32(maxAbsBarValue), float32(maxAbsBarValue), -float32(p.Height)/2, float32(p.Height)/2, float32(minBarValue)))
}
for i, bar := range p.Bars {
var renderedBar string
rParams.bar = bar
rParams.indent = strings.Repeat(" ", internal.GetStringMaxWidth(RemoveColorFromString(bar.Label))/2)
if minBarValue >= 0 {
// As we don't have negative values, draw only positive (top) part of the chart:
rParams.repeatCount = internal.MapRangeToRange(0, float32(maxAbsBarValue), 0, float32(p.Height), float32(bar.Value))
rParams.moveUp = false // Don't MOVE UP as we have ONLY positive part of chart.
renderPositiveVerticalBar(&renderedBar, rParams)
} else if maxBarValue <= 0 {
// As we have only negative values, draw only negative (bottom) part of the chart:
rParams.repeatCount = internal.MapRangeToRange(-float32(maxAbsBarValue), 0, -float32(p.Height), 0, float32(bar.Value))
renderNegativeVerticalBar(&renderedBar, rParams)
} else {
// We have positive and negative values, so draw both (top+bottom) parts of the chart:
rParams.repeatCount = internal.MapRangeToRange(-float32(maxAbsBarValue), float32(maxAbsBarValue), -float32(p.Height)/2, float32(p.Height)/2, float32(bar.Value))
if bar.Value >= 0 {
rParams.moveUp = true // MOVE UP positive part, because we have both positive and negative parts of chart.
renderPositiveVerticalBar(&renderedBar, rParams)
}
if bar.Value < 0 {
renderNegativeVerticalBar(&renderedBar, rParams)
}
}
labelHeight := len(strings.Split(bar.Label, "\n"))
renderedBars[i] = renderedBar + bar.Label + strings.Repeat("\n", maxLabelHeight-labelHeight) + " "
}
var maxBarHeight int
for _, bar := range renderedBars {
totalBarHeight := len(strings.Split(bar, "\n"))
if totalBarHeight > maxBarHeight {
maxBarHeight = totalBarHeight
}
}
for i, bar := range renderedBars {
totalBarHeight := len(strings.Split(bar, "\n"))
if totalBarHeight < maxBarHeight {
renderedBars[i] = strings.Repeat("\n", maxBarHeight-totalBarHeight) + renderedBars[i]
}
}
for i := 0; i <= maxBarHeight; i++ {
for _, barString := range renderedBars {
var barLine string
letterLines := strings.Split(barString, "\n")
maxBarWidth := internal.GetStringMaxWidth(RemoveColorFromString(barString))
if len(letterLines) > i {
barLine = letterLines[i]
}
letterLineLength := runewidth.StringWidth(RemoveColorFromString(barLine))
if letterLineLength < maxBarWidth {
barLine += strings.Repeat(" ", maxBarWidth-letterLineLength)
}
ret += barLine
}
ret += "\n"
}
}
return ret, nil
}
// Render prints the Template to the terminal.
func (p BarChartPrinter) Render() error {
s, _ := p.Srender()
Fprintln(p.Writer, s)
return nil
}