Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local color1 = colors.lime
- local color2 = colors.white
- local taskFile = "/.tasks"
- local page = 1
- local function getTasks()
- if not fs.exists(taskFile) then
- local A = fs.open(taskFile, "w")
- A.write("{}")
- A.close()
- return {}
- else
- local A = fs.open(taskFile, "r")
- local tmp = textutils.unserialize(A.readAll())
- A.close()
- return tmp
- end
- end
- local function redraw(page)
- local tasks = getTasks()
- local maxX, maxY = term.getSize()
- local nMaxY = maxY - 2
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColour(color1)
- term.setTextColour(color2)
- term.clearLine()
- print(" + / - | HD's Todo list ")
- for i=1, nMaxY do
- if i%2 == 1 then
- term.setBackgroundColour(color2)
- term.setTextColour(color1)
- else
- term.setBackgroundColour(color1)
- term.setTextColour(color2)
- end
- term.setCursorPos(1,1+i)
- term.clearLine()
- local num = i+(nMaxY*(page-1))
- if tasks[num] ~= nil then
- write(num..": "..tasks[num])
- else
- write(num..": ")
- end
- end
- if maxY%2 == 0 then
- term.setBackgroundColour(color2)
- term.setTextColour(color1)
- else
- term.setBackgroundColour(color1)
- term.setTextColour(color2)
- end
- term.setCursorPos(2,maxY)
- term.clearLine()
- write("<")
- term.setCursorPos((maxX/2-#tostring(page)/2)+1,maxY)
- write(page)
- term.setCursorPos(maxX-1,maxY)
- write(">")
- end
- local function saveTasks(tbl)
- local A = fs.open(taskFile, "w")
- A.write(textutils.serialize(tbl))
- A.close()
- end
- local function addTask()
- local tasks = getTasks()
- term.setBackgroundColour(colors.black)
- term.setTextColour(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- write("Task: ")
- local tsk = read()
- table.insert(tasks, tsk)
- saveTasks(tasks)
- end
- local function delTask()
- local tasks = getTasks()
- term.setBackgroundColour(colors.black)
- term.setTextColour(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- write("Task number: ")
- local tsknmbr = tonumber(read())
- table.remove(tasks, tsknmbr)
- saveTasks(tasks)
- end
- local function showTask(pg, pos, h, w)
- local tasks = getTasks()
- local step1 = (h-2)*(pg-1)
- local step2 = (pos-1)+step1
- term.setBackgroundColour(color2)
- term.setTextColour(color1)
- term.clear()
- term.setCursorPos(1,1)
- if tasks[step2] ~= nil then
- write("Task #"..step2..":")
- local text = tasks[step2]
- local k = 1
- for i=1, h-1 do
- term.setCursorPos(1,i+1)
- for j=1,w do
- write(string.sub(text, k, k))
- k = k + 1
- end
- end
- term.setCursorPos(1,h)
- write("Press any key to continue")
- os.pullEvent("key")
- end
- end
- redraw(page)
- local maxX, maxY = term.getSize()
- while true do
- sleep(0)
- ev, b, xPos, yPos = os.pullEvent()
- if ev == "mouse_click" then
- if xPos == 2 and yPos == 1 then
- addTask()
- elseif xPos == 6 and yPos == 1 then
- delTask()
- elseif xPos == 2 and yPos == maxY then
- if page > 1 then
- page = page - 1
- end
- elseif xPos == maxX-1 and yPos == maxY then
- page = page + 1
- elseif xPos > 1 and yPos < maxY then
- showTask(page, yPos, maxY, maxX)
- end
- elseif ev == "mouse_scroll" then
- if b == -1 then
- if page > 1 then page = page - 1 end
- elseif b == 1 then
- page = page + 1
- end
- end
- redraw(page)
- end
Add Comment
Please, Sign In to add comment