j3d247

todo list

Mar 12th, 2014
833
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local list = {}
  2. local sensor = peripheral.wrap("bottom")
  3. local d
  4. for _, name in pairs(peripheral.getNames()) do
  5.     if peripheral.getType(name) == "monitor" then
  6.         d = peripheral.wrap(name)
  7.     end
  8. end
  9. local maxX,maxY = d.getSize()
  10. function cwrite(text)
  11.   local w, h = d.getSize()
  12.   local x, y = d.getCursorPos()
  13.   x = math.max(math.floor((w/2) - (#text / 2)), 0)
  14.   d.setCursorPos(x,y)
  15.   d.write(text)
  16.   d.setCursorPos(x,y+1)
  17. end
  18. function header()
  19.     d.setBackgroundColor(colors.cyan)
  20.     for i=1, 3 do
  21.     local currx, curry = d.getCursorPos()  
  22.         if i == 2 then
  23.             term.redirect(d)
  24.             paintutils.drawLine(1,curry,maxX,curry,colors.cyan)
  25.             term.restore()
  26.             cwrite("TO-DO LIST")
  27.         else
  28.             for i=1, maxX do
  29.                 d.write("-")
  30.             end
  31.         end
  32.     d.setCursorPos(currx, curry + 1)   
  33.     end
  34.     d.setBackgroundColor(colors.black)
  35. end
  36. function listtomon()
  37.     if list then
  38.     d.setCursorPos(2,5)
  39.         for k,v in pairs(list) do
  40.             --local currx, curry = d.getCursorPos()
  41.             --d.write(k..") "..v)
  42.             --d.setCursorPos(currx, curry + 1)
  43.             term.redirect(d)
  44.             print(k..") "..v)
  45.             x,y = term.getCursorPos()
  46.             term.setCursorPos(x+1,y+1)
  47.             term.restore()
  48.         end
  49.     end
  50. end
  51. local function saveAll()
  52.     save = fs.open("todolist.sav","w")
  53.     save.write(textutils.serialize(list))
  54.     save.close()
  55. end
  56. local function loadAll()
  57.     if fs.exists("todolist.sav") then
  58.         load = fs.open("todolist.sav","r")
  59.         list = textutils.unserialize(load.readAll())
  60.         load.close()
  61.     end
  62. end
  63. function drawScreen()
  64.     loadAll()
  65.     d.clear()
  66.     term.clear()
  67.     d.setCursorPos(1,1)
  68.     term.setCursorPos(1,1)
  69.     d.setBackgroundColor(colors.black)
  70.     header()
  71.     listtomon()
  72. end
  73. function userInput()
  74.     while true do
  75.     listtomon()
  76.     print("<add 'message'>/<del key> (eg. add test  / or del 1)")
  77.     input = read()
  78.     print(" ")
  79.     if string.sub(input,0,3) == "del" then
  80.         key = string.sub(input,5)
  81.         for k,v in pairs(list) do
  82.             if tostring(k) == key then
  83.                 list[k] = nil
  84.                 break
  85.             end
  86.         end
  87.         saveAll()
  88.     elseif string.sub(input,0,3) == "add" then
  89.         table.insert(list, string.sub(input,5))
  90.         saveAll()
  91.     else
  92.         print("Invalid Attempt, Please Start with 'add' or 'del' followed by either your new item or the item's key(number) to delete")
  93.     end
  94.     drawScreen()
  95.     end
  96. end
  97. -- THE PROGRAM
  98. drawScreen()
  99. userInput()
RAW Paste Data