Guest User

todo

a guest
Mar 6th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  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.  term.setBackgroundColour(color2)
  55.  term.setTextColour(color1)
  56.  term.setCursorPos(2,maxY)
  57.  term.clearLine()
  58.  write("<")
  59.  term.setCursorPos((maxX/2-#tostring(page)/2)+1,maxY)
  60.  write(page)
  61.  term.setCursorPos(maxX-1,maxY)
  62.  write(">")
  63. end
  64.  
  65. local function saveTasks(tbl)
  66.  local A = fs.open(taskFile, "w")
  67.  A.write(textutils.serialize(tbl))
  68.  A.close()
  69. end
  70.  
  71. local function addTask()
  72.  local tasks = getTasks()
  73.  term.setBackgroundColour(colors.black)
  74.  term.setTextColour(colors.white)
  75.  term.clear()
  76.  term.setCursorPos(1,1)
  77.  write("Task: ")
  78.  local tsk = read()
  79.  table.insert(tasks, tsk)
  80.  saveTasks(tasks)
  81. end
  82.  
  83. local function delTask()
  84.  local tasks = getTasks()
  85.  term.setBackgroundColour(colors.black)
  86.  term.setTextColour(colors.white)
  87.  term.clear()
  88.  term.setCursorPos(1,1)
  89.  write("Task number: ")
  90.  local tsknmbr = tonumber(read())
  91.  table.remove(tasks, tsknmbr)
  92.  saveTasks(tasks)
  93. end
  94.  
  95. local function showTask(pg, pos, h, w)
  96.  local tasks = getTasks()
  97.  local step1 = (h-2)*(pg-1)
  98.  local step2 = (pos-1)+step1
  99.  term.setBackgroundColour(color2)
  100.  term.setTextColour(color1)
  101.  term.clear()
  102.  term.setCursorPos(1,1)
  103.  if tasks[step2] ~= nil then
  104.   write("Task #"..step2..":")
  105.   local text = tasks[step2]
  106.   local k = 1
  107.   for i=1, h-1 do
  108.    term.setCursorPos(1,i+1)
  109.    for j=1,w do
  110.     write(string.sub(text, k, k))
  111.     k = k + 1
  112.    end
  113.   end
  114.  term.setCursorPos(1,h)
  115.  write("Press any key to continue")
  116.  os.pullEvent("key")
  117.  end
  118. end
  119.  
  120. redraw(page)
  121. local maxX, maxY = term.getSize()
  122. while true do
  123.  sleep(0)
  124.  ev, b, xPos, yPos = os.pullEvent("mouse_click")
  125.  if xPos == 2 and yPos == 1 then
  126.   addTask()
  127.  elseif xPos == 6 and yPos == 1 then
  128.   delTask()
  129.  elseif xPos == 2 and yPos == maxY then
  130.   if page > 1 then
  131.    page = page - 1
  132.   end
  133.  elseif xPos == maxX-1 and yPos == maxY then
  134.   page = page + 1
  135.  elseif xPos > 1 and yPos < maxY then
  136.   showTask(page, yPos, maxY, maxX)
  137.  end
  138.  redraw(page)
  139. end
Advertisement
Add Comment
Please, Sign In to add comment