-- Notes: quick notes kept as Markdown files in ~/Notes, one a file, its -- first line the title. Saved as you type. The list is beside the note, -- or before it when the window is narrow. local c8 = require "c8" local folder = (os.getenv("HOME") or ".") .. "/Notes" local ID = "org.cont8nuum.Notes" local win, list, editor, body, list_pane, note_pane, back local notes = {} -- { path, title, text, time }, newest first local current, pending local narrow = false local function quote(s) return "'" .. s:gsub("'", "'\\''") .. "'" end local function read(path) local f = io.open(path) if not f then return "" end local text = f:read("a") f:close() return text end local function title_of(text) local line = text:match("^%s*([^\n]+)") or "" line = line:gsub("^#+%s*", "") return line ~= "" and line or "New note" end -- ls gives the names newest first; io has no listing of its own local function load_all(done) os.execute("mkdir -p " .. quote(folder)) c8.run({ "ls", "-t", "--", folder }, function(status, out) notes = {} for name in (out or ""):gmatch("[^\n]+") do if name:match("%.md$") then local path = folder .. "/" .. name local text = read(path) notes[#notes + 1] = { path = path, text = text, title = title_of(text) } end end done() end) end local function tile() local n = notes[1] if n then c8.tile.update(ID, { template = "text", heading = n.title, text1 = (n.text:gsub("^[^\n]*\n?", "")):sub(1, 200) }) else c8.tile.clear(ID) end end local function write(note) local f = io.open(note.path, "w") if f then f:write(note.text) f:close() end end local function flush() if pending then pending:cancel() pending = nil if current then write(current) tile() end end end local function index_of(note) for i, n in ipairs(notes) do if n == note then return i end end end local function layout() local open = current ~= nil list_pane.hidden = narrow and open note_pane.hidden = narrow and not open back.hidden = not narrow end local function show(note) flush() current = note editor:set(note and note.text or "") editor.disabled = note == nil layout() if note then editor:focus() end end local function edited(w) local text = w:get() if not current or text == current.text then return end current.text = text current.title = title_of(text) -- The note just written goes to the top local i = index_of(current) if i and i ~= 1 then table.remove(notes, i) table.insert(notes, 1, current) list:count(#notes) end list:redraw() if pending then pending:cancel() end pending = c8.after(0.8, flush) end local function new_note() flush() -- Named for when it was made; a second one that second gets a number local stem = string.format("%s/%s", folder, os.date("%Y-%m-%d %H.%M.%S")) local path, n = stem .. ".md", 1 while true do local f = io.open(path) if not f then break end f:close() n = n + 1 path = string.format("%s (%d).md", stem, n) end local note = { path = path, text = "", title = "New note" } write(note) table.insert(notes, 1, note) list:count(#notes) show(note) end local function delete_note() local note = current if not note then return end win:dialog { title = "Delete \"" .. note.title .. "\"?", message = "The note goes to the Trash.", buttons = { "Delete", "Keep" }, done = function(choice) if choice ~= 1 then return end if pending then pending:cancel() pending = nil end c8.run({ "gio", "trash", "--", note.path }, function(status) if status ~= 0 then os.remove(note.path) end end) table.remove(notes, index_of(note)) list:count(#notes) current = nil show(not narrow and notes[1] or nil) tile() end } end local function build() list = c8.list { count = #notes, item = function(i) local n = notes[i] local rest = n.text:gsub("^[^\n]*\n?", ""):gsub("%s+", " ") return { title = n.title, subtitle = rest:sub(1, 80) } end, on_open = function(_, i) show(notes[i]) end, } editor = c8.text { editable = true, wrap = true, fill = true, expand = true, placeholder = "Write here. The first line is the title.", on_change = edited } back = c8.back_button { on_click = function() show(nil) list:redraw() end } list_pane = c8.column { spacing = 12, width = 320, fill = true, c8.row { spacing = 12, c8.label { text = "Notes", style = "header", expand = true }, }, c8.row { spacing = 8, c8.button { label = "New note", primary = true, on_click = new_note }, }, c8.scroll { list, expand = true, fill = true }, } note_pane = c8.column { spacing = 12, expand = true, fill = true, c8.row { spacing = 12, back, c8.button { label = "Delete", on_click = delete_note } }, editor, } body = c8.row { spacing = 32, padding = { 24, 24, 24, 24 }, narrow_padding = { 16, 16, 16, 16 }, fill = true, list_pane, note_pane } return body end c8.app { id = ID, open = function() if not (win and win.open) then win = c8.window { title = "Notes", width = 980, height = 700, build(), -- Which pane shows and the list's width; the rest -- follows the window by itself on_resize = function(_, _, _, now_narrow) narrow = now_narrow list_pane.width = narrow and 0 or 320 list_pane.expand = narrow layout() end, on_key = function(_, ev) if ev.ctrl and ev.key == "n" then new_note() return true elseif ev.key == "Escape" and narrow and current then show(nil) return true end end, on_close = flush } load_all(function() list:count(#notes) show(not narrow and notes[1] or nil) tile() end) end win:present() end, search = function(query) local found = {} local q = query:lower() for i, n in ipairs(notes) do if q ~= "" and n.text:lower():find(q, 1, true) then found[#found + 1] = { id = n.path, title = n.title, icon = "text-x-generic" } end end return found end, result = function(id) for _, n in ipairs(notes) do if n.path == id then show(n) end end end, }