biggles2206

TodoList Gui

Mar 14th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 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.  
  8. if #args > 0 then
  9.     currentTask = args[1]
  10. end
  11.  
  12. function loadTasks(fileName)
  13.   if fs.exists(fileName) then
  14.     local file = fs.open(fileName,"r")
  15.     local i = 1
  16.     local tmpTasks = {}
  17.     while true do
  18.         tmpTasks[i] = file.readLine()
  19.         if tmpTasks[i] == nil then
  20.             break
  21.         end
  22.         i = i + 1
  23.     end
  24.     file.close()
  25.     return tmpTasks
  26.   end
  27.   return {}
  28. end
  29. function saveTasks(fileName, taskTable)
  30.     local file = fs.open(fileName,"w")
  31.     for i = 1, #taskTable, 1 do
  32.          file.writeLine(taskTable[i])
  33.     end
  34.     file.close()
  35. end
  36.  
  37. local selectedMenuItem= 1
  38. function printTasks(taskTable)
  39.     local padding = 2
  40.     local w, h = term.getSize()
  41.     term.clear()
  42.     if #taskTable > 0 then
  43.     for i=1, #taskTable do
  44.         if i == selectedMenuItem then
  45.             term.setBackgroundColor(colors.white)
  46.             term.setTextColor(colors.black)
  47.         else
  48.             term.setBackgroundColor(colors.black)
  49.             term.setTextColor(colors.white)
  50. end
  51.         term.setCursorPos(padding, padding+i-1)
  52.         local completed = false
  53.         if i == 2 then
  54.             completed = true
  55.         end
  56.         if completed then
  57.         term.write("[X] "..taskTable[i])
  58. else
  59.         term.write("[_] "..taskTable[i])
  60. end
  61.             term.setBackgroundColor(colors.black)
  62.             term.setTextColor(colors.white)
  63. end
  64. else
  65.         term.setCursorPos(padding, padding)
  66.         term.write("No tasks found.")
  67. end
  68.     term.setCursorPos(2, h - 1)
  69.     term.write("Up/Down/Enter - [A]dd - [D]elete - [Q]uit")
  70. end
  71.  
  72. local currentMessage = {}
  73. function showMessage(messageTable)
  74.     state = "msg"
  75. currentMessage = messageTable
  76. end
  77. function displayMsg(messageTable)
  78.     local padding = 6
  79.     local w, h = term.getSize()
  80.     term.clear()
  81.     term.setBackgroundColor(colors.black)
  82.     term.setTextColor(colors.white)
  83.     for i=1, #messageTable do
  84.         term.setCursorPos(padding, padding+i-1)
  85.         term.write(messageTable[i])
  86. end
  87.     term.setCursorPos(2, h - 1)
  88.     term.write("Press [ENTER] to continue")
  89. end
  90. function displayAddItem()
  91.     local padding = 6
  92.     local w, h = term.getSize()
  93.     term.clear()
  94.     term.setBackgroundColor(colors.black)
  95.     term.setTextColor(colors.white)
  96. term.setCursorPos(padding, padding)
  97.     term.write("Add Task")     
  98. term.setCursorPos(padding, padding+1)
  99.     term.write("-----------------------")
  100.     term.setCursorPos(2, h - 2)
  101.     term.write("Press [ENTER] to insert new item") 
  102. term.setCursorPos(2, h - 1)
  103.     term.write("Enter nothing (blank string) to cancel")
  104.  
  105.     --we have to draw the whole screen first!
  106.     term.setCursorPos(padding, padding+3)
  107.     term.write("Task Name: ")
  108.     local newTask = read()
  109.     if newTask ~= "" then
  110.     tasks[#tasks+1] = newTask
  111. end
  112. state = "list" --return our state back to the main list
  113. end
  114.  
  115. tasks = loadTasks(currentTask)
  116.  
  117. timer = os.startTimer(0)
  118. while running do
  119. local event, p1 = os.pullEvent()
  120. if event == "key" then
  121. if state == "list" then
  122.     if p1 == keys.q then
  123.         running = false
  124.     elseif p1 == keys.up then
  125.         selectedMenuItem=selectedMenuItem-1
  126.         if selectedMenuItem <= 0 then
  127.             selectedMenuItem = #tasks
  128.         end
  129.     elseif p1 == keys.down then
  130.         selectedMenuItem=selectedMenuItem+1
  131.         if selectedMenuItem > #tasks then
  132.             selectedMenuItem = 1
  133.         end
  134.     elseif p1 == keys.a then
  135.         state = "additem"
  136.     elseif p1 == keys.d then
  137.         table.remove(tasks, selectedMenuItem)
  138.     elseif p1 == keys.enter then
  139.         showMessage({
  140. "You selected number: #"..selectedMenuItem,
  141. "",
  142. "Value: "..tasks[selectedMenuItem]
  143. })
  144. end
  145.         elseif state == "msg" then
  146. if p1 == keys.enter then
  147.         state = "list"
  148.     end
  149. end
  150. end
  151. if event == "timer" and p1 == timer then
  152.     if state == "list" then
  153.         printTasks(tasks)
  154.     elseif state == "additem" then
  155.         displayAddItem()
  156.     elseif state == "msg" then
  157.         displayMsg(currentMessage)
  158. end
  159. timer = os.startTimer(0.1)
  160. end
  161. end
  162.  
  163. saveTasks(currentTask, tasks)
  164.  
  165. term.clear()
  166. term.setCursorPos(1,1)
  167. term.write("Thanks for using my task program")
  168. term.setCursorPos(1,3)
  169. sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment