summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/helpers/stale.lua
diff options
context:
space:
mode:
Diffstat (limited to '.config/nvim/lua/helpers/stale.lua')
-rw-r--r--.config/nvim/lua/helpers/stale.lua159
1 files changed, 159 insertions, 0 deletions
diff --git a/.config/nvim/lua/helpers/stale.lua b/.config/nvim/lua/helpers/stale.lua
new file mode 100644
index 0000000..062e3cf
--- /dev/null
+++ b/.config/nvim/lua/helpers/stale.lua
@@ -0,0 +1,159 @@
1local M = {}
2_VT_NS = vim.api.nvim_create_namespace("lsp_signature")
3
4local prescript
5local prescript_hi
6local due_hi
7local ft
8local future
9local future_hi
10local date_hi
11local pattern_start
12local pattern_end
13
14local use_clock_time
15local default_due_time
16
17-- local date_pattern
18local datetime_pattern
19local datetime_pattern_match
20
21local function parseDue(due)
22 local year = 31556926
23 local month = 2629743
24 local week = 604800
25 local day = 86400
26 local hour = 3600
27 local minute = 60
28 local res = ''
29
30 if due >= year then
31 res = res .. math.floor(due / year) .. 'y '
32 due = due % year
33 end
34
35 if due >= month then
36 res = res .. math.floor(due / month) .. 'm '
37 due = due % month
38 end
39
40 if due >= week then
41 res = res .. math.floor(due / week) .. 'w '
42 due = due % week
43 end
44
45 if use_clock_time == true then
46 if due >= day then
47 res = res .. math.floor(due / day) .. 'd '
48 due = due % day
49 end
50
51 if due >= hour then
52 res = res .. math.floor(due / hour) .. 'h '
53 due = due % hour
54 end
55
56 if due >= minute then
57 res = res .. math.floor(due / minute) .. 'min '
58 due = due % minute
59 end
60 else
61 res = res .. math.floor(due / day) + 1 .. 'd '
62 end
63
64 return res
65end
66
67function M.setup(c)
68 use_clock_time = c.use_clock_time or true
69 default_due_time = c.default_due_time or 'midnight'
70 prescript = c.prescript or 'has been: '
71 prescript_hi = c.prescript_hi or 'Comment'
72 due_hi = c.due_hi or 'String'
73 ft = c.ft or '*.wiki'
74 future = c.future or 'future'
75 future_hi = c.future_hi or 'Error'
76 date_hi = c.date_hi or 'Conceal'
77 pattern_start = c.pattern_start or '('
78 pattern_end = c.pattern_end or ')'
79
80 local lua_start = pattern_start:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
81 local lua_end = pattern_end:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
82
83 local regex_start = pattern_start:gsub("\\%^%$%.%*~%[%]&", "\\%1")
84 local regex_end = pattern_end:gsub("\\%^%$%.%*~%[%]&", "\\%1")
85
86 datetime_pattern = lua_start .. '%d%d%d%d%-%d%d%-%d%d %d%d:%d%d' .. lua_end
87 datetime_pattern_match = lua_start .. '(%d%d%d%d)%-(%d%d)%-(%d%d) (%d%d):(%d%d)' .. lua_end
88
89 local regex_hi = '/' .. regex_start .. '\\d*-*\\d\\+-\\d\\+' .. regex_end ..
90 '/'
91
92 vim.api.nvim_command('autocmd BufEnter ' .. ft ..
93 ' lua require("helpers.stale").draw(0)')
94 vim.api.nvim_command('autocmd InsertLeave ' .. ft ..
95 ' lua require("helpers.stale").redraw(0)')
96 vim.api.nvim_command('autocmd TextChanged ' .. ft ..
97 ' lua require("helpers.stale").redraw(0)')
98 vim.api.nvim_command('autocmd TextChangedI ' .. ft ..
99 ' lua require("helpers.stale").redraw(0)')
100
101 vim.api.nvim_command('autocmd BufEnter ' .. ft .. ' syn match DueDate ' ..
102 regex_hi ..
103 ' display containedin=mkdNonListItemBlock,mkdListItemLine,mkdBlockquote contained')
104 vim.api.nvim_command('autocmd BufEnter ' .. ft .. ' hi def link DueDate ' ..
105 date_hi)
106end
107
108function M.draw(buf)
109 local user_time
110 local user_hour
111 local user_min
112 local user_sec = 00
113
114 -- get current time
115 local now = os.time(os.date('*t'))
116
117 -- find which date pattern is being passed in by user
118 for key, value in pairs(vim.api.nvim_buf_get_lines(buf, 0, -1, {})) do
119 local dateTime = string.match(value, datetime_pattern)
120 local due
121
122 if dateTime then
123 local cur_year, month, day, user_hour, user_min = dateTime:match(datetime_pattern_match)
124 user_time = os.time({
125 year = cur_year,
126 month = month,
127 day = day,
128 hour = user_hour,
129 min = user_min,
130 sec = user_sec
131 })
132
133 due = now - user_time
134 end
135
136 if due then
137 local parsed
138
139 if due > 0 then
140 parsed = { parseDue(due), due_hi }
141 else
142 parsed = { future, future_hi }
143 end
144
145 vim.api.nvim_buf_set_virtual_text(buf, _VT_NS, key - 1, {
146 { prescript, prescript_hi }, parsed
147 }, {})
148 end
149 end
150end
151
152function M.clear(buf) vim.api.nvim_buf_clear_namespace(buf, _VT_NS, 0, -1) end
153
154function M.redraw(buf)
155 M.clear(buf)
156 M.draw(buf)
157end
158
159return M