openTODO.lua 2.94 KB
Newer Older
Martin's avatar
Martin committed
1
-- OpenTODO
Martin's avatar
Martin committed
2 3 4 5 6
-- A simple program that lets you keep a list of TODO's with checkboxes next to them
-- Allows adding/deleting entries, checking/unchecking entries
-- Saves entries to file on quit and reads on start
-- Made by Forecaster

Martin's avatar
Martin committed
7 8 9 10 11 12
local c = require("component")
local term = require("term")
local cereal = require("serialization")
local gpu = c.gpu
local color = require("colors")
local event = require("event")
Martin's avatar
Martin committed
13
local fs = require("filesystem")
Martin's avatar
Martin committed
14

Martin's avatar
Martin committed
15 16 17
fs.makeDirectory("/usr/share/opentodo/")

local file = io.open("/usr/share/opentodo/save.tbl","r")
Martin's avatar
Martin committed
18 19 20 21 22 23 24 25
local entries
if file ~= nil then
  local contents = file:read("*a")
  if contents ~= "" then
    entries = cereal.unserialize(contents)
  else
    entries = {}
  end
Martin's avatar
Martin committed
26
  file:close()
Martin's avatar
Martin committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
else
  entries = {}
end
local selected = 1

local function count(table)
  local counter = 0
  for _ in pairs(table) do
    counter = counter + 1
  end
  return counter
end

local function drawEntries()
  term.clear()
  for key, value in pairs(entries) do
    if selected == key then
      gpu.setBackground(0x4B4B4B)
    end
    if value[1] == true then
      print("[x] " .. value[2])
    else
      print("[ ] " .. value[2])
    end
    gpu.setBackground(0x000000)
  end
  if selected == count(entries) + 1 then
    gpu.setBackground(0x4B4B4B)
  end
  print("New entry (Press Enter)")
  gpu.setBackground(0x000000)
Martin's avatar
Martin committed
58
  print("Left Arrow: Delete Entry, Right Arrow: Toggle Checked, ctrl + x: Exit")
Martin's avatar
Martin committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
end

local function incrementSelection()
  selected = selected + 1
  if  selected > count(entries) + 1 then
    selected = count(entries) + 1
  end
end

local function decrementSelection()
  selected = selected - 1
  if selected == 0 then
    selected = 1
  end
end

local function deleteEntry()
  if entries[selected] ~= nil then
    entries[selected] = nil
Martin's avatar
Martin committed
78
	drawEntries()
Martin's avatar
Martin committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  end
end

local function toggleChecked()
  if entries[selected] ~= nil then
    entries[selected][1] = not entries[selected][1]
  end
  drawEntries()
end

drawEntries()

local run = true
while run do
  local event, _, _, data = event.pull()
  if event == "interrupted" then
    run = false
  elseif event == "key_down" then
    if data == 200 then
      decrementSelection()
      drawEntries()
    elseif data == 208 then
      incrementSelection()
      drawEntries()
    elseif data == 205 then
      toggleChecked()
      drawEntries()
    elseif data == 203 then
      deleteEntry()
    elseif data == 28 then
      if selected == count(entries) + 1 then
Martin's avatar
Martin committed
110
		term.write("New entry: ")
Martin's avatar
Martin committed
111 112 113 114 115 116 117 118 119 120 121 122
        local input = term.read()
        if input ~= "" then
          table.insert(entries, {false, input})
          drawEntries()
        end
      end
    end
  end
end

term.clear()

Martin's avatar
Martin committed
123
local file = io.open("/usr/share/opentodo/save.tbl","w")
Martin's avatar
Martin committed
124
if file == nil then
Martin's avatar
Martin committed
125
  error("Couldn't open file '/usr/share/opentodo/save.tbl'")
Martin's avatar
Martin committed
126
end
127 128 129
local cerealEntries = cereal.serialize(entries)
local result, message = file:write(cerealEntries)
file:close()
Martin's avatar
Martin committed
130 131 132 133 134 135

if result == nil then
  error(message)
end

print("Successfully saved and quit")