Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local tasks = {}
- local currentTask = "tasks"
- local running = true
- local state = "list"
- local timer
- local lastclicked = 1
- local line = 0
- local w, h = term.getSize()
- if #args > 0 then
- currentTask = args[1]
- end
- function loadTasks(fileName)
- if fs.exists(fileName) then
- local file = fs.open(fileName,"r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- return {}
- end
- function saveTasks(fileName, taskTable)
- for i = 2, #taskTable do
- taskTable[i].checked = false
- end
- local file = fs.open(fileName,"w")
- file.write(textutils.serialize(taskTable))
- file.close()
- end
- function printTasks(taskTable, line2)
- local padding = 2
- term.clear()
- if #taskTable > 0 then
- for i=1+line2, (line2 + (line2+13 > #tasks and #tasks-line2 or 0) == #tasks and line2+1 or 13+line2) do
- term.setCursorPos(padding, padding+i-line2-1)
- if taskTable[i].checked == true then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- end
- if taskTable[i].completed == "x" then
- term.write("[X] "..taskTable[i].name)
- else
- term.write("[_] "..taskTable[i].name)
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- else
- term.setCursorPos(padding, padding)
- term.write("No tasks found.")
- end
- term.setCursorPos(math.ceil((w / 2) - (11 / 2)), h - 3)
- term.write("<<< >>>")
- term.setCursorPos(2, h - 1)
- term.write("[A]dd - [D]elete - [Q]uit")
- --
- end
- local currentMessage = {}
- function displayAddItem()
- local padding = 6
- term.clear()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(padding, padding)
- term.write("Add Task")
- term.setCursorPos(padding, padding+1)
- term.write("-----------------------")
- term.setCursorPos(2, h - 2)
- term.write("Press [ENTER] to insert new item")
- term.setCursorPos(2, h - 1)
- term.write("Enter nothing (blank string) to cancel")
- --we have to draw the whole screen first!
- term.setCursorPos(padding, padding+3)
- term.write("Task Name: ")
- local newTask = read()
- if newTask ~= "" then
- table.insert(tasks, {name = newTask, completed = "_", checked = false})
- end
- state = "list" --return our state back to the main list
- end
- tasks = loadTasks(currentTask)
- timer = os.startTimer(0)
- while running do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "key" and state == "list" then
- if p1 == keys.q then
- running = false
- elseif p1 == keys.a then
- state = "additem"
- elseif p1 == keys.d then
- table.remove(tasks, lastclicked)
- end
- end
- if event == "mouse_click" and state == "list" then
- if p3-1 <= 13 and p3-1 >= 1 then
- if p2 >= 2 and p2 <= 4 then
- if tasks[p3-1+line].completed == "x" then
- tasks[p3-1+line].completed = "_"
- else
- tasks[p3-1+line].completed = "x"
- end
- end
- if p2 >= 5 then
- tasks[lastclicked].checked = false
- tasks[p3-1+line].checked = true
- lastclicked = p3-1+line
- end
- end
- if p3 == h-3 then
- if p2 >= 20 and p2 <= 22 then
- if line >= 13 then
- line = line - 13
- elseif line - 13 < 0 then
- line = 0
- end
- end
- if p2 >= 28 and p2 <= 30 then
- if line < #tasks - (#tasks/13 - math.floor(#tasks/13)) * 13 then
- line = line + 13
- end
- end
- end
- end
- if event == "timer" and p1 == timer then
- if state == "list" then
- printTasks(tasks, line)
- elseif state == "additem" then
- displayAddItem()
- end
- timer = os.startTimer(0.1)
- end
- end
- saveTasks(currentTask, tasks)
- term.setCursorPos(1,1)
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement