~rbdr/nota.nvim

2cc29448a37001cf90b22d089ab992e93afdd71f — Ruben Beltran del Rio 3 months ago 503d09f
Add rescheduling tasks
6 files changed, 104 insertions(+), 22 deletions(-)

M README.md
M lua/notes.lua
M lua/task_views.lua
M lua/tasks.lua
M lua/util.lua
M plugin/nota.lua
M README.md => README.md +1 -3
@@ 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

M lua/notes.lua => lua/notes.lua +3 -2
@@ 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


M lua/task_views.lua => lua/task_views.lua +1 -1
@@ 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

M lua/tasks.lua => lua/tasks.lua +70 -15
@@ 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

M lua/util.lua => lua/util.lua +28 -0
@@ 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

M plugin/nota.lua => plugin/nota.lua +1 -1
@@ 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 })