RandomShovel

[CC] Todo List Advanced Computers Only!

Aug 16th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.27 KB | None | 0 0
  1. --[[  Config  ]]--
  2.  
  3. --[[
  4. It's empty here...
  5. Looks Random didn't make me customizable!
  6. I need features.. Post ideas on the CC Forum!
  7. ]]--
  8.  
  9.  
  10. --[[  DO NOT EDIT WHAT'S BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!  ]]--
  11.  
  12. --[[  Tables  ]]--
  13.  
  14. local tArgs = { ... }
  15. local currTask = {}
  16. local taskNums = {}
  17. local mOptions = {
  18.  {5, "Help"},
  19.  {6, "Add"},
  20.  {7, "Remove"},
  21.  {8, "Delete"},
  22.  {9, "Check"},
  23.  {10, "List"},
  24.  {11, "Quit"}
  25. }
  26.  
  27.  
  28. --[[  Random Messages  ]]--
  29.  
  30. local footerMessages = {
  31.  "Bug reports are useful!",
  32.  "Menus anyone?"
  33. }
  34.  
  35.  
  36. --[[  Variables  ]]--
  37.  
  38. local pVersion = "1.7.10-2"
  39. local pHeader = "Task List v"..pVersion.." BETA"
  40. local pFooter = footerMessages[math.random(1, #footerMessages)]
  41.  
  42.  
  43. --[[  Functions  ]]--
  44.  
  45. local function cs()
  46.  term.clear()
  47.  term.setCursorPos(1, 1)
  48. end
  49.  
  50. local function centerT(text, textType)
  51.  local mx, my = term.getSize()
  52.  local cx, cy = term.getCursorPos()
  53.  term.setCursorPos(math.floor((mx-string.len(text))/2), cy)
  54.  if string.lower(textType) == "print" then
  55.   print(text)
  56.  elseif string.lower(textType) == "write" then
  57.   write(text)
  58.  elseif string.lower(textType) == "swrite" then
  59.   textutils.slowWrite(text)
  60.  elseif string.lower(textType) == "sprint" then
  61.   textutils.slowPrint(text)
  62.  elseif string.lower(textType) == "error" then
  63.   printError(text)
  64.  end
  65. end
  66.  
  67. local function dScreen()
  68.  cs()
  69.  centerT(pHeader, "print")
  70.  centerT("----------------", "print")
  71.  local mx, my = term.getSize()
  72.  term.setCursorPos(1, my-1)
  73.  centerT("----------------", "print")
  74.  centerT(pFooter, "write")
  75.  term.setCursorPos(1, 5)
  76. end
  77.  
  78. local function fTasks()
  79.  if fs.exists("tasks") then
  80.   local tasks = fs.open("tasks", "r")
  81.   local data = tasks.readAll()
  82.   tasks.close()
  83.   local nData = textutils.unserialize(data)
  84.   if nData == "nil" then
  85.    dScreen()
  86.    centerT("No tasks!", "error")
  87.    sleep(0.5)
  88.   end
  89.   for a=1, #nData do
  90.    currTask[a] = nData[a]
  91.   end
  92.  else
  93.   print("DOESN'T EXIST!")
  94.   sleep(0.5)
  95.   dScreen()
  96.   centerT("You don't have a task file!", "error")
  97.   print("Y | N")
  98.   write("> ")
  99.   local tAnswer = read()
  100.   if string.lower(tAnswer) == "y" then
  101.    local tasks = fs.open("tasks", "w")
  102.    tasks.close()
  103.   else
  104.    cs()
  105.   end
  106.  end
  107. end
  108.  
  109. local function tSave(task)
  110.  dScreen()
  111.  centerT("Saving...", "sprint")
  112.  sleep(0.5)
  113.  fTasks()
  114.  for a=1, #currTask do
  115.   if string.lower(currTask[a]) == string.lower(task) then
  116.    dScreen()
  117.    centerT("Task already exists!", "error")
  118.    sleep(0.5)
  119.    tExists = true
  120.    break
  121.   elseif a == #currTask then
  122.    tExists = false
  123.   end
  124.  end
  125.  if tExists == false then
  126.   currTask[#currTask+1] = task
  127.   local tasks = fs.open("tasks", "w")
  128.   tasks.write(textutils.serialize(currTask))
  129.   tasks.close()
  130.   dScreen()
  131.   centerT('"'..task..'" has been added!', "print")
  132.   sleep(1.5)
  133.  end
  134. end
  135.  
  136. local function rTasks(tNumber)
  137.  fTasks()
  138.  table.remove(currTask, tNumber)
  139.  local tasks = fs.open("tasks", "w")
  140.  tasks.write(textutils.serialize(currTask))
  141.  tasks.close()
  142.  cs()
  143. end
  144.  
  145. local function dOptions()
  146.  dScreen()
  147.  for a=1, #mOptions do
  148.   print("* "..mOptions[a][2])
  149.  end
  150. end
  151.  
  152. local function checkClick(cY)
  153.  for a=1, #mOptions do
  154.   if cY == mOptions[a][1] then
  155.    return mOptions[a][2]
  156.   end
  157.  end
  158. end
  159.  
  160. local function tEnd(text1, text2)
  161.  local cx, cy = term.getCursorPos()
  162.  local mx, my = term.getSize()
  163.  write(text1)
  164.  term.setCursorPos(mx-string.len(text2), cy)
  165.  write(text2)
  166. end
  167.  
  168. local function hTHandler(text1, text2, textType2)
  169.  local cx, cy = term.getCursorPos()
  170.  local mx, my = term.getSize()
  171.  write(text1)
  172.  
  173.  term.setCursorPos(15, cy)
  174.  if string.lower(textType2) == "print" then
  175.   print(text2)
  176.  elseif string.lower(textType2) == "write" then
  177.   write(text2)
  178.  elseif string.lower(textType2) == "sprint" then
  179.   textutils.slowPrint(text2)
  180.  elseif string.lower(textType2) == "swrite" then
  181.   textutils.slowWrite(text2)
  182.  elseif string.lower(textType2) == "error" then
  183.   printError(text2)
  184.  end
  185. end
  186.  
  187.  
  188. --[[  Main  ]]--
  189.  
  190. while true do
  191.  dScreen()
  192.  dOptions()
  193.  event, param1, eventX, eventY = os.pullEvent("mouse_click")
  194.  local uOption = checkClick(eventY)
  195.  if uOption == "Help" then
  196.   dScreen()
  197.   centerT("Help Section:", "print")
  198.   print("")
  199.   hTHandler("Add:", "Adds a task to the task file", "print")
  200.   hTHandler("Remove:", "Removes a task from the task file", "print")
  201.   hTHandler("Delete:", "Deletes task file", "print")
  202.   hTHandler("Check:", "Checks for task file", "print")
  203.   hTHandler("List:", "Lists all tasks", "print")
  204.   hTHandler("Quit:", "Quits the program", "print")
  205.   hTHandler("Pages:", "Click anywhere to exit, or flip pages", "print")
  206.   print("")
  207.   centerT("Click anywhere to return to menu", "error")
  208.   sleep(0.5)
  209.   os.pullEvent("mouse_click")
  210.  elseif uOption == "Add" then
  211.   dScreen()
  212.   centerT("Add A Task", "print")
  213.   centerT("Type your task here:", "error")
  214.   write("> ")
  215.   local uTask = read()
  216.   tSave(uTask)
  217.   if not tExists then
  218.    dScreen()
  219.    centerT("Saved!", "sprint")
  220.   end
  221.   sleep(0.5)
  222.  elseif uOption == "Remove" then
  223.   dScreen()
  224.   fTasks()
  225.   if #currTask > 0 then
  226.    centerT("Remove A Task:", "print")
  227.    centerT("Click your task:", "error")
  228.   else
  229.    centerT("No tasks to remove!", "error")
  230.   end
  231.   for a=1, #currTask do
  232.    local cx, cy = term.getCursorPos()
  233.    print("* "..currTask[a])
  234.    table.insert(taskNums, a+6)
  235.   end
  236.   if #currTask > 0 then
  237.    event, param1, x, y = os.pullEvent("mouse_click")
  238.   end
  239.   for a=1, #taskNums do
  240.    if y == tonumber(taskNums[a]) then
  241.     table.remove(currTask, a)
  242.     break
  243.    end
  244.   end
  245.   local tasks = fs.open("tasks", "w")
  246.   tasks.write(textutils.serialize(currTask))
  247.   tasks.close()
  248.  elseif uOption == "Delete" then
  249.   dScreen()
  250.   centerT("Deleting file...", "sprint")
  251.   fs.delete("tasks")
  252.   sleep(0.5)
  253.   dScreen()
  254.   centerT("Deleted!", "error")
  255.   sleep(0.5)
  256.   dScreen()
  257.   centerT("Remaking file...", "sprint")
  258.   sleep(0.5)
  259.   local tasks = fs.open("tasks", "w")
  260.   tasks.write(textutils.serialize(currTask))
  261.   tasks.close()
  262.   dScreen()
  263.   centerT("File remade!", "error")
  264.   sleep(0.5)
  265.   os.pullEvent("mouse_click")
  266.  elseif uOption == "Check" then
  267.   dScreen()
  268.   centerT("Checking file...", "sprint")
  269.   if fs.exists("tasks") then
  270.    dScreen()
  271.    centerT("File found!", "print")
  272.   else
  273.    dScreen()
  274.    centerT("File not found!", "error")
  275.   end
  276.   dScreen()
  277.   fTasks()
  278.   for a=1, #currTask do
  279.    for b=a+1, #currTask do
  280.     if string.lower(currTask[a]) == string.lower(currTask[b]) then
  281.      centerT("Duplicate tasks found!", "error")
  282.      sleep(0.5)
  283.      dScreen()
  284.      centerT("Deleting...", "write")
  285.      sleep(0.5)
  286.      table.remove(currTask, a)
  287.      local tasks = fs.open("tasks", "w")
  288.      tasks.write(textutils.serialize(currTask))
  289.      tasks.close()
  290.      dScreen()
  291.      centerT("Deleted!", "print")
  292.     end
  293.    end
  294.   end
  295.   sleep(0.5)
  296.   os.pullEvent("mouse_click")
  297.  elseif uOption == "List" then
  298.   dScreen()
  299.   fTasks()
  300.   if #currTask <= 0 then
  301.    centerT("No tasks to display!", "error")
  302.   elseif #currTask > 10 then
  303.    for a=1, 9 do
  304.     print(a..") "..currTask[a])
  305.    end
  306.    tEnd("<- Previous", "Next ->")
  307.   else
  308.    for a=1, #currTask do
  309.     print(a..") "..currTask[a])
  310.    end
  311.   end
  312.   os.pullEvent("mouse_click")
  313.  elseif uOption == "Quit" then
  314.   dScreen()
  315.   centerT("Quitting...", "sprint")
  316.   sleep(0.5)
  317.   cs()
  318.   break
  319.  end
  320. end
Advertisement
Add Comment
Please, Sign In to add comment