LivioBrunner

To Do List

Nov 20th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. local version = "1.3"
  2. local todos = { }
  3. local h = true
  4. local mon = peripheral.wrap("right")
  5. function load(name)
  6.   local file = fs.open(name,"r")
  7.   local data = file.readAll()
  8.   file.close()
  9.   return textutils.unserialize(data)
  10. end
  11. function save(table,name)
  12.   local file = fs.open(name,"w")
  13.   file.write(textutils.serialize(table))
  14.   file.close()
  15. end
  16. function tablelength(T)
  17.   local count = 0
  18.   for _ in pairs(T) do count = count + 1 end
  19.   return count
  20. end
  21. local function centerText(text, object)
  22.     if object == nil then
  23.         object = term
  24.     end
  25.         local x,y = object.getSize()
  26.         local x2,y2 = object.getCursorPos()
  27.         object.setCursorPos(math.floor((x / 2) - (string.len(text) / 2)), y2)
  28.         object.write(text)
  29. end
  30. function create(x,y,width,height,color,mon)
  31.         mon.setCursorPos(x,y)
  32.         for j = 0, height do
  33.                 mon.setCursorPos(x,y+j)
  34.                 for i = 0,width do
  35.                         mon.setBackgroundColor(color)
  36.                         mon.write(" ")
  37.                 end
  38.         end
  39. end
  40. function clearAll(mon, color)
  41.         local w,h = mon.getSize()
  42.         for i = 0,h do
  43.                 for j = 0, w do
  44.                         mon.setCursorPos(1+j,1+i)
  45.                         mon.setBackgroundColor(color)
  46.                         mon.write(" ")
  47.                 end
  48.         end
  49. end
  50. function reload(object)
  51.     if object == nil then
  52.         object = term
  53.     end
  54.     if fs.exists("saves/todos") then
  55.         todos = load("saves/todos")
  56.         background(object)
  57.         for i = 1, tablelength(todos) do
  58.         object.setCursorPos(1,3+i)
  59.         centerText(i..". "..todos[i], object)
  60.         end
  61.     end
  62.    
  63. end
  64. function background(object)
  65.     if object == nil then
  66.         object = term
  67.     end
  68.     local x,y = object.getSize()
  69.     create(x/2-20,3,40,tablelength(todos)+1,colors.black,object)
  70. end
  71.     clearAll(term, colors.blue)
  72.     clearAll(mon, colors.blue)
  73.     mon.clear()
  74.     term.clear()
  75.     background(mon)
  76.     background(term)
  77. reload(term)
  78. reload(mon)
  79.  
  80. while true do
  81.     mon.setBackgroundColor(colors.blue)
  82.     term.setBackgroundColor(colors.blue)
  83.     mon.setCursorPos(1,1)
  84.     term.setCursorPos(1,1)
  85.     centerText("To Do list v.".. version)
  86.     centerText("To Do list v.".. version, mon)
  87.     term.setCursorPos(1,2)
  88.     centerText("To Do:")
  89.     message = read()
  90.     if message == "delete" then
  91.         clearAll(term, colors.blue)
  92.     clearAll(mon, colors.blue)
  93.     mon.clear()
  94.     term.clear()
  95.     background(mon)
  96.     background(term)
  97.     reload(term)
  98.     reload(mon)
  99.         while h do
  100.             mon.setBackgroundColor(colors.blue)
  101.             term.setBackgroundColor(colors.blue)
  102.             mon.setCursorPos(1,1)
  103.             term.setCursorPos(1,1)
  104.             centerText("To Do list v.".. version)
  105.             centerText("To Do list v.".. version, mon)
  106.             term.setCursorPos(1,2)
  107.             term.clearLine()
  108.             term.setTextColor(colors.red)
  109.             centerText("Delete:")
  110.             delete = read()
  111.             if delete == "" then
  112.                 h = false
  113.                 term.clearLine()
  114.             else
  115.                 h = true
  116.                 todos = load("saves/todos")
  117.                 table.remove(todos, tonumber(delete))
  118.                 save(todos, "saves/todos")
  119.             end
  120.             term.setTextColor(colors.white)
  121.             term.clear()
  122.             mon.clear()
  123.             reload(term)
  124.             reload(mon)
  125.         end
  126.        
  127.     elseif message == "change" then
  128.         clearAll(term, colors.blue)
  129.         clearAll(mon, colors.blue)
  130.         mon.clear()
  131.         term.clear()
  132.         background(mon)
  133.         background(term)
  134.         reload(term)
  135.         reload(mon)
  136.         mon.setBackgroundColor(colors.blue)
  137.         term.setBackgroundColor(colors.blue)
  138.         mon.setCursorPos(1,1)
  139.         term.setCursorPos(1,1)
  140.         centerText("To Do list v.".. version)
  141.         centerText("To Do list v.".. version, mon)
  142.         term.setCursorPos(1,2)
  143.         term.clearLine()
  144.         term.setTextColor(colors.orange)
  145.         centerText("Change:")
  146.         changenum = tonumber(read())
  147.         if change == "" then
  148.             term.setCursorPos(1,2)
  149.             term.clearLine()
  150.         else
  151.             term.setCursorPos(1,2)
  152.             term.clearLine()
  153.             centerText("To:")
  154.             tonum = tonumber(read())
  155.         end
  156.         todos = load("saves/todos")
  157.         to = todos[tonum]
  158.         change = todos[changenum]
  159.         todos[tonum] = change
  160.         todos[changenum] = to
  161.         save(todos, "saves/todos")
  162.         term.setTextColor(colors.white)
  163.         term.clear()
  164.         mon.clear()
  165.         reload(term)
  166.         reload(mon)
  167.     else
  168.     clearAll(term, colors.blue)
  169.     clearAll(mon, colors.blue)
  170.     table.insert(todos, message)
  171.     fs.makeDir("saves")
  172.     save(todos, "saves/todos")
  173.     term.clear()
  174.     mon.clear()
  175.     reload(term)
  176.     reload(mon)
  177.     end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment