7 Commits

Author SHA1 Message Date
81f137ee1a updated readme 2024-10-11 16:48:04 +02:00
bac26868d8 better persistace 2024-10-11 16:47:37 +02:00
9c2ac4bc46 update readme 2024-10-08 11:05:16 +02:00
312689b005 fix persist 2024-10-08 10:57:57 +02:00
e9eab52653 fix 2024-10-07 22:19:02 +02:00
d367d166a9 update readme 2024-10-07 22:11:05 +02:00
50bcdedb9b log time 2024-10-07 22:07:55 +02:00
5 changed files with 180 additions and 57 deletions

0
.asdf
View File

7
.timer.toml Normal file
View File

@ -0,0 +1,7 @@
## dates and their corresponding seconds been here :)
[24-10-08]
u80864958_at_u80864958 = 425
[24-10-11]
u80864958_at_u80864958 = 6021
[24-10-07]
u80864958_at_u80864958 = 432

View File

@ -0,0 +1,38 @@
# Timer
Timer times the time it took you to realize your project
Counts the time in your editor inside on directory.
Not beeing inside the editor for less than 5 minutes gets counted as beeing
inside the editor.
## Usage
### Show time
```:Timer```: prints the time inside this directory. If ```.timer.toml``` exists it get's included.
```:TimerLog```: logs the current time table.
### Save time
Time gets automatically saved, when neovim is closed. Given that the ```.timer.toml``` file exists.
If it doesn't Timer searches the next 10 higher directories for one. If there isn't one either nothing get's persisted.
```:TimerSave```: saves the current time to ```.timer.toml```. If the file doesn't exist it gets created.
## Installation
Lunar vim:
```lua
lvim.plugins = {
{
"schreifuchs/timer",
url = "https://git.schreifuchs.ch/schreifuchs/timer.git",
config = function()
require('timer').setup()
end
},
}
```

View File

@ -1,15 +1,16 @@
local TOML = require("toml")
local augroup = vim.api.nvim_create_augroup("Timer", { clear = true })
local focus_events = {}
local clean_to = 1
function trim(s)
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local augroup = vim.api.nvim_create_augroup("Timer", { clear = true })
local filename = ".timer.toml"
local username = trim(os.getenv("USER")) .. "_at_" .. trim(io.popen("hostname -s"):read("*a"))
local focus_events = {}
local clean_to = 1
local function clean_from(from, events)
local to = 1
local clean_events = {}
-- coppy until from
@ -45,7 +46,7 @@ local function clean_from(from, events)
return clean_events
end
local function time()
local function summ_time()
focus_events = clean_from(clean_to + 1, focus_events)
clean_to = #focus_events
@ -65,10 +66,65 @@ local function time()
return t
end
local function log_time()
print(time())
local function fmt_time(time)
local str = ""
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"
end
return str
end
local function log_total_time()
local times = {}
times[username] = summ_time()
local file = io.open(filename, "r")
-- if file exists read
if file ~= nil then
local history = require("toml").parse(file:read("*a"))
file:close()
print("Time for:", filename)
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
end
local function log_time_table()
for i, e in pairs(focus_events) do
print(i, e.event, os.date("%H:%M:%S", e.time))
@ -76,8 +132,6 @@ local function log_time_table()
end
local function focus_gained()
print("Huiii Fokus")
table.insert(focus_events, {
time = os.time(),
event = "gain",
@ -85,75 +139,103 @@ local function focus_gained()
end
local function focus_lost()
print("Hokus pokus fort ist der fokus")
table.insert(focus_events, {
time = os.time(),
event = "lose",
})
end
local function persist()
local filename = ".timer.toml"
local username = trim(os.getenv("USER")) .. "_at_" .. trim(io.popen("hostname -s"):read("*a"))
local function next_lowest_time_file()
local file_path = filename
for _ = 1, 10, 1 do
local f = io.open(file_path)
if f ~= nil then
return file_path
end
file_path = "../" .. file_path
end
return ""
end
local function persist(force)
return function()
local TOML = require("toml")
local today = os.date("%y-%m-%d", os.time())
local table = {}
local file_path = filename
local file = io.open(filename, "r")
local file = io.open(file_path, "r")
-- if file exists read
if file ~= nil then
table = TOML.parse(file:read("*a"))
file:close()
else
if not force then
file_path = next_lowest_time_file()
if file_path == "" then
print("No save file found. To create a new save file run :TimerSave")
return
end
print("Save file found:", file_path)
end
end
-- write time to table
if table[today] ~= nil and table[today][username] ~= nil then
table[today][username] = table[today][username] + time()
table[today][username] = table[today][username] + summ_time()
else
if table[today] == nil then
table[today] = {}
end
table[today][username] = time()
table[today][username] = summ_time()
end
-- write table to file
file = io.open(filename, "w")
file = io.open(file_path, "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()
local tml = TOML.encode(table)
file:write("## dates and their corresponding seconds been here :)\n", tml, "\n")
file:close()
print("Timer saved successfully :-)")
--reset timer
clean_to = 1
focus_events = {}
focus_gained()
end
end
local function setup()
vim.api.nvim_create_autocmd("VimEnter",
{ 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 })
{ group = augroup, desc = "Persist timer", once = true, callback = persist(false) })
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)
log_time,
'Timer',
log_total_time,
{ 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)
'TimerLog',
log_time_table,
{ nargs = 0 } -- No arguments for this command (you can configure this later)
)
vim.api.nvim_create_user_command(
'TimerSave', -- The name of the command (accessible via :TimerStart)
persist,
'TimerSave',
persist(true),
{ nargs = 0 } -- No arguments for this command (you can configure this later)
)
end

View File

@ -1,4 +0,0 @@
local username = os.getenv("USER")
local hostname = io.popen("hostname -s"):read("*a")
print(username)
print(hostname)