-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimelimit.lua
More file actions
106 lines (95 loc) · 2.96 KB
/
timelimit.lua
File metadata and controls
106 lines (95 loc) · 2.96 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
-- timelimit.lua : barra de progreso basada en fechas para Neovim
-- Coloca este archivo en ~/.config/nvim/plugin/ y Neovim lo cargará automáticamente.
local ns = vim.api.nvim_create_namespace("timelimit")
-- Configuración visual
local INNER_LEN = 26 -- caracteres internos "━" o espacio
local FILL_CHAR = "━" -- carácter lleno
local EMPTY_CHAR = " " -- carácter vacío
local LEFT_MARK = '"' -- comilla inicial
local RIGHT_MARK = '"' -- comilla final
local UPDATE_MS = 60 * 1000 -- refresco cada 60 s (ms)
local HLGROUP = "TimelimitBar"
-- Crea el grupo de resaltado si no existe
if vim.fn.hlID(HLGROUP) == 0 then
-- verde claro por defecto; respeta colorescheme si ya lo define
vim.api.nvim_set_hl(0, HLGROUP, { fg = "#7CFF7C" })
end
---------------------------------------------------------------------
-- Utilidades
---------------------------------------------------------------------
local function parse_date(s)
local y, m, d = s:match("(%d+)%-(%d+)%-(%d+)")
if not (y and m and d) then
return nil
end
return os.time({ year = tonumber(y), month = tonumber(m), day = tonumber(d), hour = 0 })
end
local function make_inner_bar(p)
if p < 0 then
p = 0
end
if p > 1 then
p = 1
end
local fill = math.floor(p * INNER_LEN + 0.5)
return string.rep(FILL_CHAR, fill) .. string.rep(EMPTY_CHAR, INNER_LEN - fill)
end
local function make_full_bar(p)
return LEFT_MARK .. make_inner_bar(p) .. RIGHT_MARK
end
---------------------------------------------------------------------
-- Dibujar la barra
---------------------------------------------------------------------
local function refresh(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
if not vim.api.nvim_buf_is_valid(bufnr) then
return
end
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
local now = os.time()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
for i, line in ipairs(lines) do
local s_date, e_date = line:match("TIMELIMIT%s*%[(%d%d%d%d%-%d%d%-%d%d)%]%s*%[(%d%d%d%d%-%d%d%-%d%d)%]")
if s_date and e_date then
local ts, te = parse_date(s_date), parse_date(e_date)
if ts and te and te > ts then
local pct = (now - ts) / (te - ts)
local bar = make_full_bar(pct)
vim.api.nvim_buf_set_extmark(bufnr, ns, i - 1, 0, {
virt_text = { { bar, HLGROUP } },
virt_text_pos = "right_align",
hl_mode = "combine",
})
end
end
end
end
---------------------------------------------------------------------
-- Auto‑comandos y temporizador
---------------------------------------------------------------------
vim.api.nvim_create_autocmd({
"BufReadPost",
"BufWritePost",
"BufEnter",
"TextChanged",
"TextChangedI",
}, {
callback = function(args)
refresh(args.buf)
end,
})
local timer = vim.loop.new_timer()
if timer then
timer:start(
UPDATE_MS,
UPDATE_MS,
vim.schedule_wrap(function()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(buf) then
refresh(buf)
end
end
end)
)
end
return { refresh = refresh }