Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local command = tArgs[1]
- function help()
- term.clear()
- term.setCursorPos(1,1)
- print("ToDoList 1.0 by zachdyer")
- print(" ")
- print("USAGE:")
- print("Type 'todolist help' for usage instructions.")
- print("Type 'todolist add <entry>' to add a task")
- print("Type 'todolist' to display your list.")
- print("Type 'todolist del <number>' to delete a task.")
- end
- function getTasks()
- local task = {}
- local file = fs.open("tasks","r")
- local i = 1
- while true do
- task[i] = file.readLine()
- if task[i] == nil then
- break
- else
- i = i + 1
- end
- end
- file.close()
- return task
- end
- function saveTasks(taskTable)
- local list = ""
- local file = fs.open("tasks","w")
- for i = 1, #taskTable, 1 do
- list = list .. taskTable[i] .. "\n"
- end
- file.write(list)
- file.close()
- end
- function printTasks()
- local task = getTasks()
- if task[1] == nil then
- help()
- else
- term.clear()
- term.setCursorPos(1,1)
- local w, h = term.getSize()
- print("Printing your tasks...")
- for i = 1, #task, 1 do
- local x, y = term.getCursorPos()
- if y == h - 2 then
- print(i .. ". " .. task[i])
- print("Press any key to continue...")
- os.pullEvent("char")
- term.clear()
- term.setCursorPos(1,1)
- else
- print(i .. ". " .. task[i])
- end
- end
- end
- end
- if command == "add" then
- local entry = ""
- for i = 2, #tArgs, 1 do
- entry = entry .. tArgs[i] .. " "
- end
- print("Adding " .. entry)
- local task = getTasks()
- task[#task + 1] = entry
- saveTasks(task)
- elseif command == "help" then
- help()
- elseif command == nil then
- if fs.exists("tasks") then
- printTasks()
- else
- help()
- end
- elseif command == "del" then
- local number = tonumber(tArgs[2])
- local task = getTasks()
- if number > #task then
- print("Task number " .. number .. " does not exist.")
- else
- print("Deleting task number " .. number .. ". " .. task[number])
- table.remove(task,number)
- saveTasks(task)
- end
- end
Add Comment
Please, Sign In to add comment