summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/helpers/stale.lua
blob: 062e3cfc8cf21f82410e2ea91c6037b8a87669c4 (plain)
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
local M = {}
_VT_NS = vim.api.nvim_create_namespace("lsp_signature")

local prescript
local prescript_hi
local due_hi
local ft
local future
local future_hi
local date_hi
local pattern_start
local pattern_end

local use_clock_time
local default_due_time

-- local date_pattern
local datetime_pattern
local datetime_pattern_match

local function parseDue(due)
    local year = 31556926
    local month = 2629743
    local week = 604800
    local day = 86400
    local hour = 3600
    local minute = 60
    local res = ''

    if due >= year then
        res = res .. math.floor(due / year) .. 'y '
        due = due % year
    end

    if due >= month then
        res = res .. math.floor(due / month) .. 'm '
        due = due % month
    end

    if due >= week then
        res = res .. math.floor(due / week) .. 'w '
        due = due % week
    end

    if use_clock_time == true then
        if due >= day then
            res = res .. math.floor(due / day) .. 'd '
            due = due % day
        end

        if due >= hour then
            res = res .. math.floor(due / hour) .. 'h '
            due = due % hour
        end

        if due >= minute then
            res = res .. math.floor(due / minute) .. 'min '
            due = due % minute
        end
    else
        res = res .. math.floor(due / day) + 1 .. 'd '
    end

    return res
end

function M.setup(c)
    use_clock_time = c.use_clock_time or true
    default_due_time = c.default_due_time or 'midnight'
    prescript = c.prescript or 'has been: '
    prescript_hi = c.prescript_hi or 'Comment'
    due_hi = c.due_hi or 'String'
    ft = c.ft or '*.wiki'
    future = c.future or 'future'
    future_hi = c.future_hi or 'Error'
    date_hi = c.date_hi or 'Conceal'
    pattern_start = c.pattern_start or '('
    pattern_end = c.pattern_end or ')'

    local lua_start = pattern_start:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
    local lua_end = pattern_end:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")

    local regex_start = pattern_start:gsub("\\%^%$%.%*~%[%]&", "\\%1")
    local regex_end = pattern_end:gsub("\\%^%$%.%*~%[%]&", "\\%1")

    datetime_pattern = lua_start .. '%d%d%d%d%-%d%d%-%d%d %d%d:%d%d' .. lua_end
    datetime_pattern_match = lua_start .. '(%d%d%d%d)%-(%d%d)%-(%d%d) (%d%d):(%d%d)' .. lua_end

    local regex_hi = '/' .. regex_start .. '\\d*-*\\d\\+-\\d\\+' .. regex_end ..
        '/'

    vim.api.nvim_command('autocmd BufEnter ' .. ft ..
        ' lua require("helpers.stale").draw(0)')
    vim.api.nvim_command('autocmd InsertLeave ' .. ft ..
        ' lua require("helpers.stale").redraw(0)')
    vim.api.nvim_command('autocmd TextChanged ' .. ft ..
        ' lua require("helpers.stale").redraw(0)')
    vim.api.nvim_command('autocmd TextChangedI ' .. ft ..
        ' lua require("helpers.stale").redraw(0)')

    vim.api.nvim_command('autocmd BufEnter ' .. ft .. ' syn match DueDate ' ..
        regex_hi ..
        ' display containedin=mkdNonListItemBlock,mkdListItemLine,mkdBlockquote contained')
    vim.api.nvim_command('autocmd BufEnter ' .. ft .. ' hi def link DueDate ' ..
        date_hi)
end

function M.draw(buf)
    local user_time
    local user_hour
    local user_min
    local user_sec = 00

    -- get current time
    local now = os.time(os.date('*t'))

    -- find which date pattern is being passed in by user
    for key, value in pairs(vim.api.nvim_buf_get_lines(buf, 0, -1, {})) do
        local dateTime = string.match(value, datetime_pattern)
        local due

        if dateTime then
            local cur_year, month, day, user_hour, user_min = dateTime:match(datetime_pattern_match)
            user_time = os.time({
                year = cur_year,
                month = month,
                day = day,
                hour = user_hour,
                min = user_min,
                sec = user_sec
            })

            due = now - user_time
        end

        if due then
            local parsed

            if due > 0 then
                parsed = { parseDue(due), due_hi }
            else
                parsed = { future, future_hi }
            end

            vim.api.nvim_buf_set_virtual_text(buf, _VT_NS, key - 1, {
                { prescript, prescript_hi }, parsed
            }, {})
        end
    end
end

function M.clear(buf) vim.api.nvim_buf_clear_namespace(buf, _VT_NS, 0, -1) end

function M.redraw(buf)
    M.clear(buf)
    M.draw(buf)
end

return M