Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Where would you like to install tskmgr? (empty for current dir)")
- local path = "/"..shell.resolve(read())
- local tskmgr_path = path.."/tskmgr/"
- local main_program = [[
- local own_path = "]]..tskmgr_path..[["
- local task_path = "tasks.lua"
- local tasks = {}
- local desc_char = 0x1f
- local w,h = term.getSize()
- local scroll = 0
- local selected = 1
- local click_marked_selection = nil
- local must_save = false
- local function loadAll(path)
- local f = fs.open(path,"r")
- if f==nil then
- return {}
- end
- local t = textutils.unserialise(f.readAll())
- f.close()
- return t
- end
- local function saveAll(list)
- local f = fs.open(own_path..task_path,"w")
- f.write(textutils.serialise(list))
- f.close()
- end
- local function renderTasks(list)
- term.setBackgroundColour(colours.black)
- term.clear()
- local c = 0
- for i=1,#list do
- if list[i].completed==list[i].fullcomp then
- c = c + 1
- end
- end
- paintutils.drawLine(1,1,w,1,colours.green)
- term.setCursorPos(1,1)
- term.setTextColour(colours.white)
- write(c.."/"..#list.." tasks done")
- term.setBackgroundColour(colours.black)
- if selected<1 then selected=1
- elseif selected>#list then selected=#list end
- if #list>=h-2 then
- --if scroll>h-2-#list then scroll = h-2-#list end
- if scroll<0 then scroll = 0 end
- else
- scroll = 0
- end
- for i=scroll+1,#list do
- local y = ({term.getCursorPos()})[2]
- term.setCursorPos(1,y+1)
- if list[i].completed==list[i].fullcomp then
- term.setTextColour(colours.grey)
- else
- term.setTextColour(colours.white)
- end
- if i==selected then
- term.setBackgroundColour(colours.lightGrey)
- else
- term.setBackgroundColour(colours.black)
- end
- write(i..":["..list[i].completed.."/"..list[i].fullcomp.."] "..list[i].title)
- if list[i].desc~=nil then write(" \x1f") end
- y = ({term.getCursorPos()})[2]
- if click_marked_selection~=nil and y>=click_marked_selection then
- click_marked_selection=nil
- selected = i
- end
- if y>=h then break end
- end
- paintutils.drawLine(1,h,w,h,colours.grey)
- term.setCursorPos(1,h)
- term.setTextColour(colours.white)
- write("Press H for help")
- end
- local function deleteTask(list,id)
- if id>#list then return end
- if list[id].desc~=nil then
- fs.delete(own_path..list[id].desc)
- end
- table.remove(list,id)
- must_save = true
- end
- local function deleteAllCompletedTasks(list,id)
- local i = 1
- while i<=#list do
- if list[i].completed>=list[i].fullcomp then
- deleteTask(list,i)
- else
- i=i+1
- end
- end
- end
- local function deleteAllTasks(list,id)
- while #list>0 do
- deleteTask(list,1)
- end
- end
- local function selectById(list,id)
- write("\nID: ")
- selected = tonumber(read())
- end
- local function showHelp(list,id)
- shell.run("edit "..own_path.."help.txt")
- end
- local function makeTask(list,id)
- write("\nTitle: ")
- table.insert(list,{title=read(),completed=0})
- write("Steps (empty for 1): ")
- list[#list].fullcomp = tonumber(read()) or 1
- must_save = true
- end
- local function goUp(list,id)
- selected=selected-1
- end
- local function goDown(list,id)
- selected=selected+1
- end
- local function addPoint(list,id)
- list[id].completed = list[id].completed + 1
- if list[id].completed>list[id].fullcomp then list[id].completed=list[id].fullcomp end
- must_save = true
- end
- local function setPoints(list,id)
- write("\nSet to: ")
- list[id].completed = tonumber(read())
- if list[id].completed>list[id].fullcomp then list[id].completed=list[id].fullcomp end
- must_save = true
- end
- local function addDesc(list,id)
- if list[id].desc==nil then
- local name = id+tostring(os.time("utc"))
- fs.open(own_path..name,"w").close()
- list[id].desc=name
- must_save = true
- end
- shell.run("edit "..own_path..list[id].desc)
- end
- local function discardChar()
- local timer = os.startTimer(0)
- while true do
- local event, id = os.pullEvent()
- if event == "timer" and id == timer then break
- elseif event == "char" or event == "key" or event == "key_up" then
- os.cancelTimer(timer)
- break
- end
- end
- end
- tasks = loadAll(own_path..task_path)
- local keybinds = {h=showHelp, c=deleteTask, r=deleteAllCompletedTasks, R=deleteAllTasks,
- s=selectById, m=makeTask, up=goUp, UP=goUp, down=goDown, DOWN=goDown,
- a=addPoint,A=setPoints,enter=addDesc}
- while true do
- renderTasks(tasks)
- local ev = {os.pullEventRaw()}
- if ev[1]=="mouse_click" or ev[1]=="monitor_touch" then
- click_marked_selection = ev[4]
- elseif ev[1]=="mouse_scroll" then
- scroll=scroll+ev[2]
- elseif ev[1]=="key" then
- discardChar()
- local n = keys.getName(ev[2])
- if n == "e" then
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- term.setCursorPos(1,1)
- term.clear()
- break
- end
- if ev[3] then n=string.upper(n) end
- local f = keybinds[n]
- if f~=nil then
- f(tasks,selected)
- end
- if must_save then
- saveAll(tasks)
- if selected>#tasks then selected=#tasks end
- must_save = false
- end
- if selected-scroll>h-2 then scroll=selected-h+2 end
- if selected<scroll+1 then scroll = selected-1 end
- elseif ev[1]=="terminate" then
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- term.setCursorPos(1,1)
- term.clear()
- break
- end
- end]]
- local help = [[
- H: help
- up and down keys:
- move selection
- C: delete selected
- S: select by id
- R: delete all completed,
- delete all if held
- A: increase completion
- of selected by 1, if
- held asks for exact
- amount
- Enter: edit description
- M: make a task
- E or Ctrl+T: exit
- ]]
- fs.makeDir(tskmgr_path)
- local f = fs.open(tskmgr_path.."help.txt","w")
- f.write(help)
- f.close()
- f = fs.open(path.."/tskmgr.lua","w")
- f.write(main_program)
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement