-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
legend.go
45 lines (39 loc) · 1.05 KB
/
legend.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
package asciigraph
import (
"bytes"
"fmt"
"strings"
"unicode/utf8"
)
// Create legend item as a colored box and text
func createLegendItem(text string, color AnsiColor) (string, int) {
return fmt.Sprintf(
"%s■%s %s",
color.String(),
Default.String(),
text,
),
// Can't use len() because of AnsiColor, add 2 for box and space
utf8.RuneCountInString(text) + 2
}
// Add legend for each series added to the graph
func addLegends(lines *bytes.Buffer, config *config, lenMax int, leftPad int) {
lines.WriteString("\n\n")
lines.WriteString(strings.Repeat(" ", leftPad))
var legendsText string
var legendsTextLen int
rightPad := 3
for i, text := range config.SeriesLegends {
item, itemLen := createLegendItem(text, config.SeriesColors[i])
legendsText += item
legendsTextLen += itemLen
if i < len(config.SeriesLegends)-1 {
legendsText += strings.Repeat(" ", rightPad)
legendsTextLen += rightPad
}
}
if legendsTextLen < lenMax {
lines.WriteString(strings.Repeat(" ", (lenMax-legendsTextLen)/2))
}
lines.WriteString(legendsText)
}