Files
dotfiles/config/nvim-old/lua/tasks.lua
T

93 lines
3.2 KiB
Lua

local M = {}
local function get_current_time()
return os.date("%H:%M")
end
local function get_active_task_line()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local active_lines = {}
for i, line in ipairs(lines) do
if line:match("#status/in%-progress") then
table.insert(active_lines, {line = line, lnum = i})
end
end
if #active_lines == 0 then
print("No active task found (#status/in-progress).")
return nil, "no_active"
elseif #active_lines > 1 then
print("Multiple active tasks found. Please ensure only one task is #status/in-progress.")
return nil, "multiple_active"
else
return active_lines[1].line, active_lines[1].lnum
end
end
function M.create_new_task()
local desc = vim.fn.input("Task Description: ")
if desc == '' then return end
local project = vim.fn.input("Project (leave empty if none, e.g., '[[Project Name]]'): ")
local priority_options = {"high", "medium", "low"}
local priority_idx = vim.fn.inputlist("Priority:", priority_options)
local priority = priority_options[tonumber(priority_idx)] or "medium"
local category_options = {"work", "meeting", "learning", "journal", "focused-work", "break"}
local category_idx = vim.fn.inputlist("Category:", category_options)
local category = category_options[tonumber(category_idx)] or "work"
local new_task_line = string.format("- [ ] %s %s #status/todo #priority/%s #%s",
desc, project, priority, category)
vim.api.nvim_put({new_task_line}, "l", true, true)
print("New task created: " .. new_task_line)
end
function M.start_task()
local current_line, lnum = get_active_task_line()
if not current_line then return end
local new_line = current_line
local current_time = get_current_time()
if new_line:match("start:: %d%d:%d%d") then
print("Task already has a start time. Updating to current time.")
new_line = new_line:gsub("start:: %d%d:%d%d", "start:: " .. current_time)
else
new_line = new_line .. " start:: " .. current_time
end
-- Clear end time if present when starting a task again
new_line = new_line:gsub(" end:: %d%d:%d%d", "")
vim.api.nvim_buf_set_lines(0, lnum - 1, lnum, false, {new_line})
print("Task started/resumed: " .. current_time)
end
function M.stop_task()
local current_line, lnum = get_active_task_line()
if not current_line then return end
local new_line = current_line
local current_time = get_current_time()
if new_line:match("end:: %d%d:%d%d") then
print("Task already has an end time. Updating to current time.")
new_line = new_line:gsub("end:: %d%d:%d%d", "end:: " .. current_time)
else
new_line = new_line .. " end:: " .. current_time
end
-- Option to change status to done
local change_status = vim.fn.confirm("Change status to #status/done?", "&Yes\n&No", 2)
if change_status == 1 then -- Yes
new_line = new_line:gsub("#status/in%-progress", "#status/done")
end
vim.api.nvim_buf_set_lines(0, lnum - 1, lnum, false, {new_line})
print("Task stopped: " .. current_time)
end
return M