zamkamec

notes

Sep 24th, 2026 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. -- Notes App (apps/notes.lua)
  2. return {
  3. name = "Notes",
  4. color = colors.yellow,
  5. symbol = "N",
  6. page = 1,
  7. run = function(w, h, drawBottomBar)
  8. local notesDir = "notes"
  9.  
  10. -- 1. Ensure notes directory exists
  11. if not fs.exists(notesDir) then
  12. fs.makeDir(notesDir)
  13. elseif not fs.isDir(notesDir) then
  14. fs.delete(notesDir)
  15. fs.makeDir(notesDir)
  16. end
  17.  
  18. local function getNotesList()
  19. local list = {}
  20. local files = fs.list(notesDir)
  21. for _, filename in ipairs(files) do
  22. local fullPath = fs.combine(notesDir, filename)
  23. if not fs.isDir(fullPath) then
  24. local file = fs.open(fullPath, "r")
  25. local preview = ""
  26. if file then
  27. preview = file.readLine() or "<Empty Note>"
  28. file.close()
  29. else
  30. preview = "<Unreadable>"
  31. end
  32. table.insert(list, {
  33. filename = filename,
  34. path = fullPath,
  35. preview = preview
  36. })
  37. end
  38. end
  39. return list
  40. end
  41.  
  42. -- Prompt Input Helper
  43. local function promptInput(title, defaultVal)
  44. term.setBackgroundColor(colors.gray)
  45. term.setTextColor(colors.white)
  46. term.setCursorPos(2, h - 2)
  47. term.write(string.rep(" ", w - 2))
  48. term.setCursorPos(2, h - 2)
  49. term.write(title .. ": ")
  50. term.setTextColor(colors.yellow)
  51.  
  52. local input = read(nil, nil, nil, defaultVal or "")
  53. return (input and #input > 0) and input or nil
  54. end
  55.  
  56. -- Helper to wrap text cleanly across terminal columns
  57. local function wrapText(text, width)
  58. local lines = {}
  59. for line in text:gmatch("[^\r\n]+") do
  60. while #line > width do
  61. table.insert(lines, string.sub(line, 1, width))
  62. line = string.sub(line, width + 1)
  63. end
  64. table.insert(lines, line)
  65. end
  66. if #lines == 0 then lines = {"<Empty Note>"} end
  67. return lines
  68. end
  69.  
  70. local scrollOffset = 0
  71.  
  72. while true do
  73. term.setBackgroundColor(colors.black)
  74. term.clear()
  75.  
  76. -- Header
  77. term.setBackgroundColor(colors.gray)
  78. term.setTextColor(colors.white)
  79. term.setCursorPos(1, 1)
  80. term.write(" Notes " .. string.rep(" ", w - 8))
  81.  
  82. -- New Note Button
  83. term.setCursorPos(w - 6, 1)
  84. term.setBackgroundColor(colors.yellow)
  85. term.setTextColor(colors.black)
  86. term.write(" [ + ] ")
  87.  
  88. local notes = getNotesList()
  89.  
  90. -- Render Notes List (Previews)
  91. if #notes == 0 then
  92. term.setBackgroundColor(colors.black)
  93. term.setTextColor(colors.gray)
  94. term.setCursorPos(2, 4)
  95. term.write("No notes found.")
  96. term.setCursorPos(2, 5)
  97. term.write("Tap [ + ] to create one!")
  98. else
  99. local maxVisible = h - 4
  100. for i = 1, maxVisible do
  101. local noteIdx = i + scrollOffset
  102. if notes[noteIdx] then
  103. local note = notes[noteIdx]
  104. term.setCursorPos(2, 2 + i)
  105. term.setBackgroundColor(colors.gray)
  106. term.setTextColor(colors.white)
  107.  
  108. -- Truncate preview text to fit screen width cleanly
  109. local displayStr = note.preview
  110. if #displayStr > (w - 4) then
  111. displayStr = string.sub(displayStr, 1, w - 7) .. "..."
  112. end
  113. term.write(string.format(" %-" .. (w - 4) .. "s ", displayStr))
  114. end
  115. end
  116. end
  117.  
  118. -- Render Bottom Nav Bar
  119. drawBottomBar()
  120.  
  121. -- Event Listener
  122. local event, p1, clickX, clickY = os.pullEvent()
  123.  
  124. if event == "mouse_click" and p1 == 1 then
  125. -- Intercept Bottom Bar Click
  126. if clickY == h then
  127. return "bottom_bar", clickX
  128. end
  129.  
  130. -- Create New Note
  131. if clickY == 1 and clickX >= (w - 6) then
  132. local newText = promptInput("New Note")
  133. if newText then
  134. local filename = "note_" .. os.time() .. ".txt"
  135. local file = fs.open(fs.combine(notesDir, filename), "w")
  136. file.write(newText)
  137. file.close()
  138. end
  139. end
  140.  
  141. -- Tap on a Note Preview to View Entire Note
  142. if clickY >= 3 and clickY < h - 1 then
  143. local clickedIdx = (clickY - 2) + scrollOffset
  144. if notes[clickedIdx] then
  145. local targetNote = notes[clickedIdx]
  146. local viewingNote = true
  147. local noteScroll = 0
  148.  
  149. while viewingNote do
  150. term.setBackgroundColor(colors.black)
  151. term.clear()
  152.  
  153. -- Reader Header
  154. term.setBackgroundColor(colors.gray)
  155. term.setTextColor(colors.white)
  156. term.setCursorPos(1, 1)
  157. term.write(" Note View " .. string.rep(" ", w - 18))
  158.  
  159. term.setCursorPos(w - 6, 1)
  160. term.setBackgroundColor(colors.blue)
  161. term.setTextColor(colors.white)
  162. term.write(" [ < ] ")
  163.  
  164. -- Read Full Note Content
  165. local file = fs.open(targetNote.path, "r")
  166. local fullContent = file and file.readAll() or ""
  167. if file then file.close() end
  168.  
  169. local formattedLines = wrapText(fullContent, w - 2)
  170.  
  171. -- Display Full Wrapped Text
  172. term.setBackgroundColor(colors.black)
  173. term.setTextColor(colors.white)
  174. local maxViewLines = h - 5
  175. for l = 1, maxViewLines do
  176. local lineIdx = l + noteScroll
  177. if formattedLines[lineIdx] then
  178. term.setCursorPos(2, 2 + l)
  179. term.write(formattedLines[lineIdx])
  180. end
  181. end
  182.  
  183. -- Action Bar (Delete / Edit Buttons)
  184. term.setCursorPos(2, h - 2)
  185. term.setBackgroundColor(colors.red)
  186. term.setTextColor(colors.white)
  187. term.write(" [ Delete ] ")
  188.  
  189. term.setCursorPos(14, h - 2)
  190. term.setBackgroundColor(colors.yellow)
  191. term.setTextColor(colors.black)
  192. term.write(" [ Edit ] ")
  193.  
  194. drawBottomBar()
  195.  
  196. local vevent, vp1, vx, vy = os.pullEvent()
  197.  
  198. if vevent == "mouse_click" and vp1 == 1 then
  199. -- Intercept Bottom Nav Bar
  200. if vy == h then
  201. return "bottom_bar", vx
  202. end
  203.  
  204. -- Back Button
  205. if vy == 1 and vx >= (w - 6) then
  206. viewingNote = false
  207. end
  208.  
  209. -- Delete Note
  210. if vy == (h - 2) and vx >= 2 and vx <= 11 then
  211. fs.delete(targetNote.path)
  212. viewingNote = false
  213. end
  214.  
  215. -- Edit Note
  216. if vy == (h - 2) and vx >= 14 and vx <= 21 then
  217. local updatedText = promptInput("Edit Note", fullContent)
  218. if updatedText then
  219. local f = fs.open(targetNote.path, "w")
  220. f.write(updatedText)
  221. f.close()
  222. end
  223. end
  224. elseif vevent == "mouse_scroll" then
  225. if vp1 > 0 and (noteScroll + (h - 5)) < #formattedLines then
  226. noteScroll = noteScroll + 1
  227. elseif vp1 < 0 and noteScroll > 0 then
  228. noteScroll = noteScroll - 1
  229. end
  230. end
  231. end
  232. end
  233. end
  234.  
  235. elseif event == "mouse_scroll" then
  236. if p1 > 0 and (scrollOffset + (h - 4)) < #notes then
  237. scrollOffset = scrollOffset + 1
  238. elseif p1 < 0 and scrollOffset > 0 then
  239. scrollOffset = scrollOffset - 1
  240. end
  241. end
  242. end
  243. end
  244. }
Advertisement
Add Comment
Please, Sign In to add comment