mariebu

todolist

Oct 3rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. local tArgs = {...}
  2. local command = tArgs[1]
  3.  
  4. function help()
  5.     term.clear()
  6.     term.setCursorPos(1,1)
  7.     print("ToDoList 1.0 by zachdyer")
  8.     print(" ")
  9.     print("USAGE:")
  10.     print("Type 'todolist help' for usage instructions.")
  11.     print("Type 'todolist add <entry>' to add a task")
  12.     print("Type 'todolist' to display your list.")
  13.     print("Type 'todolist del <number>' to delete a task.")
  14. end
  15. function getTasks()
  16.     local task = {}
  17.     local file = fs.open("tasks","r")
  18.     local i = 1
  19.     while true do
  20.         task[i] = file.readLine()
  21.         if task[i] == nil then
  22.             break
  23.         else
  24.             i = i + 1
  25.         end
  26.     end
  27.     file.close()
  28.     return task
  29. end
  30. function saveTasks(taskTable)
  31.     local list = ""
  32.     local file = fs.open("tasks","w")
  33.     for i = 1, #taskTable, 1 do
  34.          list = list .. taskTable[i] .. "\n"
  35.     end
  36.     file.write(list)
  37.     file.close()
  38. end
  39. function printTasks()
  40.     local task = getTasks()
  41.     if task[1] == nil then
  42.         help()
  43.     else
  44.         term.clear()
  45.         term.setCursorPos(1,1)
  46.         local w, h = term.getSize()
  47.         print("Printing your tasks...")
  48.         for i = 1, #task, 1 do
  49.             local x, y = term.getCursorPos()
  50.             if y == h - 2 then
  51.                 print(i .. ". " .. task[i])
  52.                 print("Press any key to continue...")
  53.                 os.pullEvent("char")
  54.                 term.clear()
  55.                 term.setCursorPos(1,1)
  56.             else
  57.                 print(i .. ". " .. task[i])
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. if command == "add" then
  64.     local entry = ""
  65.     for i = 2, #tArgs, 1 do
  66.        entry = entry .. tArgs[i] .. " "
  67.     end
  68.     print("Adding " .. entry)
  69.     local task = getTasks()
  70.     task[#task + 1] = entry
  71.     saveTasks(task)
  72. elseif command == "help" then
  73.     help()
  74. elseif command == nil then
  75.     if fs.exists("tasks") then
  76.         printTasks()
  77.     else
  78.         help()
  79.     end
  80. elseif command == "del" then
  81.     local number = tonumber(tArgs[2])
  82.     local task = getTasks()
  83.     if number > #task then
  84.        print("Task number " .. number .. " does not exist.")
  85.     else
  86.         print("Deleting task number " .. number .. ". " .. task[number])
  87.         table.remove(task,number)
  88.         saveTasks(task)
  89.     end
  90. end
Add Comment
Please, Sign In to add comment