From: Ruben Beltran del Rio Date: Sun, 3 Mar 2024 20:07:57 +0000 (+0100) Subject: Add rescheduling tasks X-Git-Url: https://git.r.bdr.sh/rbdr/nota.nvim/commitdiff_plain/2cc29448a37001cf90b22d089ab992e93afdd71f?hp=503d09fc95a47c13141d097cf80dd243d1dce342 Add rescheduling tasks --- diff --git a/README.md b/README.md index 8106dfe..2d8acf3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The minimal setup doesn't require any configuration, and sets the default paths ```lua require('lazy').setup({ - 'git@git.sr.ht:~rbdr/nota.nvim', + 'https://git.sr.ht/~rbdr/nota.nvim', }) ``` @@ -69,8 +69,6 @@ require('lazy').setup({ }) ``` -If the default directories don't work for you, you can override them as well: - ## Default Keybinds ### Opening Notes diff --git a/lua/notes.lua b/lua/notes.lua index 008baac..f5a4b00 100644 --- a/lua/notes.lua +++ b/lua/notes.lua @@ -36,8 +36,9 @@ end ------------------------------------------------------------------------------- --- Opens the daily note -function Notes.open_daily() - local filename = os.date('%Y-%m-%d') .. '.md' +function Notes.open_daily(date) + date = date or os.date('%Y-%m-%d') + local filename = date .. '.md' open_periodic_note('daily', filename) end diff --git a/lua/task_views.lua b/lua/task_views.lua index ee76ad3..9309ad3 100644 --- a/lua/task_views.lua +++ b/lua/task_views.lua @@ -23,7 +23,7 @@ local function get_this_weeks_files() end local function find_tasks(completed, important) - local file_directory_path = Configuration.path_for(Configuration.configuration.periodic_locations.daily) + local file_directory_path = Configuration.path_for() local completed_fragment = '(\\s|x)' if completed == 1 then diff --git a/lua/tasks.lua b/lua/tasks.lua index 618a688..66dbc48 100644 --- a/lua/tasks.lua +++ b/lua/tasks.lua @@ -3,10 +3,31 @@ ------------------------------------------------------------------------------- local Tasks = {} local Configuration = require('configuration') +local Notes = require('notes') +local Util = require('util') local api = vim.api ------------------------------------------------------------------------------- -- Internal Functions ------------------------------------------------------------------------------- +local unchecked_pattern = '^(%s*)%- %[ %]' +local unchecked_important_pattern = '^(%s*)%* %[ %]' +local checked_pattern = '^(%s*)%- %[x%]' +local checked_important_pattern = '^(%s*)%* %[x%]' + +local function open(path) + local parent = Util.directory_name(path) + Util.ensure_directory_exists(parent) + vim.cmd('edit ' .. path) +end + +local function open_inbox() + local path = Configuration.path_for(Configuration.configuration.tasks.inbox) + open(path) +end +local function open_someday() + local path = Configuration.path_for(Configuration.configuration.tasks.someday) + open(path) +end ------------------------------------------------------------------------------- -- Public Interface ------------------------------------------------------------------------------- @@ -16,11 +37,6 @@ function Tasks.toggle() local line_number = api.nvim_win_get_cursor(0)[1] local line = api.nvim_get_current_line() - local unchecked_pattern = '^(%s*)%- %[ %]' - local unchecked_important_pattern = '^(%s*)%* %[ %]' - local checked_pattern = '^(%s*)%- %[x%]' - local checked_important_pattern = '^(%s*)%* %[x%]' - if line:match(unchecked_pattern) then line = line:gsub(unchecked_pattern, '%1- [x]', 1) elseif line:match(unchecked_important_pattern) then @@ -39,11 +55,6 @@ function Tasks.toggle_importance() local line_number = api.nvim_win_get_cursor(0)[1] local line = api.nvim_get_current_line() - local unchecked_pattern = '^(%s*)%- %[ %]' - local unchecked_important_pattern = '^(%s*)%* %[ %]' - local checked_pattern = '^(%s*)%- %[x%]' - local checked_important_pattern = '^(%s*)%* %[x%]' - if line:match(unchecked_pattern) then line = line:gsub(unchecked_pattern, '%1* [ ]', 1) elseif line:match(unchecked_important_pattern) then @@ -65,7 +76,10 @@ end --- Captures a new task into the inbox function Tasks.capture() - error('Not yet implemented') + local prefix = '- [ ] ' + open_inbox() + vim.cmd('normal! Go'..prefix) + vim.cmd('startinsert!') end --- Tag a task @@ -75,22 +89,63 @@ end --- Reschedule a task for today function Tasks.reschedule_for_today() - error('Not yet implemented') + local today = os.date('%Y-%m-%d') + Tasks.reschedule(today) end --- Reschedule a task for tomorrow function Tasks.reschedule_for_tomorrow() - error('Not yet implemented') + local tomorrow = os.date('%Y-%m-%d', os.time() + 24*60*60) + Tasks.reschedule(tomorrow) end --- Reschedule a task for someday function Tasks.reschedule_for_someday() - error('Not yet implemented') + Tasks.reschedule('someday') end --- Reschedule a task for an arbitrary date function Tasks.reschedule(new_date) - error('Not yet implemented') + + if not new_date or new_date == '' then + new_date = vim.fn.input('Reschedule for which date (YYYY-mm-dd): ') + end + + if not Util.is_valid_date(new_date) and new_date ~= 'someday' then + api.nvim_err_writeln(new_date .. ' is not a valid date in the format YYYY-mm-dd') + return + end + + local line_number = api.nvim_win_get_cursor(0)[1] + local buffer = api.nvim_get_current_buf() + local line = api.nvim_get_current_line() + local filename = vim.fn.expand('%:t:r') + + if line:match(unchecked_pattern) or line:match(unchecked_important_pattern) then + if Util.is_before_today(filename) and line:match(unchecked_pattern) then + local rescheduled_line = line:gsub(unchecked_pattern, '%1- [>' .. new_date .. ']', 1) + api.nvim_buf_set_lines(0, line_number - 1, line_number, false, {rescheduled_line}) + elseif Util.is_before_today(filename) and line:match(unchecked_important_pattern) then + local rescheduled_line = line:gsub(unchecked_important_pattern, '%1* [>' .. new_date .. ']', 1) + api.nvim_buf_set_lines(0, line_number - 1, line_number, false, {rescheduled_line}) + else + api.nvim_buf_set_lines(buffer, line_number - 1, line_number, false, {}) + end + + vim.cmd('write') + + if new_date == 'someday' then + open_someday() + else + Notes.open_daily(new_date) + end + + local line_count = api.nvim_buf_line_count(0) + api.nvim_buf_set_lines(0, line_count, line_count, false, {line}) + api.nvim_win_set_cursor(0, {line_count, 0}) + else + api.nvim_err_writeln('Reschedule only works on open tasks') + end end return Tasks diff --git a/lua/util.lua b/lua/util.lua index 2636e7c..de53228 100644 --- a/lua/util.lua +++ b/lua/util.lua @@ -29,11 +29,15 @@ end ------------------------------------------------------------------------------- -- Public Interface ------------------------------------------------------------------------------- +-- File Utils + function Util.ensure_directory_exists(path) local full_path = vim.fn.expand(path) create_directory(path) end +-- Path Utils + function Util.join(...) local separator = '/' local paths = {...} @@ -45,4 +49,28 @@ function Util.directory_name(file_path) return file_path:match(pattern) end +-- Date Utils + +function Util.is_valid_date(date_string) + local pattern = '(%d+)-(%d+)-(%d+)' + local year, month, day = date_string:match(pattern) + if year and month and day then + return true + end + return false +end + +function Util.is_before_today(date_string) + local pattern = '(%d+)-(%d+)-(%d+)' + local year, month, day = date_string:match(pattern) + if year and month and day then + local today = os.date('*t') + local today_date = os.time({year = today.year, month = today.month, day = today.day}) + local date = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)}) + return date < today_date + else + return false + end +end + return Util diff --git a/plugin/nota.lua b/plugin/nota.lua index 9c6e419..d34a7be 100644 --- a/plugin/nota.lua +++ b/plugin/nota.lua @@ -75,7 +75,7 @@ command('NotaTagTask', function() require('tasks').tag() end, { nargs = 0 }) command('NotaRescheduleTaskToday', function() require('tasks').reschedule_for_today() end, { nargs = 0 }) command('NotaRescheduleTaskTomorrow', function() require('tasks').reschedule_for_tomorrow() end, { nargs = 0 }) command('NotaRescheduleTaskSomeday', function() require('tasks').reschedule_for_someday() end, { nargs = 0 }) -command('NotaRescheduleTask', function(options) require('tasks').reschedule(options.args) end, { nargs = 1 }) +command('NotaRescheduleTask', function(options) require('tasks').reschedule(options.args) end, { nargs = '?' }) -- .plan Handling Commands command('NotaOpenPlan', function() require('plan').open() end, { nargs = 0 })