Advertisement
dragonshardz

Tjakka5 Todo Bugfix

Sep 22nd, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. term.clear()
  2. term.setCursorPos(1, 1)
  3.  
  4. local side = nil
  5. for _, s in pairs(rs.getSides()) do
  6.   if peripheral.getType(s) == "monitor" then
  7.     side = s
  8.   end
  9. end
  10. if not side then
  11.   print("You don't have a monitor attached")
  12.   print("  Please add one.")
  13.   error()
  14. end
  15.  
  16. mon = peripheral.wrap(side)
  17.  
  18. local x
  19. local y
  20. x, y = mon.getSize()
  21. local page = 1
  22. m = peripheral.wrap("right")
  23.  
  24. local things = {""}
  25.  
  26. function saveAll()
  27.   local file = fs.open("tasks", "w")
  28.   file.write(textutils.serialize(things))
  29.   file.close()
  30. end
  31.  
  32. function loadAll()
  33.   local file = fs.open("tasks", "r")
  34.   local data = file.readAll()
  35.   file.close()
  36.   data = textutils.unserialize(data)
  37.   for i = 1, #data do
  38.     things[i] = data[i]
  39.   end
  40. end
  41.  
  42. function mainLoop()
  43.   loadAll()
  44.   mon.clear()
  45.   printTop()
  46.   for i = 1, y-2 do
  47.     printLine(i)
  48.   end
  49.   printButtons()
  50.   term.clear()
  51.   term.setCursorPos(1, 1)
  52.   term.setTextColor(colors.white)
  53.   print("Do you want to delete or add a task?")
  54.   term.setTextColor(colors.lime)
  55.   term.write("  Add")
  56.   term.setTextColor(colors.red)
  57.   print("       Delete")
  58.   term.setTextColor(colors.white)
  59.  
  60.   event, a, b, c = os.pullEvent()
  61.   if event == "mouse_click" then
  62.     if c == 2 then
  63.       if (b == 3) or (b == 4) or (b == 5) then
  64.         print("")
  65.         print("What task do you want to add?")
  66.         task = read()
  67.         things[#things+1] = task
  68.         saveAll()
  69.         sleep(0.2)
  70.         print("Task added!")
  71.         sleep(0.5)
  72.       elseif (c == 12) or (b >= 13) or (b <= 18) then
  73.         print("")
  74.         print("Which task do you want to delete? (Number)")
  75.         task = read()
  76.         print("Are you sure? (y/n)")
  77.         answ = read()
  78.         if (answ == "y") or (answ == "Y") then
  79.           print("Deleting task " ..task)
  80.           for i = task, #things - 1 do
  81.             things[i] = things[i+1]
  82.           end
  83.           things[#things] = nil
  84.           saveAll()
  85.           sleep(0.2)
  86.           print("Task deleted!")
  87.           sleep(0.5)
  88.         end
  89.       end
  90.     end
  91.   end
  92.    
  93.   if event == "monitor_touch" then
  94.     if c == y then
  95.       if (b == 2) or (b == 3) or (b == 4) or (b ==5) then
  96.         if page > 1 then
  97.           page = page-1
  98.         end
  99.       elseif (b == x-4) or (b == x-3) or (b == x-2) or (b == x-1) then
  100.         page = page+1
  101.       end
  102.     end
  103.   end
  104.   mainLoop()  
  105. end
  106.  
  107.  
  108. function printButtons()
  109.   mon.setCursorPos(2, y)
  110.   if page == 1 then
  111.     mon.setTextColor(colors.lightGray)
  112.   else
  113.     mon.setTextColor(colors.black)
  114.   end
  115.   mon.setBackgroundColor(colors.white)
  116.   mon.write("Back")
  117.  
  118.   mon.setTextColor(colors.black)
  119.   mon.setCursorPos(x/2, y)
  120.   mon.write("" ..page)
  121.   mon.setCursorPos(x-4, y)
  122.   mon.write("Next")
  123. end
  124.  
  125. function printTop()
  126.   mon.setBackgroundColor(colors.white)
  127.   mon.setTextColor(colors.black)
  128.   mon.setCursorPos(x/2-5, 1)
  129.   mon.write("To-Do List")
  130. end
  131.  
  132. function printLine(num)
  133.   if num % 2 == 0 then
  134.     mon.setBackgroundColor(colors.gray)
  135.   else
  136.     mon.setBackgroundColor(colors.lightGray)
  137.   end
  138.   mon.setCursorPos(1, num+1)
  139.   if page > 1 then
  140.     num = num+(x*(page-1))
  141.   end
  142.   mon.write("" ..num)
  143.   mon.write(": ")
  144.  
  145.   if things[num] ~= nil then
  146.     mon.write("" ..things[num])
  147.     mon.write("                                                                                           ")
  148.   else
  149.     mon.write("                                                                                           ")
  150.   end
  151. end
  152.  
  153. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement