Interactive Task creation in Lua

Instead of working on actual tasks, I am trying to (re)create Task Management in Silverbullet, also based on many suggestions found here in the Community. Mostly all of these were relying on features no longer available, so I have to start from scratch.

Being sick of writing the calendar emoji in v1, I used to have different slash commands linked with tags and dates, e.g. task-today-work or task-tomorrow-private. Not proud of that.

Inspired by recent InsertDate topic (credits to @lutzky), I used the opportunity to integrate it with a date picker, allowing for simple slash commands task-work, task-private etc. while dynamically setting the date with a comfortable picker during task creation.

See below a base task slash template with date picker included, have fun!

local DAY_SECONDS = 60 * 60 * 24

function nDaysFromNow(n, extraSpec)
  extraSpec = extraSpec or ""
  return os.date("%Y-%m-%d" .. extraSpec, os.time() + DAY_SECONDS * n)
end

local function selectDate()
  local options = {}

  local metaOptions = {
    { "Today", 0 },
    { "Tomorrow", 1 },
    { "Day after tomorrow", 2 },
    { "In one week", 7 },
    { "In two weeks", 14 },
  }

  for _, pair in ipairs(metaOptions) do
    local desc, n = pair[1], pair[2]
    table.insert(options, {
      name  = desc .. " (" .. nDaysFromNow(n) .. ")",
      value = nDaysFromNow(n)
    })
  end

  for n = 1, 365 do
    local desc = nDaysFromNow(n, " (%A)")
    table.insert(options, {
      name  = desc,
      value = nDaysFromNow(n)
    })
  end

  return editor.filterBox("Pick a due date", options)
end

slashcommand.define {
  name = "task",
  run = function()
    local line = editor.getCurrentLine()
    local ws, prefix, rest = string.match(line.textWithCursor, "^(%s*)([%-%*]?)%s*(.+)$")

    local result = selectDate()
    if result then
      local newText = ws ..
        "* [ ] " ..
        rest ..
        " 📅" ..
        result.value
      editor.replaceRange(line.from, line.to, newText, true)
    end
  end
}
3 Likes