about summary refs log tree commit diff
path: root/.local/bin/rofi-notes
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/rofi-notes')
-rwxr-xr-x.local/bin/rofi-notes101
1 files changed, 101 insertions, 0 deletions
diff --git a/.local/bin/rofi-notes b/.local/bin/rofi-notes
new file mode 100755
index 0000000..876bc66
--- /dev/null
+++ b/.local/bin/rofi-notes
@@ -0,0 +1,101 @@
+#!/usr/bin/env lua
+
+local HOME   = os.getenv("HOME")
+local NOTES  = HOME .. "/Documents/Notes"
+local TERM   = "foot -a=floating_foot"
+local EDITOR = "nvim"
+
+local MODE   = arg[1] -- nil | "--delete" | "-d"
+
+local function rofi(prompt, lines)
+  local cmd = string.format(
+    'printf "%s" | rofi -dmenu -i -p "%s"',
+    table.concat(lines, "\n"),
+    prompt
+  )
+  local handle = io.popen(cmd)
+  local selection = handle:read("*a")
+  handle:close()
+  selection = selection:gsub("%s+$", "")
+  if selection == "" then return nil end
+  return selection
+end
+
+local function list_notes()
+  local display = {}
+  local lookup  = {}
+
+  local cmd     = string.format('find "%s" -type f', NOTES)
+  local pipe    = assert(io.popen(cmd, "r"))
+
+  for fullPath in pipe:lines() do
+    local relPath  = fullPath:gsub("^" .. NOTES .. "/", "")
+    local noteName = relPath:gsub("%.[^./]*$", "")
+
+    table.insert(display, noteName)
+    lookup[noteName] = fullPath
+  end
+
+  pipe:close()
+  return display, lookup
+end
+
+local function normalize_path(input)
+  input = input:gsub("^/", "")
+  if not input:match("%.md$") then
+    input = input .. ".md"
+  end
+  return NOTES .. "/" .. input
+end
+
+local function ensure_parent_dir(path)
+  local dir = path:match("(.+)/[^/]+$")
+  if dir then
+    os.execute(string.format('mkdir -p "%s"', dir))
+  end
+end
+
+local function open_in_nvim(path)
+  local cmd = string.format(
+    '%s -e %s "%s"',
+    TERM,
+    EDITOR,
+    path
+  )
+  os.execute(cmd)
+end
+
+local function delete_note(path, name)
+  local confirm = rofi("Delete " .. name .. "?", { "No", "Yes" })
+  if confirm == "Yes" then
+    os.execute(string.format('rm -- "%s"', path))
+  end
+end
+
+-- ---- main ----
+
+local notes, paths = list_notes()
+
+if MODE == "--delete" or MODE == "-d" then
+  local choice = rofi(" ", notes)
+  if not choice then os.exit(0) end
+
+  local path = paths[choice]
+  if path then
+    delete_note(path, choice)
+  end
+
+  os.exit(0)
+end
+
+-- default: open / create
+local choice = rofi(" ", notes)
+if not choice then os.exit(0) end
+
+local path = paths[choice]
+if not path then
+  path = normalize_path(choice)
+  ensure_parent_dir(path)
+end
+
+open_in_nvim(path)