-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcounter-widget.lua
More file actions
151 lines (119 loc) · 3.4 KB
/
counter-widget.lua
File metadata and controls
151 lines (119 loc) · 3.4 KB
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
-- name = "Counter"
-- description = "Time counting widget to fight bad habits"
-- type = "widget"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
-- version = "2.0"
-- on_resume_when_folding = "true"
local prefs = require "prefs"
local date = require "date"
-- constants
local max_counters = 5
local year = 365.25
local month = 30.43
local milestones = {
1, 3, 7, 14,
month, month * 3, month * 6,
year, year * 3, year * 5, year * 10, year * 20, year * 100
}
local milestones_formatted = {
"1+ days", "3+ days", "1+ weeks", "2+ weeks",
"1+ months", "3+ months", "6+ months",
"1+ years", "3+ years", "5+ years", "10+ years", "20+ years", "100+ years"
}
function on_load()
init_default_settings()
end
function on_resume()
local gui_inst = {}
local lines_num = max_counters
if ui:is_folded() then
lines_num = 1
end
for i = 1, lines_num do
local title, start_date = parse_settings_string(i)
if title == nil then
break
end
local curr_date = date()
local passed = date.diff(curr_date, start_date)
local passed_days = math.floor(passed:spandays())
if passed_days < 0 then
table.insert(gui_inst, {"text", "Error: the date can't be in the future"})
break
end
local idx = get_milestone_idx(passed)
local passed_str = passed_days.." days"
if prefs.show_milestones and passed_days > 0 then
passed_str = passed_str.." / "..milestones_formatted[idx]
end
local next_milestone_percent = passed_days / milestones[idx+1] * 100
table.insert(gui_inst, {"progress", title..": "..passed_str, {progress = next_milestone_percent}})
end
gui_inst = insert_between_elements(gui_inst, {"new_line", 1})
gui(gui_inst):render()
end
function init_default_settings()
if prefs.counter_1 == nil then
prefs["counter_1"] = "Sample / 31.01.2024"
for i = 2,max_counters do
prefs["counter_"..i] = ""
end
end
if prefs.show_milestones == nil then
prefs.show_milestones = true
end
end
function parse_settings_string(i)
local title_and_date = prefs["counter_"..i]
if title_and_date == nil or #title_and_date == 0 then
return nil
end
local splitted = title_and_date:split("/")
local title = trim(splitted[1])
if title == nil or #title == 0 then
return nil
end
local date_str = trim(splitted[2])
if date_str == nil or #date_str == 0 then
return nil
end
local arr = date_str:split("%.")
if arr == nil or #arr < 3 then
return nil
end
local start_date = date(arr[3], arr[2], arr[1])
return title, start_date
end
function on_click()
prefs:show_dialog()
end
function on_settings()
prefs:show_dialog()
end
-- utils
function trim(s)
if s == nil or #s == 0 then return s end
return s:match("^%s*(.-)%s*$")
end
function insert_between_elements(t, element)
local new_table = {}
for i = 1, #t do
table.insert(new_table, t[i])
if i < #t then
table.insert(new_table, element)
end
end
return new_table
end
function get_milestone_idx(passed)
local days_passed = passed:spandays()
local idx = 0
for k,v in ipairs(milestones) do
if days_passed > v then
idx = idx + 1
else
break
end
end
return idx
end