Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Notes App (apps/notes.lua)
- return {
- name = "Notes",
- color = colors.yellow,
- symbol = "N",
- page = 1,
- run = function(w, h, drawBottomBar)
- local notesDir = "notes"
- -- 1. Ensure notes directory exists
- if not fs.exists(notesDir) then
- fs.makeDir(notesDir)
- elseif not fs.isDir(notesDir) then
- fs.delete(notesDir)
- fs.makeDir(notesDir)
- end
- local function getNotesList()
- local list = {}
- local files = fs.list(notesDir)
- for _, filename in ipairs(files) do
- local fullPath = fs.combine(notesDir, filename)
- if not fs.isDir(fullPath) then
- local file = fs.open(fullPath, "r")
- local preview = ""
- if file then
- preview = file.readLine() or "<Empty Note>"
- file.close()
- else
- preview = "<Unreadable>"
- end
- table.insert(list, {
- filename = filename,
- path = fullPath,
- preview = preview
- })
- end
- end
- return list
- end
- -- Prompt Input Helper
- local function promptInput(title, defaultVal)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(2, h - 2)
- term.write(string.rep(" ", w - 2))
- term.setCursorPos(2, h - 2)
- term.write(title .. ": ")
- term.setTextColor(colors.yellow)
- local input = read(nil, nil, nil, defaultVal or "")
- return (input and #input > 0) and input or nil
- end
- -- Helper to wrap text cleanly across terminal columns
- local function wrapText(text, width)
- local lines = {}
- for line in text:gmatch("[^\r\n]+") do
- while #line > width do
- table.insert(lines, string.sub(line, 1, width))
- line = string.sub(line, width + 1)
- end
- table.insert(lines, line)
- end
- if #lines == 0 then lines = {"<Empty Note>"} end
- return lines
- end
- local scrollOffset = 0
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- -- Header
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.write(" Notes " .. string.rep(" ", w - 8))
- -- New Note Button
- term.setCursorPos(w - 6, 1)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- term.write(" [ + ] ")
- local notes = getNotesList()
- -- Render Notes List (Previews)
- if #notes == 0 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.gray)
- term.setCursorPos(2, 4)
- term.write("No notes found.")
- term.setCursorPos(2, 5)
- term.write("Tap [ + ] to create one!")
- else
- local maxVisible = h - 4
- for i = 1, maxVisible do
- local noteIdx = i + scrollOffset
- if notes[noteIdx] then
- local note = notes[noteIdx]
- term.setCursorPos(2, 2 + i)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- -- Truncate preview text to fit screen width cleanly
- local displayStr = note.preview
- if #displayStr > (w - 4) then
- displayStr = string.sub(displayStr, 1, w - 7) .. "..."
- end
- term.write(string.format(" %-" .. (w - 4) .. "s ", displayStr))
- end
- end
- end
- -- Render Bottom Nav Bar
- drawBottomBar()
- -- Event Listener
- local event, p1, clickX, clickY = os.pullEvent()
- if event == "mouse_click" and p1 == 1 then
- -- Intercept Bottom Bar Click
- if clickY == h then
- return "bottom_bar", clickX
- end
- -- Create New Note
- if clickY == 1 and clickX >= (w - 6) then
- local newText = promptInput("New Note")
- if newText then
- local filename = "note_" .. os.time() .. ".txt"
- local file = fs.open(fs.combine(notesDir, filename), "w")
- file.write(newText)
- file.close()
- end
- end
- -- Tap on a Note Preview to View Entire Note
- if clickY >= 3 and clickY < h - 1 then
- local clickedIdx = (clickY - 2) + scrollOffset
- if notes[clickedIdx] then
- local targetNote = notes[clickedIdx]
- local viewingNote = true
- local noteScroll = 0
- while viewingNote do
- term.setBackgroundColor(colors.black)
- term.clear()
- -- Reader Header
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.write(" Note View " .. string.rep(" ", w - 18))
- term.setCursorPos(w - 6, 1)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.write(" [ < ] ")
- -- Read Full Note Content
- local file = fs.open(targetNote.path, "r")
- local fullContent = file and file.readAll() or ""
- if file then file.close() end
- local formattedLines = wrapText(fullContent, w - 2)
- -- Display Full Wrapped Text
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- local maxViewLines = h - 5
- for l = 1, maxViewLines do
- local lineIdx = l + noteScroll
- if formattedLines[lineIdx] then
- term.setCursorPos(2, 2 + l)
- term.write(formattedLines[lineIdx])
- end
- end
- -- Action Bar (Delete / Edit Buttons)
- term.setCursorPos(2, h - 2)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.write(" [ Delete ] ")
- term.setCursorPos(14, h - 2)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- term.write(" [ Edit ] ")
- drawBottomBar()
- local vevent, vp1, vx, vy = os.pullEvent()
- if vevent == "mouse_click" and vp1 == 1 then
- -- Intercept Bottom Nav Bar
- if vy == h then
- return "bottom_bar", vx
- end
- -- Back Button
- if vy == 1 and vx >= (w - 6) then
- viewingNote = false
- end
- -- Delete Note
- if vy == (h - 2) and vx >= 2 and vx <= 11 then
- fs.delete(targetNote.path)
- viewingNote = false
- end
- -- Edit Note
- if vy == (h - 2) and vx >= 14 and vx <= 21 then
- local updatedText = promptInput("Edit Note", fullContent)
- if updatedText then
- local f = fs.open(targetNote.path, "w")
- f.write(updatedText)
- f.close()
- end
- end
- elseif vevent == "mouse_scroll" then
- if vp1 > 0 and (noteScroll + (h - 5)) < #formattedLines then
- noteScroll = noteScroll + 1
- elseif vp1 < 0 and noteScroll > 0 then
- noteScroll = noteScroll - 1
- end
- end
- end
- end
- end
- elseif event == "mouse_scroll" then
- if p1 > 0 and (scrollOffset + (h - 4)) < #notes then
- scrollOffset = scrollOffset + 1
- elseif p1 < 0 and scrollOffset > 0 then
- scrollOffset = scrollOffset - 1
- end
- end
- end
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment