forked from vlang/ved
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.v
186 lines (169 loc) · 3.99 KB
/
timer.v
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
module main
import freetype
import gg
import gx
import os
import time
const (
time_cfg = gx.TextCfg {
color: gx.gray
size: 5
}
)
const (
color_distracting = gx.rgb(255,111,130)
color_productive = //gx.rgb(167,236,82)
gx.rgb(50, 90, 110)
color_neutral = gx.rgb(39,195,221)
)
struct Timer {
gg &gg.GG
ft &freetype.FreeType
mut:
tasks []Task
date time.Time
}
struct Task {
start int
end int
name string
color gx.Color
duration string
duration_min int
productive bool
}
fn (mut t Timer) load_tasks() {
//println('timer.load_tasks()')
lines := os.read_lines(tasks_path) or { return }
//println(lines)
mut tasks := []Task{}
today := t.date.ymmdd()
//println('day=$today')
for line in lines {
//println(line)
if !line.contains('|') {
continue
}
if !line.contains(today) {
continue
}
words_ := line.split('|')
words := words_.filter(it != '')
//println('wordss:')
//println(words)
if words.len != 4 {
continue
}
time := words[2].trim_space()
//println('time=$time')
a := time.split(' ')
//println('a=') println(a)
if a.len < 2 {
continue
}
b := a[1].split(':')
//println('b=') println(b)
if b.len < 2 {
continue
}
hour := b[0].int()
min := b[1].int()
end_time := words[3]
hhmm := end_time.split(':')
hour_end := hhmm[0].trim_space().int()
min_end := hhmm[1].trim_space().int()
name := words[0].trim_space()
duration:=words[1].trim_space()
productive := !name.starts_with('@')
task := Task {
start: hour * 60 + min
end: hour_end * 60 + min_end
name: if productive { name } else { name[1..] }
duration: duration
duration_min: duration[..duration.len-1].int()
color: if productive { color_productive } else { color_distracting }
productive: productive
}
//println('task:')
//println(task)
if task.end < task.start {
continue
}
tasks << task
}
//println('tasks.len=$tasks.len')
t.tasks = tasks
}
fn new_timer(gg &gg.GG, ft &freetype.FreeType) Timer {
mut timer := Timer {
gg: gg
ft: ft
date: time.now()
}
timer.load_tasks()
return timer
}
const (
hour_height = 30.0
//scale = 3
)
//fn (mut t Timer) load_tasks() {
//}
fn (mut t Timer) draw() {
window_width := t.gg.width/2
window_height := t.gg.height-20
window_x := (t.gg.width - window_width) / 2
window_y := (t.gg.height - window_height) / 2
t.gg.draw_rect(window_x, window_y, window_width, window_height, gx.white)
hour_width := window_height / 24 //window_width / 25// 60 / scale // 60 min
scale := 60.0 / f64(hour_width)
mut total := 0
for task in t.tasks {
//println('TASK $task')
if task.duration.len < 3 {
continue
}
x := f64(window_x) + 30.0
y := f64(window_y) + f64(task.start) / scale + 10
height := f64(task.end - task.start) / scale
t.gg.draw_rect(x, y, hour_width, height , task.color)
t.ft.draw_text(int(x)+hour_width + 10, int(y)+5, task.name + ' ' + task.duration, gx.TextCfg{ color: task.color })
if task.productive {
total += task.duration_min
}
}
for hour in 0 .. 24 + 1 {
hour_y := window_y + hour * hour_width + 10
hour_x := window_x + 30
if hour < 24 {
t.ft.draw_text(hour_x - 25, hour_y + 10,
'${hour:02d}', time_cfg)
}
t.gg.draw_line(hour_x, hour_y, hour_x + hour_width, hour_y, gx.gray)
}
// Large left vertical line
t.gg.draw_line(window_x + 30, window_y + 10, window_x+30, window_y+10+24*
hour_width, gx.gray)
// Large right vertical line
t.gg.draw_line(window_x + 30 + hour_width, window_y + 10, window_x+30+hour_width, window_y+10+24*
hour_width, gx.gray)
// Draw the in the top right corner
t.ft.draw_text_def(window_x + window_width - 100, 20, t.date.ymmdd())
// Draw total time
h := total/ 60
m := total % 60
t.ft.draw_text(window_x + window_width - 100, 100, '$h:${m:02d}', gx.TextCfg{ color: color_productive })
}
fn (mut timer Timer) key_down(key int, super bool) {
match key {
C.GLFW_KEY_UP {
timer.date = timer.date.add_days(-1)
timer.load_tasks()
}
C.GLFW_KEY_DOWN {
timer.date = timer.date.add_days(1)
timer.load_tasks()
}
else {}
}
}