masterdisasterHD

Todo list program

Sep 30th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local barTextColour = colors.lime
  2. local barColour = colors.white
  3.  
  4. local primaryColour = colors.white
  5. local secondaryColour = colors.lime
  6.  
  7. local monitors = {
  8.  
  9. }
  10.  
  11. local printers = {
  12.  
  13. }
  14.  
  15. for i,v in ipairs(monitors) do
  16.  if not peripheral.isPresent(v) or peripheral.getType(v) ~= "monitor" then
  17.   table.remove(monitors, i)
  18.  end
  19. end
  20.  
  21. for i,v in ipairs(printers) do
  22.  if not peripheral.isPresent(v) or peripheral.getTye(v) ~= "printer" then
  23.   table.remove(printers, i)
  24.  end
  25. end
  26.  
  27. local taskFile = ".tasks"
  28.  
  29. if not fs.exists(taskFile) then
  30.  A = fs.open(taskFile, "w")
  31.  A.write(textutils.serialize({ "Made by HD -click me!" }))
  32.  A.close()
  33. end
  34.  
  35. local A = 0
  36.  
  37. function remoteTasks()
  38.  for i,v in ipairs(rs.getSides()) do
  39.   if peripheral.isPresent(v) and peripheral.getType(v) == "modem" then
  40.    rednet.open(v)
  41.   end
  42.  end
  43.  
  44.  while true do
  45.   os.queueEvent("")
  46.   os.pullEvent()
  47.   local id, cmd = rednet.receive()
  48.   local cmd = textutils.unserialize(cmd)
  49.   if type(cmd) == "table" then
  50.    if cmd["command"] == "add" then
  51.     local A = getTasks()
  52.     table.insert(A, cmd["input"])
  53.     saveTasks(A)
  54.    elseif cmd["command"] == "del" then
  55.     local A = getTasks()
  56.     if A[tonumber(cmd["input"])] ~= nil then
  57.      table.remove(A, tonumber(cmd["input"]))
  58.      saveTasks(A)
  59.     end
  60.    end
  61.   end
  62.  end
  63. end
  64.  
  65. local function clear()
  66.  if #monitors >= 1 then
  67.   for i,v in ipairs(monitors) do
  68.    peripheral.call(v, "setCursorPos", 1, 1)
  69.    peripheral.call(v, "setBackgroundColour", colors.black)
  70.    peripheral.call(v, "setTextColour", colors.white)
  71.    peripheral.call(v, "clear")
  72.   end
  73.  end
  74.  
  75.  term.setCursorPos(1,1)
  76.  term.setBackgroundColour(colors.black)
  77.  term.setTextColour(colors.white)
  78.  term.clear()
  79. end
  80.  
  81. function getTasks()
  82.  if not fs.exists(taskFile) then
  83.   return {}
  84.  else
  85.   local file = fs.open(taskFile, "r")
  86.   local tmp = textutils.unserialize(file.readAll())
  87.   file.close()
  88.   return tmp
  89.  end
  90. end
  91.  
  92. local function redraw(screen, page)
  93.  
  94.  local text = "HD's Todo list."
  95.  local tasks = getTasks()
  96.  local maxX, maxY = screen.getSize()
  97.  local maxY = maxY - 2
  98.  
  99.  --[[ Text ]]--
  100.  --clear()
  101.  
  102.  screen.setCursorPos(1,1)
  103.  screen.setBackgroundColour(barColour)
  104.  screen.setTextColour(barTextColour)
  105.  screen.clearLine()
  106.  screen.setCursorPos(maxX/2-#text/2, 1)
  107.  screen.write(text)
  108.  --------------
  109.  
  110.  for i=1, maxY do
  111.   if i % 2 == 0 then
  112.    screen.setBackgroundColour(primaryColour)
  113.    screen.setTextColour(secondaryColour)
  114.   elseif i % 2 == 1 then
  115.    screen.setBackgroundColour(secondaryColour)
  116.    screen.setTextColour(primaryColour)
  117.   end
  118.  
  119.   screen.setCursorPos(1, 1+i)
  120.   screen.clearLine()
  121.  
  122.   local num = i+(maxY*(page-1))
  123.  
  124.   if tasks[num] ~= nil then
  125.    screen.write(num..": "..tasks[num])
  126.   else
  127.    screen.write(num..": ")
  128.   end
  129.  end
  130.  
  131.  screen.setBackgroundColour(barColour)
  132.  screen.setTextColour(barTextColour)
  133.  screen.setCursorPos(2, maxY+2)
  134.  screen.clearLine()
  135.  screen.write("Back")
  136.  screen.setCursorPos(maxX/2-#(tostring(page))/2, maxY+2)
  137.  screen.write(tostring(page))
  138.  screen.setCursorPos(maxX-#"Next", maxY+2)
  139.  screen.write("Next")
  140.  
  141.  term.clear()
  142.  term.setCursorPos(1,1)
  143.  term.setCursorPos(2,2)
  144.  write("HD's Todo list v1.0 (its improved :D)")
  145.  term.setCursorPos(2, 6)
  146.  term.setTextColour(colors.lime)
  147.  write("Add Task")
  148.  term.setTextColour(colors.red)
  149.  term.setCursorPos(2, 8)
  150.  write("Remove Task")
  151.  term.setTextColour(colors.white)
  152. end
  153.  
  154. local function showTask(pos, page, max, mon)
  155.  local pos = pos - 1
  156.  local screen = peripheral.wrap(mon)
  157.  local num = pos+(max*(page-1))
  158.  local tasks = getTasks()
  159.  
  160.  if tasks[num] ~= nil then
  161.   clear()
  162.   --error(num)
  163.   --screen.write(tasks[num])
  164.   --local sizeX, sizeY = term.getSize()
  165.   --term.setCursorPos(sizeX/2-#"Pull any event to continue"/2, sizeY)
  166.   --write("Pull any event to continue")
  167.  
  168.   if #monitors >= 1 then
  169.    for i,v in ipairs(monitors) do
  170.      peripheral.call(v, "write", tasks[num])
  171.     local sizeX, sizeY = peripheral.call(v, "getSize")
  172.     peripheral.call(v, "setCursorPos", (sizeX/2-#"Remove Task"/2)+1, sizeY)
  173.     peripheral.call(v, "write", "Remove Task")
  174.    end
  175.   end
  176.  end
  177.  
  178.  local ev, _, xPos, yPos = os.pullEvent("monitor_touch")
  179.  local sizeX, sizeY = peripheral.call(_, "getSize")
  180.  if xPos >= sizeX/2-#"Remove Task"/2 and xPos <= (sizeX/2+#"Remove Task"/2)-1 and yPos == sizeY then
  181.   local A = getTasks()
  182.   table.remove(A, num)
  183.   saveTasks(A)
  184.  else
  185.  
  186.  end
  187. end
  188.  
  189. local function addTask()
  190.  local tasks = getTasks()
  191.  term.clear()
  192.  term.setCursorPos(2,2)
  193.  write("Task: ")
  194.  tsk = read()
  195.  table.insert(tasks, tsk)
  196.  saveTasks(tasks)
  197. end
  198.  
  199. local function delTask()
  200.  local tasks = getTasks()
  201.  term.clear()
  202.  term.setCursorPos(2,2)
  203.  write("Task number: ")
  204.  nmbr = tonumber(read())
  205.  if tasks[nmbr] ~= nil then
  206.   table.remove(tasks, nmbr)
  207.  end
  208.  
  209.  saveTasks(tasks)
  210. end
  211.  
  212. function saveTasks(tbl)
  213.  local A = fs.open(taskFile, "w")
  214.  A.write(textutils.serialize(tbl))
  215.  A.close()
  216. end
  217.  
  218. local pageNmr = 1
  219.  
  220. function iCanRun()
  221.  
  222. while true do
  223.  if monitors[1] ~= nil then
  224.   clear()
  225.   for _,v in ipairs(monitors) do
  226.    local A = peripheral.wrap(v)
  227.    redraw(A, pageNmr)
  228.    os.queueEvent("")
  229.    os.pullEvent()
  230.   end
  231.  end
  232.  --redraw(term, pageNmr)
  233.  
  234.  local maxX, maxY = term.getSize()
  235.  local ev, _, xPos, yPos = os.pullEvent()
  236.  local mon = false
  237.  
  238.  if ev == "monitor_touch" then
  239.   local mon = true
  240.   maxX, maxY = (peripheral.wrap(_)).getSize()
  241.  end
  242.  
  243.  if ev == "monitor_touch" then
  244.   if xPos >= 2 and xPos <= 5 and yPos == maxY then
  245.    if pageNmr > 1 then
  246.     pageNmr = pageNmr - 1
  247.    end
  248.   elseif xPos >= maxX-4 and xPos <= maxX-1 and yPos == maxY then
  249.    pageNmr = pageNmr + 1
  250.   elseif yPos > 1 and yPos < maxY then
  251.    showTask(yPos, pageNmr, maxX, _)
  252.   end
  253.  elseif ev == "mouse_click" then
  254.   if yPos == 6 then
  255.    addTask()
  256.   elseif yPos == 8 then
  257.    delTask()
  258.   end
  259.  end
  260.  sleep(.001)
  261. end
  262.  
  263. end
  264.  
  265. parallel.waitForAny(iCanRun, remoteTasks)
Add Comment
Please, Sign In to add comment