]> git.r.bdr.sh - rbdr/nota.nvim/commitdiff
Add rescheduling tasks
authorRuben Beltran del Rio <redacted>
Sun, 3 Mar 2024 20:07:57 +0000 (21:07 +0100)
committerRuben Beltran del Rio <redacted>
Sun, 3 Mar 2024 20:07:57 +0000 (21:07 +0100)
README.md
lua/notes.lua
lua/task_views.lua
lua/tasks.lua
lua/util.lua
plugin/nota.lua

index 8106dfee6b59da15f675e8bf361e23a93827bfd9..2d8acf378c2717e951f08df235ea83cccc6c574b 100644 (file)
--- 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
index 008baac1c148fb5d1df182a3974061a42a53ba32..f5a4b00f25c1c92d1759fb08950d749b1f97e5fb 100644 (file)
@@ -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
 
index ee76ad3218dea9c8c74f1d2548fdc57b6a6f9fb0..9309ad3277c276fab28233937f913652346f7867 100644 (file)
@@ -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
index 618a688dfa60a2e99c5880152577bc08589ee3c9..66dbc48d4422845cd5a6bd152f04ed9fc0281949 100644 (file)
@@ -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
index 2636e7c0dd4b7d27ab1f637141e4f0e15cff5a93..de53228117b3ec3be8be7379235ca96b2cb310fd 100644 (file)
@@ -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
index 9c6e419bb8c3a71f8577a026667841e61e41278e..d34a7be124cd786a7a16bd41fe24226bcdc15a48 100644 (file)
@@ -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 })