masterdisasterHD

Todo list PockedEdition

Mar 6th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local color1 = colors.lime
  2. local color2 = colors.white
  3.  
  4. local taskFile = "/.tasks"
  5. local page = 1
  6.  
  7. local function getTasks()
  8.  if not fs.exists(taskFile) then
  9.   local A = fs.open(taskFile, "w")
  10.   A.write("{}")
  11.   A.close()
  12.   return {}
  13.  else
  14.   local A = fs.open(taskFile, "r")
  15.   local tmp = textutils.unserialize(A.readAll())
  16.   A.close()
  17.   return tmp
  18.  end
  19. end
  20.  
  21. local function redraw(page)
  22.  local tasks = getTasks()
  23.  local maxX, maxY = term.getSize()
  24.  local nMaxY = maxY - 2
  25.  
  26.  term.clear()
  27.  term.setCursorPos(1,1)
  28.  term.setBackgroundColour(color1)
  29.  term.setTextColour(color2)
  30.  term.clearLine()
  31.  print(" + / -  |  HD's Todo list  ")
  32.  
  33.  for i=1, nMaxY do
  34.    if i%2 == 1 then
  35.     term.setBackgroundColour(color2)
  36.     term.setTextColour(color1)
  37.    else
  38.     term.setBackgroundColour(color1)
  39.     term.setTextColour(color2)
  40.    end
  41.  
  42.   term.setCursorPos(1,1+i)
  43.   term.clearLine()
  44.  
  45.   local num = i+(nMaxY*(page-1))
  46.  
  47.   if tasks[num] ~= nil then
  48.    write(num..": "..tasks[num])
  49.   else
  50.    write(num..": ")
  51.   end
  52.  end
  53.  
  54.  if maxY%2 == 0 then
  55.   term.setBackgroundColour(color2)
  56.   term.setTextColour(color1)
  57.  else
  58.   term.setBackgroundColour(color1)
  59.   term.setTextColour(color2)
  60.  end
  61. term.setCursorPos(2,maxY)
  62.  term.clearLine()
  63.  write("<")
  64.  term.setCursorPos((maxX/2-#tostring(page)/2)+1,maxY)
  65.  write(page)
  66.  term.setCursorPos(maxX-1,maxY)
  67.  write(">")
  68. end
  69.  
  70. local function saveTasks(tbl)
  71.  local A = fs.open(taskFile, "w")
  72.  A.write(textutils.serialize(tbl))
  73.  A.close()
  74. end
  75.  
  76. local function addTask()
  77.  local tasks = getTasks()
  78.  term.setBackgroundColour(colors.black)
  79.  term.setTextColour(colors.white)
  80.  term.clear()
  81.  term.setCursorPos(1,1)
  82.  write("Task: ")
  83.  local tsk = read()
  84.  table.insert(tasks, tsk)
  85.  saveTasks(tasks)
  86. end
  87.  
  88. local function delTask()
  89.  local tasks = getTasks()
  90.  term.setBackgroundColour(colors.black)
  91.  term.setTextColour(colors.white)
  92.  term.clear()
  93.  term.setCursorPos(1,1)
  94.  write("Task number: ")
  95.  local tsknmbr = tonumber(read())
  96.  table.remove(tasks, tsknmbr)
  97.  saveTasks(tasks)
  98. end
  99.  
  100. local function showTask(pg, pos, h, w)
  101.  local tasks = getTasks()
  102.  local step1 = (h-2)*(pg-1)
  103.  local step2 = (pos-1)+step1
  104.  term.setBackgroundColour(color2)
  105.  term.setTextColour(color1)
  106.  term.clear()
  107.  term.setCursorPos(1,1)
  108.  if tasks[step2] ~= nil then
  109.   write("Task #"..step2..":")
  110.   local text = tasks[step2]
  111.   local k = 1
  112.   for i=1, h-1 do
  113.    term.setCursorPos(1,i+1)
  114.    for j=1,w do
  115.     write(string.sub(text, k, k))
  116.     k = k + 1
  117.    end
  118.   end
  119.  term.setCursorPos(1,h)
  120.  write("Press any key to continue")
  121.  os.pullEvent("key")
  122.  end
  123. end
  124.  
  125. redraw(page)
  126. local maxX, maxY = term.getSize()
  127. while true do
  128.  sleep(0)
  129.  ev, b, xPos, yPos = os.pullEvent()
  130.  if ev == "mouse_click" then
  131.   if xPos == 2 and yPos == 1 then
  132.    addTask()
  133.   elseif xPos == 6 and yPos == 1 then
  134.    delTask()
  135.   elseif xPos == 2 and yPos == maxY then
  136.    if page > 1 then
  137.     page = page - 1
  138.    end
  139.   elseif xPos == maxX-1 and yPos == maxY then
  140.    page = page + 1
  141.   elseif xPos > 1 and yPos < maxY then
  142.    showTask(page, yPos, maxY, maxX)
  143.   end
  144.  elseif ev == "mouse_scroll" then
  145.   if b == -1 then
  146.    if page > 1 then page = page - 1 end
  147.   elseif b == 1 then
  148.    page = page + 1
  149.   end
  150.  end
  151.  redraw(page)
  152. end
Add Comment
Please, Sign In to add comment