Advertisement
Alexr360

Pocket Todo

Apr 27th, 2025 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.27 KB | None | 0 0
  1. -- pocket_todo.lua
  2. -- A pocket computer todo list with notes, completed section, clickable buttons, and backup command
  3.  
  4. -- Data files
  5. local TODO_FILE      = "todo.txt"
  6. local NOTES_FILE     = "notes.txt"
  7. local COMPLETED_FILE = "completed.txt"
  8.  
  9. -- Data tables
  10. local items     = {}
  11. local notes     = ""
  12. local completed = {}
  13.  
  14. -- Utility: load list from file
  15. local function loadList(file, tbl)
  16.   if fs.exists(file) then
  17.     local f = fs.open(file, "r")
  18.     local line = f.readLine()
  19.     while line do
  20.       table.insert(tbl, line)
  21.       line = f.readLine()
  22.     end
  23.     f.close()
  24.   end
  25. end
  26.  
  27. -- Initial load
  28. loadList(TODO_FILE, items)
  29. loadList(COMPLETED_FILE, completed)
  30. if fs.exists(NOTES_FILE) then
  31.   local f = fs.open(NOTES_FILE, "r")
  32.   notes = f.readAll() or ""
  33.   f.close()
  34. end
  35.  
  36. -- Utility: save all data
  37. local function save()
  38.   local f = fs.open(TODO_FILE, "w")
  39.   for _,v in ipairs(items) do f.writeLine(v) end
  40.   f.close()
  41.   local f2 = fs.open(COMPLETED_FILE, "w")
  42.   for _,v in ipairs(completed) do f2.writeLine(v) end
  43.   f2.close()
  44.   local f3 = fs.open(NOTES_FILE, "w")
  45.   f3.write(notes)
  46.   f3.close()
  47. end
  48.  
  49. -- Button mapping
  50. local buttons = {}
  51.  
  52. -- Redraw UI to term
  53. local function redraw()
  54.   term.setBackgroundColor(colors.black)
  55.   term.clear()
  56.   term.setCursorPos(1,1)
  57.   term.setTextColor(colors.white)
  58.   local w,h = term.getSize()
  59.   local y = 1
  60.   buttons = {}
  61.  
  62.   -- Notes section
  63.   if notes:match("%S") then
  64.     term.setCursorPos(1,y)
  65.     term.write("Notes:")
  66.     y = y + 1
  67.     local line = notes
  68.     if #line > w then line = line:sub(1, w-3) .. "..." end
  69.     term.setCursorPos(1, y)
  70.     term.write(line)
  71.     y = y + 1
  72.     -- blank line after notes
  73.     y = y + 1
  74.   end
  75.  
  76.   -- To-Do List
  77.   if y <= h then
  78.     term.setCursorPos(1,y)
  79.     term.write("To-Do List:")
  80.     y = y + 1
  81.     for i = 1, math.min(#items, h - y) do
  82.       local text = string.format("%2d. %s", i, items[i])
  83.       if #text > w-6 then text = text:sub(1, w-9) .. "..." end
  84.       term.setCursorPos(1, y + i - 1)
  85.       term.write(text)
  86.       -- Done button
  87.       local bx = w - 3
  88.       term.setCursorPos(bx, y + i - 1)
  89.       term.write("[D]")
  90.       table.insert(buttons, {x1 = bx, x2 = bx + 2, y = y + i - 1, type = "done", idx = i})
  91.     end
  92.     y = y + math.min(#items, h - y) + 1
  93.   end
  94.  
  95.   -- Completed Section
  96.   if #completed > 0 and y <= h then
  97.     term.setCursorPos(1,y)
  98.     term.write("Completed:")
  99.     y = y + 1
  100.     for i = 1, math.min(#completed, h - y + 1) do
  101.       local entry = completed[i]
  102.       local text = "- " .. entry
  103.       if #text > w-10 then text = text:sub(1, w-13) .. "..." end
  104.       term.setCursorPos(1, y + i - 1)
  105.       term.write(text)
  106.       -- Restore button
  107.       local rx = w - 7
  108.       term.setCursorPos(rx, y + i - 1)
  109.       term.write("[R]")
  110.       table.insert(buttons, {x1 = rx, x2 = rx + 2, y = y + i - 1, type = "restore", idx = i})
  111.       -- Delete button
  112.       local dx = w - 3
  113.       term.setCursorPos(dx, y + i - 1)
  114.       term.write("[X]")
  115.       table.insert(buttons, {x1 = dx, x2 = dx + 2, y = y + i - 1, type = "delete", idx = i})
  116.     end
  117.   end
  118. end
  119.  
  120. -- Handle mouse clicks
  121. local function handleClick(x,y)
  122.   for _,b in ipairs(buttons) do
  123.     if y == b.y and x >= b.x1 and x <= b.x2 then
  124.       if b.type == "done" then
  125.         table.insert(completed, items[b.idx])
  126.         table.remove(items, b.idx)
  127.       elseif b.type == "restore" then
  128.         table.insert(items, completed[b.idx])
  129.         table.remove(completed, b.idx)
  130.       elseif b.type == "delete" then
  131.         table.remove(completed, b.idx)
  132.       end
  133.       save()
  134.       redraw()
  135.       return
  136.     end
  137.   end
  138. end
  139.  
  140. -- Initial draw
  141. redraw()
  142.  
  143. -- Main loops: mouse and command
  144. parallel.waitForAny(
  145.   function()
  146.     while true do
  147.       local event, button, x, y = os.pullEvent("mouse_click")
  148.       if button == 1 then handleClick(x,y) end
  149.     end
  150.   end,
  151.   function()
  152.     while true do
  153.       term.setCursorPos(1,1)
  154.       local input = read()
  155.       local cmd, rest = input:match("^(%S+)%s*(.*)$")
  156.       if not cmd then
  157.         cmd = input
  158.         rest = ""
  159.       end
  160.       if cmd=="add" and #rest>0 then
  161.         table.insert(items,rest)
  162.       elseif cmd=="remove" and tonumber(rest) and items[tonumber(rest)] then
  163.         table.remove(items,tonumber(rest))
  164.       elseif cmd=="done" and tonumber(rest) and items[tonumber(rest)] then
  165.         table.insert(completed,items[tonumber(rest)]); table.remove(items,tonumber(rest))
  166.       elseif cmd=="clear" then
  167.         items = {}
  168.       elseif cmd=="clearcompleted" then
  169.         completed = {}
  170.       elseif cmd=="backup" then
  171.         -- delete existing backups to prevent file exists errors
  172.         if fs.exists(BUP_TODO) then fs.delete(BUP_TODO) end
  173.         if fs.exists(BUP_COMPLETED) then fs.delete(BUP_COMPLETED) end
  174.         if fs.exists(BUP_NOTES) then fs.delete(BUP_NOTES) end
  175.         -- perform backup
  176.         if fs.exists(TODO_FILE) then fs.copy(TODO_FILE, BUP_TODO) end
  177.         if fs.exists(COMPLETED_FILE) then fs.copy(COMPLETED_FILE, BUP_COMPLETED) end
  178.         if fs.exists(NOTES_FILE) then fs.copy(NOTES_FILE, BUP_NOTES) end
  179.         print("Backup complete.")
  180.         os.sleep(0.5)
  181.       elseif cmd=="sync" then
  182.         -- delete existing files before sync
  183.         if fs.exists(TODO_FILE) then fs.delete(TODO_FILE) end
  184.         if fs.exists(COMPLETED_FILE) then fs.delete(COMPLETED_FILE) end
  185.         if fs.exists(NOTES_FILE) then fs.delete(NOTES_FILE) end
  186.         -- overwrite existing files from backup
  187.         if fs.exists(BUP_TODO) then fs.copy(BUP_TODO, TODO_FILE) end
  188.         if fs.exists(BUP_COMPLETED) then fs.copy(BUP_COMPLETED, COMPLETED_FILE) end
  189.         if fs.exists(BUP_NOTES) then fs.copy(BUP_NOTES, NOTES_FILE) end
  190.         -- reload data
  191.         items = loadList(TODO_FILE)
  192.         completed = loadList(COMPLETED_FILE)
  193.         if fs.exists(NOTES_FILE) then local f = fs.open(NOTES_FILE, "r"); notes = f.readAll() or ""; f.close() else notes = "" end
  194.         print("Sync complete.")
  195.         os.sleep(0.5)
  196.       elseif cmd=="note" then
  197.         notes = rest or ""
  198.       elseif cmd=="clearnote" then
  199.         notes = ""
  200.       elseif cmd=="exit" then
  201.         print("Goodbye!") os.exit()
  202.       else
  203.         print("Invalid command. Press Enter to continue.") read()
  204.       end
  205.       save()
  206.       redraw()
  207.     end
  208.   end
  209. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement