Advertisement
AdditionalPylons

tskmgr installer

Mar 22nd, 2025 (edited)
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.30 KB | None | 0 0
  1. print("Where would you like to install tskmgr? (empty for current dir)")
  2. local path = "/"..shell.resolve(read())
  3. local tskmgr_path = path.."/tskmgr/"
  4. local main_program = [[
  5. local own_path = "]]..tskmgr_path..[["
  6. local task_path = "tasks.lua"
  7. local tasks = {}
  8. local desc_char = 0x1f
  9. local w,h = term.getSize()
  10. local scroll = 0
  11. local selected = 1
  12. local click_marked_selection = nil
  13. local must_save = false
  14. local function loadAll(path)
  15.     local f = fs.open(path,"r")
  16.     if f==nil then
  17.         return {}
  18.     end
  19.     local t = textutils.unserialise(f.readAll())
  20.     f.close()
  21.     return t
  22. end
  23. local function saveAll(list)
  24.     local f = fs.open(own_path..task_path,"w")
  25.     f.write(textutils.serialise(list))
  26.     f.close()
  27. end
  28. local function renderTasks(list)
  29.     term.setBackgroundColour(colours.black)
  30.     term.clear()
  31.     local c = 0
  32.     for i=1,#list do
  33.         if list[i].completed==list[i].fullcomp then
  34.             c = c + 1
  35.         end
  36.     end
  37.     paintutils.drawLine(1,1,w,1,colours.green)
  38.     term.setCursorPos(1,1)
  39.     term.setTextColour(colours.white)
  40.     write(c.."/"..#list.." tasks done")
  41.     term.setBackgroundColour(colours.black)
  42.     if selected<1 then selected=1
  43.     elseif selected>#list then selected=#list end
  44.     if #list>=h-2 then
  45.         --if scroll>h-2-#list then scroll = h-2-#list end
  46.         if scroll<0 then scroll = 0 end
  47.     else
  48.         scroll = 0
  49.     end
  50.     for i=scroll+1,#list do
  51.         local y = ({term.getCursorPos()})[2]
  52.         term.setCursorPos(1,y+1)
  53.         if list[i].completed==list[i].fullcomp then
  54.             term.setTextColour(colours.grey)
  55.         else
  56.             term.setTextColour(colours.white)
  57.         end
  58.         if i==selected then
  59.             term.setBackgroundColour(colours.lightGrey)
  60.         else
  61.             term.setBackgroundColour(colours.black)
  62.         end
  63.         write(i..":["..list[i].completed.."/"..list[i].fullcomp.."] "..list[i].title)
  64.         if list[i].desc~=nil then write(" \x1f") end
  65.         y = ({term.getCursorPos()})[2]
  66.         if click_marked_selection~=nil and y>=click_marked_selection then
  67.             click_marked_selection=nil
  68.             selected = i
  69.         end
  70.         if y>=h then break end
  71.     end
  72.     paintutils.drawLine(1,h,w,h,colours.grey)
  73.     term.setCursorPos(1,h)
  74.     term.setTextColour(colours.white)
  75.     write("Press H for help")
  76. end
  77. local function deleteTask(list,id)
  78.     if id>#list then return end
  79.     if list[id].desc~=nil then
  80.         fs.delete(own_path..list[id].desc)
  81.     end
  82.     table.remove(list,id)
  83.     must_save = true
  84. end
  85.  
  86. local function deleteAllCompletedTasks(list,id)
  87.     local i = 1
  88.     while i<=#list do
  89.         if list[i].completed>=list[i].fullcomp then
  90.             deleteTask(list,i)
  91.         else
  92.             i=i+1
  93.         end
  94.     end
  95. end
  96. local function deleteAllTasks(list,id)
  97.     while #list>0 do
  98.         deleteTask(list,1)
  99.     end
  100. end
  101. local function selectById(list,id)
  102.     write("\nID: ")
  103.     selected = tonumber(read())
  104. end
  105. local function showHelp(list,id)
  106.     shell.run("edit "..own_path.."help.txt")
  107. end
  108. local function makeTask(list,id)
  109.     write("\nTitle: ")
  110.     table.insert(list,{title=read(),completed=0})
  111.     write("Steps (empty for 1): ")
  112.     list[#list].fullcomp = tonumber(read()) or 1
  113.     must_save = true
  114. end
  115. local function goUp(list,id)
  116.     selected=selected-1
  117. end
  118. local function goDown(list,id)
  119.     selected=selected+1
  120. end
  121. local function addPoint(list,id)
  122.     list[id].completed = list[id].completed + 1
  123.     if list[id].completed>list[id].fullcomp then list[id].completed=list[id].fullcomp end
  124.     must_save = true
  125. end
  126. local function setPoints(list,id)
  127.     write("\nSet to: ")
  128.     list[id].completed = tonumber(read())
  129.     if list[id].completed>list[id].fullcomp then list[id].completed=list[id].fullcomp end
  130.     must_save = true
  131. end
  132. local function addDesc(list,id)
  133.     if list[id].desc==nil then
  134.         local name = id+tostring(os.time("utc"))
  135.         fs.open(own_path..name,"w").close()
  136.         list[id].desc=name
  137.         must_save = true
  138.     end
  139.     shell.run("edit "..own_path..list[id].desc)
  140. end
  141.  
  142.  
  143. local function discardChar()
  144.     local timer = os.startTimer(0)
  145.     while true do
  146.         local event, id = os.pullEvent()
  147.         if event == "timer" and id == timer then break
  148.         elseif event == "char" or event == "key" or event == "key_up" then
  149.             os.cancelTimer(timer)
  150.             break
  151.         end
  152.     end
  153. end
  154.  
  155. tasks = loadAll(own_path..task_path)
  156. local keybinds = {h=showHelp, c=deleteTask, r=deleteAllCompletedTasks, R=deleteAllTasks,
  157.                 s=selectById, m=makeTask, up=goUp, UP=goUp, down=goDown, DOWN=goDown,
  158.                 a=addPoint,A=setPoints,enter=addDesc}
  159. while true do
  160.     renderTasks(tasks)
  161.     local ev = {os.pullEventRaw()}
  162.     if ev[1]=="mouse_click" or ev[1]=="monitor_touch" then
  163.         click_marked_selection = ev[4]
  164.     elseif ev[1]=="mouse_scroll" then
  165.         scroll=scroll+ev[2]
  166.     elseif ev[1]=="key" then
  167.         discardChar()
  168.         local n = keys.getName(ev[2])
  169.         if n == "e" then
  170.             term.setBackgroundColour(colours.black)
  171.             term.setTextColour(colours.white)
  172.             term.setCursorPos(1,1)
  173.             term.clear()
  174.             break
  175.         end
  176.         if ev[3] then n=string.upper(n) end
  177.         local f = keybinds[n]
  178.         if f~=nil then
  179.             f(tasks,selected)
  180.         end
  181.  
  182.         if must_save then
  183.             saveAll(tasks)
  184.             if selected>#tasks then selected=#tasks end
  185.             must_save = false
  186.         end
  187.         if selected-scroll>h-2 then scroll=selected-h+2 end
  188.         if selected<scroll+1 then scroll = selected-1 end
  189.     elseif ev[1]=="terminate" then
  190.         term.setBackgroundColour(colours.black)
  191.         term.setTextColour(colours.white)
  192.         term.setCursorPos(1,1)
  193.         term.clear()
  194.         break
  195.     end
  196. end]]
  197. local help = [[
  198. H: help
  199. up and down keys:
  200.    move selection
  201. C: delete selected
  202. S: select by id
  203. R: delete all completed,
  204.    delete all if held
  205. A: increase completion
  206.    of selected by 1, if
  207.    held asks for exact
  208.    amount
  209. Enter: edit description
  210. M: make a task
  211. E or Ctrl+T: exit
  212. ]]
  213. fs.makeDir(tskmgr_path)
  214. local f = fs.open(tskmgr_path.."help.txt","w")
  215. f.write(help)
  216. f.close()
  217. f = fs.open(path.."/tskmgr.lua","w")
  218. f.write(main_program)
  219. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement