Created
May 12, 2026 22:03
-
-
Save eduardoarandah/5c133a8f8339ee8ec06237fa1898f7a4 to your computer and use it in GitHub Desktop.
Neovim command to Get work hours, read from markdown files in ./logs folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Get work hours, read from markdown files in ./logs folder | |
| -- | |
| -- example: | |
| --- | |
| -- --- | |
| -- date: 2026-05-12 | |
| -- summary: some work summary here | |
| -- hours: 3 | |
| -- --- | |
| -- usage: :GetWorkLogInLogFolder | |
| -- usage: :GetWorkLogInLogFolder 2023-01-01 | |
| -- usage: :GetWorkLogInLogFolder 2023-01-01 2023-12-31 | |
| vim.api.nvim_create_user_command("WorkLog", function(opts) | |
| local since = opts.fargs[1] or "0000-00-00" | |
| local until_ = opts.fargs[2] or "9999-99-99" | |
| local function read_frontmatter(path) | |
| local f = io.open(path, "r") | |
| if not f then | |
| return "", "" | |
| end | |
| local hours, summary | |
| for _ = 1, 10 do | |
| local line = f:read("*l") | |
| if not line then | |
| break | |
| end | |
| if not hours then | |
| hours = line:match("^hours:%s*(.+)$") | |
| end | |
| if not summary then | |
| summary = line:match("^summary:%s*(.+)$") | |
| end | |
| if hours and summary then | |
| break | |
| end | |
| end | |
| f:close() | |
| return hours or "", summary or "" | |
| end | |
| local lines = { "| Date | Hours | Summary |", "|------|-------|---------|" } | |
| local total = 0 | |
| local files = vim.fn.glob("logs/*.md", false, true) | |
| table.sort(files) | |
| for _, file in ipairs(files) do | |
| local date = file:match("(%d%d%d%d%-%d%d%-%d%d)%.md$") | |
| if date and date >= since and date <= until_ then | |
| local hours, summary = read_frontmatter(file) | |
| table.insert(lines, string.format("| %s | %s | %s |", date, hours, summary)) | |
| total = total + (tonumber(hours) or 0) | |
| end | |
| end | |
| table.insert(lines, "") | |
| table.insert(lines, "Total: " .. total) | |
| -- Format with prettier if available | |
| local output = table.concat(lines, "\n") | |
| if vim.fn.executable("prettier") == 1 then | |
| local formatted = vim.fn.system("prettier --parser=markdown", output) | |
| if vim.v.shell_error == 0 then | |
| output = formatted | |
| end | |
| end | |
| vim.api.nvim_put(vim.split(output, "\n"), "l", true, true) | |
| end, { nargs = "*", desc = "Get worklog with optional date range" }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment