-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatusbar.go
326 lines (283 loc) · 7.3 KB
/
statusbar.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
package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/encoding"
"github.com/mattn/go-runewidth"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
"sort"
)
var defStyle tcell.Style
var cheatDays int
func bar_path() string {
args := os.Args[1]
return filepath.Join(args)
}
func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
}
func days_since(s string) int {
const layout = "2006-01-02"
t, _ := time.Parse(layout, s)
return int((time.Since(t)).Hours() / (24))
}
func add_day(s string) string {
const layout = "2006-01-02"
t, _ := time.Parse(layout, s)
newT := t.AddDate(0, 0, 1)
return newT.Format(layout)
}
func sub_day(s string) string {
const layout = "2006-01-02"
t, _ := time.Parse(layout, s)
newT := t.AddDate(0, 0, -1)
return newT.Format(layout)
}
func insert(a []map[string]interface{}, index int, value map[string]interface{}) []map[string]interface{} {
if len(a) == index {
return append(a, value)
}
a = append(a[:index+1], a[index:]...)
a[index] = value
return a
}
func sort_bars(bars []map[string]interface{}) []map[string]interface{} {
// Define a custom comparison function
sort.Slice(bars, func(i, j int) bool {
// Get the necessary values for the first element
startDateI := bars[i]["start_date"].(string)
lengthI := bars[i]["length"].(int)
nameI := bars[i]["name"].(string)
// Get the necessary values for the second element
startDateJ := bars[j]["start_date"].(string)
lengthJ := bars[j]["length"].(int)
nameJ := bars[j]["name"].(string)
// Calculate the sorting key
diffI := days_since(startDateI) - lengthI
diffJ := days_since(startDateJ) - lengthJ
// Compare the sorting keys
if diffI == diffJ {
return nameI < nameJ
}
return diffI > diffJ
})
return bars
}
func render_bars(s tcell.Screen, max_bar_length int, bars []map[string]interface{}) {
green := tcell.StyleDefault.Foreground(tcell.ColorLawnGreen)
yellow := tcell.StyleDefault.Foreground(tcell.Color184)
orange := tcell.StyleDefault.Foreground(tcell.ColorDarkOrange)
red := tcell.StyleDefault.Foreground(tcell.ColorRed)
blue := tcell.StyleDefault.Foreground(tcell.ColorBlue)
theme := []string{"█", " ", "|", "▐", "▀", "▄" }
index := 0
maxBarLength := max_bar_length
cheatString := fmt.Sprintf("Cheat + | -%s", strings.Repeat(" " + theme[4], cheatDays))
emitStr(s, 1, index, blue, cheatString)
for _, el := range bars {
length := el["length"].(int)
inc := el["inc"].(int)
days_since := days_since(el["start_date"].(string))
day_bar := days_since % maxBarLength
overflow := length / maxBarLength
bar_length := length % maxBarLength
barString := fmt.Sprintf("\r %s%s%s",
theme[3],
strings.Repeat(theme[0], bar_length),
" + | -",
)
errorBarString := fmt.Sprintf("\r %s%s",
theme[3],
strings.Repeat(theme[0], day_bar),
)
name_string := fmt.Sprintf("\r %s (%v) ", el["name"].(string), length)
var bar_color = green
if days_since <= length {
bar_color = green
} else {
if (days_since - length) < 5 {
bar_color = yellow
} else if (days_since - length) < 10 {
bar_color = orange
} else {
bar_color = red
}
}
inc_string := fmt.Sprintf(" (+-%d)", inc)
day_string := fmt.Sprintf(" %v day(s)", days_since)
medal_string := strings.Repeat(" " + theme[4], overflow)
emitStr(s, 2, index+2, bar_color, name_string)
emitStr(s, len(name_string)+1, index+2, bar_color, inc_string)
emitStr(s, len(inc_string)+len(name_string)+1, index+2, blue, day_string)
emitStr(s, len(inc_string)+len(name_string)+len(day_string)+1, index+2, yellow, medal_string)
emitStr(s, 2, index+3, bar_color, fmt.Sprintf(barString))
emitStr(s, 2, index+4, blue, fmt.Sprintf(errorBarString))
index += 4
}
}
func save_bars(bars []map[string]interface{}) {
data := make(map[string]interface{})
data["bars"] = bars
data["cheat_days"] = cheatDays
d, err := yaml.Marshal(data)
if err != nil {
log.Fatalf("error: %v", err)
}
f, err := os.Create(bar_path())
if err != nil {
log.Fatalf("error: %v", err)
}
f.Write(d)
}
func inc_dec_bars(max_bar_length int, x int, y int, bars []map[string]interface{}) []map[string]interface{} {
new_bars := []map[string]interface{}{}
if y == 0 {
if x > 9 {
for _, el := range bars {
date := el["start_date"].(string)
el["start_date"] = sub_day(date)
new_bars = append(new_bars, el)
}
if cheatDays > 0 { // Ensure cheatDays doesn't go below 0
cheatDays--
}
} else {
for _, el := range bars {
date := el["start_date"].(string)
el["start_date"] = add_day(date)
new_bars = append(new_bars, el)
}
cheatDays++
}
} else {
for i, el := range bars {
if 4*(i+1)-1 == y {
length := el["length"].(int)
bar_length := length % max_bar_length
inc := el["inc"].(int)
if x <= (bar_length + 6) {
new_length := el["length"].(int) + inc
el["length"] = new_length
new_bars = append(new_bars, el)
} else if x > (bar_length + 6) {
new_length := el["length"].(int) - inc
if new_length < 0 {
el["length"] = 0
} else {
el["length"] = new_length
}
new_bars = append(new_bars, el)
} else {
new_bars = append(new_bars, el)
}
} else {
new_bars = append(new_bars, el)
}
}
}
save_bars(new_bars)
return new_bars
}
func get_bars() ([]map[string]interface{}, error) {
yamlFile, err := ioutil.ReadFile(bar_path())
type BarConfig struct {
Bars []map[string]interface{}
CheatDays int `yaml:"cheat_days"`
}
bars := BarConfig{}
if err == nil {
err = yaml.Unmarshal(yamlFile, &bars)
}
cheatDays = bars.CheatDays
return bars.Bars, err
}
func main() {
s, e := tcell.NewScreen()
encoding.Register()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
if e := s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
defStyle = tcell.StyleDefault.
Background(tcell.ColorReset).
Foreground(tcell.ColorReset)
s.SetStyle(defStyle)
s.EnableMouse()
s.EnablePaste()
s.Clear()
max_bar_length := 30
bars, err := get_bars()
ecnt := 0
last_press := time.Now().AddDate(-1, 0, 0)
if err != nil {
s.Fini()
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(0)
}
render_bars(s, max_bar_length, bars)
s.Show()
go func() {
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape {
ecnt++
if ecnt > 1 {
s.Fini()
os.Exit(0)
}
}
case *tcell.EventMouse:
x, y := ev.Position()
switch ev.Buttons() {
case tcell.Button1, tcell.Button2, tcell.Button3:
s.Clear()
new_bars, _ := get_bars()
if time.Now().Sub(last_press).Seconds() > 0.5 {
new_bars = inc_dec_bars(max_bar_length, x, y, new_bars)
last_press = time.Now()
} else {
new_bars, _ = get_bars()
}
render_bars(s, max_bar_length, new_bars)
s.Sync()
s.Show()
}
}
}
}()
t := time.NewTicker(time.Second * 10)
for {
select {
case <-t.C:
new_bars, _ := get_bars()
sorted_bars := sort_bars(new_bars)
save_bars(sorted_bars)
s.Clear()
render_bars(s, max_bar_length, new_bars)
s.Sync()
s.Show()
}
}
}