timer/lua/timer.lua

213 lines
4.9 KiB
Lua
Raw Normal View History

2024-10-07 21:32:23 +02:00
local TOML = require("toml")
2024-10-07 22:07:55 +02:00
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
2024-10-06 16:36:58 +02:00
2024-10-07 21:32:23 +02:00
local augroup = vim.api.nvim_create_augroup("Timer", { clear = true })
2024-10-07 22:07:55 +02:00
local filename = ".timer.toml"
local username = trim(os.getenv("USER")) .. "_at_" .. trim(io.popen("hostname -s"):read("*a"))
2024-10-07 21:32:23 +02:00
local focus_events = {}
local clean_to = 1
2024-10-07 22:07:55 +02:00
2024-10-07 21:32:23 +02:00
local function clean_from(from, events)
local to = 1
local clean_events = {}
-- coppy until from
for i = 1, from - 1, 1 do
table.insert(clean_events, events[i])
end
local last = {}
for i = from, #events, 1
do
local event = events[i]
local next_event = events[i + 1]
if next_event == nil then
--do nothing
elseif next_event.event == "gain" and event.event == "lose" then
if os.difftime(next_event.time, event.time) > 5 * 60 then
table.insert(clean_events, event)
end
end
if last == nil then
-- do nothing
elseif last.event == "lose" and event.event == "gain" then
if os.difftime(event.time, last.time) > 5 * 60 then
table.insert(clean_events, event)
end
end
last = event
end
return clean_events
end
2024-10-07 22:07:55 +02:00
local function summ_time()
2024-10-07 21:32:23 +02:00
focus_events = clean_from(clean_to + 1, focus_events)
clean_to = #focus_events
local t = 0
local start = os.time()
for _, fe in pairs(focus_events) do
if fe.event == "gain" then
start = fe.time
else
t = t + os.difftime(fe.time, start)
end
if focus_events[#focus_events].event == "gain" then
t = t + os.difftime(os.time(), focus_events[#focus_events].time)
end
end
return t
end
2024-10-07 22:07:55 +02:00
local function fmt_time(time)
local str = ""
2024-10-07 21:32:23 +02:00
2024-10-07 22:07:55 +02:00
local days = math.floor(time / (60 * 60 * 24))
if days ~= 0 then
str = str .. days .. "d"
end
time = time % (60 * 60 * 24)
local hours = math.floor(time / (60 * 60))
if hours ~= 0 then
str = str .. hours .. "h"
end
time = time % (60 * 60)
local minutes = math.floor(time / 60)
if minutes ~= 0 then
str = str .. minutes .. "m"
end
local seconds = time % (60)
if seconds ~= 0 then
str = str .. seconds .. "s"
2024-10-07 21:32:23 +02:00
end
2024-10-07 22:07:55 +02:00
return str
2024-10-07 21:32:23 +02:00
end
2024-10-06 16:36:58 +02:00
2024-10-07 22:07:55 +02:00
local function log_total_time()
local times = {}
times[username] = summ_time()
2024-10-07 21:32:23 +02:00
2024-10-07 22:07:55 +02:00
local file = io.open(filename, "r")
-- if file exists read
if file ~= nil then
local history = TOML.parse(file:read("*a"))
file:close()
for _, day in pairs(history) do
for user, time in pairs(day) do
if times[user] == nil then
times[user] = 0
end
times[user] = times[user] + time
end
end
end
for user, time in pairs(times) do
print(user, fmt_time(time))
end
2024-10-06 16:36:58 +02:00
end
2024-10-07 21:32:23 +02:00
2024-10-07 22:07:55 +02:00
local function log_time_table()
for i, e in pairs(focus_events) do
print(i, e.event, os.date("%H:%M:%S", e.time))
end
2024-10-06 16:36:58 +02:00
end
2024-10-07 22:07:55 +02:00
2024-10-07 21:32:23 +02:00
local function persist()
local today = os.date("%y-%m-%d", os.time())
local table = {}
local file = io.open(filename, "r")
-- if file exists read
if file ~= nil then
table = TOML.parse(file:read("*a"))
file:close()
end
-- write time to table
if table[today] ~= nil and table[today][username] ~= nil then
2024-10-07 22:07:55 +02:00
table[today][username] = table[today][username] + summ_time()
2024-10-07 21:32:23 +02:00
else
if table[today] == nil then
table[today] = {}
end
2024-10-07 22:07:55 +02:00
table[today][username] = summ_time()
2024-10-07 21:32:23 +02:00
end
-- write table to file
file = io.open(filename, "w")
if file == nil then
error("can't write to .timer file")
return
end
file:write("## dates and their corresponding seconds been here :)\n", TOML.encode(table), "\n")
file.close()
--reset timer
clean_to = 1
focus_events = {}
focus_gained()
2024-10-06 16:36:58 +02:00
end
2024-10-07 22:07:55 +02:00
local function focus_gained()
table.insert(focus_events, {
time = os.time(),
event = "gain",
})
end
local function focus_lost()
table.insert(focus_events, {
time = os.time(),
event = "lose",
})
end
2024-10-07 21:32:23 +02:00
2024-10-06 16:36:58 +02:00
local function setup()
vim.api.nvim_create_autocmd("VimEnter",
2024-10-07 21:32:23 +02:00
{ group = augroup, desc = "Start Timer", once = true, callback = focus_gained })
vim.api.nvim_create_autocmd("ExitPre",
{ group = augroup, desc = "Persist timer", once = true, callback = persist })
vim.api.nvim_create_autocmd("FocusGained", { group = augroup, callback = focus_gained })
vim.api.nvim_create_autocmd("FocusLost", { group = augroup, callback = focus_lost })
vim.api.nvim_create_user_command(
'Timer', -- The name of the command (accessible via :TimerStart)
2024-10-07 22:07:55 +02:00
log_total_time,
2024-10-07 21:32:23 +02:00
{ nargs = 0 } -- No arguments for this command (you can configure this later)
)
vim.api.nvim_create_user_command(
'TimerLog', -- The name of the command (accessible via :TimerStart)
log_time_table,
{ nargs = 0 } -- No arguments for this command (you can configure this later)
)
2024-10-06 16:36:58 +02:00
vim.api.nvim_create_user_command(
2024-10-07 21:32:23 +02:00
'TimerSave', -- The name of the command (accessible via :TimerStart)
persist,
{ nargs = 0 } -- No arguments for this command (you can configure this later)
2024-10-06 16:36:58 +02:00
)
end
return { setup = setup }