forked from wtfutil/wtf
/
bargraph.go
172 lines (129 loc) · 3.83 KB
/
bargraph.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
package wtf
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/rivo/tview"
)
//BarGraph lets make graphs
type BarGraph struct {
enabled bool
focusable bool
starChar string
maxStars int
Name string
RefreshedAt time.Time
RefreshInt int
View *tview.TextView
Position
Data [][2]int64
}
// NewBarGraph initialize your fancy new graph
func NewBarGraph(name string, configKey string, focusable bool) BarGraph {
widget := BarGraph{
enabled: Config.UBool(fmt.Sprintf("wtf.mods.%s.enabled", configKey), false),
focusable: focusable,
starChar: Config.UString(fmt.Sprintf("wtf.mods.%s.graphIcon", configKey), name),
maxStars: Config.UInt(fmt.Sprintf("wtf.mods.%s.graphStars", configKey), 20),
Name: Config.UString(fmt.Sprintf("wtf.mods.%s.title", configKey), name),
RefreshInt: Config.UInt(fmt.Sprintf("wtf.mods.%s.refreshInterval", configKey)),
}
widget.Position = NewPosition(
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.top", configKey)),
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.left", configKey)),
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.width", configKey)),
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.height", configKey)),
)
widget.addView()
return widget
}
func (widget *BarGraph) BorderColor() string {
if widget.Focusable() {
return Config.UString("wtf.colors.border.focusable", "red")
}
return Config.UString("wtf.colors.border.normal", "gray")
}
func (widget *BarGraph) Disable() {
widget.enabled = false
}
func (widget *BarGraph) Disabled() bool {
return !widget.Enabled()
}
func (widget *BarGraph) Enabled() bool {
return widget.enabled
}
func (widget *BarGraph) Focusable() bool {
return widget.enabled && widget.focusable
}
func (widget *BarGraph) RefreshInterval() int {
return widget.RefreshInt
}
func (widget *BarGraph) TextView() *tview.TextView {
return widget.View
}
/* -------------------- Unexported Functions -------------------- */
func (widget *BarGraph) UpdateRefreshedAt() {
widget.RefreshedAt = time.Now()
}
func (widget *BarGraph) addView() {
view := tview.NewTextView()
view.SetBackgroundColor(ColorFor(Config.UString("wtf.colors.background", "black")))
view.SetBorder(true)
view.SetBorderColor(ColorFor(widget.BorderColor()))
view.SetDynamicColors(true)
view.SetTitle(widget.Name)
view.SetWrap(false)
widget.View = view
}
// BuildBars will build a string of * to represent your data of [time][value]
// time should be passed as a int64
func (widget *BarGraph) BuildBars(data [][2]int64) {
widget.View.SetText(BuildStars(data, widget.maxStars, widget.starChar))
}
//BuildStars build the string to display
func BuildStars(data [][2]int64, maxStars int, starChar string) string {
var buffer bytes.Buffer
//counter to inintialize min value
var count int
//store the max value from the array
var maxValue int
//store the min value from the array
var minValue int
//just getting min and max values
for i := range data {
var val = int(data[i][0])
//initialize the min value
if count == 0 {
minValue = val
}
count++
//update max value
if val > maxValue {
maxValue = val
}
//update minValue
if val < minValue {
minValue = val
}
}
// each number = how many stars?
var starRatio = float64(maxStars) / float64((maxValue - minValue))
//build the stars
for i := range data {
var val = int(data[i][0])
//how many stars for this one?
var starCount = int(float64((val - minValue)) * starRatio)
if starCount == 0 {
starCount = 1
}
//build the actual string
var stars = strings.Repeat(starChar, starCount)
//parse the time
var t = time.Unix(int64(data[i][1]/1000), 0)
//write the line
buffer.WriteString(fmt.Sprintf("%s -\t [red]%s[white] - (%d)\n", t.Format("Jan 02, 2006"), stars, val))
}
return buffer.String()
}
/* -------------------- Exported Functions -------------------- */