Philipou

open todo

Aug 21st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. -- OpenTODO
  2. -- A simple program that lets you keep a list of TODO's with checkboxes next to them
  3. -- Allows adding/deleting entries, checking/unchecking entries
  4. -- Saves entries to file on quit and reads on start
  5. -- Made by Forecaster
  6.  
  7. local c = require("component")
  8. local term = require("term")
  9. local cereal = require("serialization")
  10. local gpu = c.gpu
  11. local color = require("colors")
  12. local event = require("event")
  13. local fs = require("filesystem")
  14.  
  15. fs.makeDirectory("/usr/share/opentodo/")
  16.  
  17. local file = io.open("/usr/share/opentodo/save.tbl","r")
  18. local entries
  19. if file ~= nil then
  20.   local contents = file:read("*a")
  21.   if contents ~= "" then
  22.     entries = cereal.unserialize(contents)
  23.   else
  24.     entries = {}
  25.   end
  26.   file:close()
  27. else
  28.   entries = {}
  29. end
  30. local selected = 1
  31.  
  32. local function count(table)
  33.   local counter = 0
  34.   for _ in pairs(table) do
  35.     counter = counter + 1
  36.   end
  37.   return counter
  38. end
  39.  
  40. local function drawEntries()
  41.   term.clear()
  42.   for key, value in pairs(entries) do
  43.     if selected == key then
  44.       gpu.setBackground(0x4B4B4B)
  45.     end
  46.     if value[1] == true then
  47.       print("[x] " .. value[2])
  48.     else
  49.       print("[ ] " .. value[2])
  50.     end
  51.     gpu.setBackground(0x000000)
  52.   end
  53.   if selected == count(entries) + 1 then
  54.     gpu.setBackground(0x4B4B4B)
  55.   end
  56.   print("New entry (Press Enter)")
  57.   gpu.setBackground(0x000000)
  58.   print("Left Arrow: Delete Entry, Right Arrow: Toggle Checked, ctrl + c: Exit")
  59. end
  60.  
  61. local function incrementSelection()
  62.   selected = selected + 1
  63.   if  selected > count(entries) + 1 then
  64.     selected = count(entries) + 1
  65.   end
  66. end
  67.  
  68. local function decrementSelection()
  69.   selected = selected - 1
  70.   if selected == 0 then
  71.     selected = 1
  72.   end
  73. end
  74.  
  75. local function deleteEntry()
  76.   if entries[selected] ~= nil then
  77.     entries[selected] = nil
  78.     drawEntries()
  79.   end
  80. end
  81.  
  82. local function toggleChecked()
  83.   if entries[selected] ~= nil then
  84.     entries[selected][1] = not entries[selected][1]
  85.   end
  86.   drawEntries()
  87. end
  88.  
  89. drawEntries()
  90.  
  91. local run = true
  92. while run do
  93.   local event, _, _, data = event.pull()
  94.   if event == "interrupted" then
  95.     run = false
  96.   elseif event == "key_down" then
  97.     if data == 200 then
  98.       decrementSelection()
  99.       drawEntries()
  100.     elseif data == 208 then
  101.       incrementSelection()
  102.       drawEntries()
  103.     elseif data == 205 then
  104.       toggleChecked()
  105.       drawEntries()
  106.     elseif data == 203 then
  107.       deleteEntry()
  108.     elseif data == 28 then
  109.       if selected == count(entries) + 1 then
  110.         term.write("New entry: ")
  111.         local input = term.read()
  112.         if input ~= "" then
  113.           table.insert(entries, {false, input})
  114.           drawEntries()
  115.         end
  116.       end
  117.     end
  118.   end
  119. end
  120.  
  121. term.clear()
  122.  
  123. local file = io.open("/usr/share/opentodo/save.tbl","w")
  124. if file == nil then
  125.   error("Couldn't open file '/usr/share/opentodo/save.tbl'")
  126. end
  127. local cerealEntries = cereal.serialize(entries)
  128. local result, message = file:write(cerealEntries)
  129. file:close()
  130.  
  131. if result == nil then
  132.   error(message)
  133. end
  134.  
  135. print("Successfully saved and quit")
Add Comment
Please, Sign In to add comment