Advertisement
Guest User

todo

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