Guest User

todo

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