Advertisement
Guest User

Untitled

a guest
Aug 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1. local args = {...}
  2. local tasks = {}
  3. local currentTask = "tasks"
  4. local running = true
  5. local state = "list"
  6. local timer
  7. local lastclicked = 1
  8. local line = 0
  9. local w, h = term.getSize()
  10.  
  11. if #args > 0 then
  12.     currentTask = args[1]
  13. end
  14.  
  15. function loadTasks(fileName)
  16.     if fs.exists(fileName) then
  17.         local file = fs.open(fileName,"r")
  18.         local data = file.readAll()
  19.         file.close()
  20.         return textutils.unserialize(data)
  21.     end
  22.     return {}
  23. end
  24. function saveTasks(fileName, taskTable)
  25.     for i = 2, #taskTable do
  26.         taskTable[i].checked = false
  27.     end
  28.     local file = fs.open(fileName,"w")
  29.     file.write(textutils.serialize(taskTable))
  30.     file.close()
  31. end
  32.  
  33. function printTasks(taskTable, line2)
  34.     local padding = 2
  35.     term.clear()
  36.     if #taskTable > 0 then
  37.     for i=1+line2, 13+line2 do
  38.         term.setCursorPos(padding, padding+i-line2-1)
  39.         if taskTable[i].checked == true then
  40.             term.setBackgroundColor(colors.white)
  41.             term.setTextColor(colors.black)
  42.         end
  43.         if taskTable[i].completed == "x" then
  44.             term.write("[X] "..taskTable[i].name)
  45.         else
  46.             term.write("[_] "..taskTable[i].name)
  47.         end
  48.         term.setBackgroundColor(colors.black)
  49.         term.setTextColor(colors.white)
  50.     end
  51.     else
  52.         term.setCursorPos(padding, padding)
  53.         term.write("No tasks found.")
  54.     end
  55.     term.setCursorPos(math.ceil((w / 2) - (11 / 2)), h - 3)
  56.     term.write("<<<     >>>")
  57.     term.setCursorPos(2, h - 1)
  58.     term.write("[A]dd - [D]elete - [Q]uit")
  59.    
  60. end
  61.  
  62. local currentMessage = {}
  63. function displayAddItem()
  64.     local padding = 6
  65.     term.clear()
  66.     term.setBackgroundColor(colors.black)
  67.     term.setTextColor(colors.white)
  68. term.setCursorPos(padding, padding)
  69.     term.write("Add Task")     
  70. term.setCursorPos(padding, padding+1)
  71.     term.write("-----------------------")
  72.     term.setCursorPos(2, h - 2)
  73.     term.write("Press [ENTER] to insert new item") 
  74. term.setCursorPos(2, h - 1)
  75.     term.write("Enter nothing (blank string) to cancel")
  76.  
  77.     --we have to draw the whole screen first!
  78.     term.setCursorPos(padding, padding+3)
  79.     term.write("Task Name: ")
  80.     local newTask = read()
  81.     if newTask ~= "" then
  82.         table.insert(tasks, {name = newTask, completed = "_", checked = false})
  83.     end
  84. state = "list" --return our state back to the main list
  85. end
  86.  
  87. tasks = loadTasks(currentTask)
  88.  
  89. timer = os.startTimer(0)
  90. while running do
  91.     local event, p1, p2, p3 = os.pullEvent()
  92.     if event == "key" and state == "list" then
  93.         if p1 == keys.q then
  94.             running = false
  95.         elseif p1 == keys.a then
  96.             state = "additem"
  97.         elseif p1 == keys.d then
  98.             table.remove(tasks, lastclicked)
  99.         end
  100.     end
  101.     if event == "mouse_click" and state == "list" then
  102.         if p3-1 <= (line + 13 < #tasks and line + 13 or #tasks) and p3-1 >= 1 then
  103.             if p2 >= 2 and p2 <= 4 then
  104.                 if tasks[p3-1].completed == "x" then
  105.                     tasks[p3-1].completed = "_"
  106.                 else
  107.                     tasks[p3-1].completed = "x"
  108.                 end
  109.             end
  110.             if p2 >= 5 then
  111.                 tasks[lastclicked].checked = false
  112.                 tasks[p3-1+line].checked = true
  113.                 lastclicked = p3-1+line
  114.             end
  115.         end
  116.         if p3 == h-3 then
  117.             if p2 >= 20 and p2 <= 22 then
  118.                 if line >= 13 then
  119.                     line = line - 13
  120.                 elseif line - 13 < 0 then
  121.                     line = 0
  122.                 end
  123.             end
  124.             if p2 >= 28 and p2 <= 30 then
  125.                 if line + 13 < #tasks then
  126.                     line = line + 13
  127.                 else
  128.                     line = #tasks
  129.                 end
  130.             end
  131.         end
  132.     end
  133.     if event == "timer" and p1 == timer then
  134.         if state == "list" then
  135.             printTasks(tasks, line)
  136.         elseif state == "additem" then
  137.             displayAddItem()
  138.         end
  139.         timer = os.startTimer(0.1)
  140.     end
  141. end
  142.  
  143. saveTasks(currentTask, tasks)
  144. term.setCursorPos(1,1)
  145. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement