DYankee

Manage programs

Mar 24th, 2022 (edited)
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.66 KB | None | 0 0
  1. PasteId = "jJ7xa9hX"
  2.  
  3. V = settings.get("Version")
  4. local W,H = term.getSize()
  5.  
  6. -- print centered on x axis
  7. function Print_centered(y,s)
  8.     local x = math.floor((W - string.len(s)) /2)
  9.     term.setCursorPos(x,y)
  10.     term.clearLine()
  11.     term.write(s)
  12. end
  13.  
  14. function PrintLeft(y,s)
  15.     local x = 1
  16.     term.setCursorPos(x,y)
  17.     term.clearLine()
  18.     term.write(s)
  19. end
  20.  
  21. function Print_right(y,s)
  22.     local x = math.floor(W - string.len(s))
  23.     term.setCursorPos(x,y)
  24.     term.clearLine()
  25.     term.write(s)
  26. end
  27.  
  28. Plist = fs.list("/os/programs")
  29. ProgNumber = table.getn(Plist)
  30.  
  31. --getsizes
  32. UsedSpace = 9
  33. ListSpace = H - UsedSpace
  34. MOption = 1
  35. PageNum = 1
  36. if ProgNumber <= ListSpace then
  37.     ListLng = ProgNumber
  38. elseif ProgNumber > ListSpace then
  39.     ListLng = ListSpace
  40. end
  41.  
  42. function Draw_menu()
  43.     term.clear()
  44.     Print_centered(1,"Lapis OS " .. V )
  45.     term.setCursorPos(W-11,1)
  46.     term.write(MOption .. " " .. PageNum)
  47. end
  48.  
  49. --GUI
  50. function Draw_front_end()
  51.     Print_centered(math.floor(H/2) - 3, "")
  52.     Print_centered(math.floor(H/2) - 2, "Manage programs")
  53.     Print_centered(math.floor(H/2) - 1, "")
  54.     Print_centered(math.floor(H/2) + 0, ((MOption == 1 and "[ Add program    ]") or " Add program    " ))
  55.     Print_centered(math.floor(H/2) + 1, ((MOption == 2 and "[ Remove program ]") or " Remove program " ))
  56.     Print_centered(math.floor(H/2) + 2, ((MOption == 3 and "[ Update program ]") or " Update program " ))
  57.     Print_centered(math.floor(H/2) + 3, "")
  58.     Print_centered(math.floor(H/2) + 4, ((MOption == 4 and "[ Back ]") or " Back " ))
  59. end
  60.  
  61. function Draw_prog_menu()
  62.     ProgLeft = ProgNumber - (ListLng * (PageNum - 1))
  63.     Print_centered(2, "")
  64.     Print_centered(3, "select program")
  65.     Print_centered(4, "")
  66.     if ProgNumber <= ListSpace then
  67.         for i = 1, ProgNumber, 1 do
  68.             Print_centered(4 + i, ((MOption == i and "[ " .. Plist[i] .. " ]") or Plist[i]))
  69.         end
  70.         Print_centered(H, ((MOption == ProgNumber + 1 and "[ Back ]") or " Back " ))
  71.     elseif ProgNumber > ListSpace then
  72.         if PageNum == 1 then
  73.             for i = 1, ListSpace, 1 do
  74.                 Print_centered(4 + i, ((MOption == i and "[ " .. Plist[i] .. " ]") or Plist[i]))
  75.             end
  76.             Print_centered(H - 4 ,((MOption == ListLng + 1 and "[Next page]") or "Next page"))
  77.             Print_centered(H, ((MOption == ListLng + 2 and "[ Back ]") or " Back " ))
  78.         elseif PageNum > 1 then
  79.             if ProgLeft > ListLng then
  80.                 Ci = ListLng * (PageNum - 1)
  81.                 for i = Ci + 1, ListLng + Ci, 1 do
  82.                     Print_centered(4 + i - Ci, ((MOption == i - Ci and "[ " .. Plist[i] .. " ]") or Plist[i]))
  83.                 end
  84.                 Print_centered(H - 4,((MOption == ListLng + 1 and "[Next page]") or "Next page"))
  85.                 Print_centered(H - 3,((MOption == ListLng + 2 and "[Prev page]") or "Prev page"))
  86.                 Print_centered(H, ((MOption == ListLng + 3 and "[ Back ]") or " Back " ))
  87.             elseif ProgLeft <= ListLng then
  88.                 Ci = ListLng * (PageNum - 1)
  89.                 for i = Ci + 1, ProgLeft + Ci, 1 do
  90.                     Print_centered(4 + i - Ci, ((MOption == i - Ci  and "[ " .. Plist[i] .. " ]") or Plist[i]))
  91.                 end
  92.                 Print_centered(H - 4,((MOption == ProgLeft + 1 and "[Prev page]") or "Prev page"))
  93.                 Print_centered(H, ((MOption == ProgLeft + 2 and "[ Back ]") or " Back " ))
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. function Get_menu_input(max, whichMenu)
  100.     while true do
  101.         local event, key = os.pullEvent()
  102.         if event == "key" then
  103.             if key == 265 or key == 87 then
  104.                 if MOption > 1 then
  105.                     MOption = MOption - 1
  106.                     Draw_menu()
  107.                     whichMenu()
  108.                 end
  109.             elseif key == 264 or key == 83 then
  110.                 if MOption < max then
  111.                     MOption = MOption + 1
  112.                     Draw_menu()
  113.                     whichMenu()
  114.                 end
  115.             elseif key == 257 then
  116.                 break
  117.             end
  118.         end
  119.     end
  120. end
  121.  
  122. function Add_program()
  123.     term.clear()
  124.     Print_centered(math.floor(H/2) - 6, "Add Program")
  125.     PrintLeft(math.floor(H/2) - 5, "Enter program name")
  126.     term.setCursorPos(1,(math.floor(H/2) - 4))
  127.     local programName = read()
  128.     term.setCursorPos(1,1)
  129.     term.write(programName)
  130.     PrintLeft(math.floor(H/2) - 3, "Enter pastebin directory")
  131.     term.setCursorPos(1,(math.floor(H/2) - 2))
  132.     local paste = read()
  133.     shell.run("pastebin", "get", paste, "/os/programs/"..programName)
  134.     PrintLeft(math.floor(H/2) ,"Program Installed")
  135.     sleep(3)
  136.     shell.run("/os/managePrograms")
  137. end
  138.  
  139. function Delete_program()
  140.     MOption = 1
  141.     local menu = Draw_prog_menu
  142.     Draw_menu()
  143.     menu()
  144.     Ci = ListLng * (PageNum - 1)
  145.     while true do
  146.         if ProgNumber <= ListSpace then
  147.             Get_menu_input(ProgNumber + 1, menu )
  148.             if MOption <= ProgNumber then
  149.                 fs.delete("/os/programs/"..Plist[MOption])
  150.                 shell.run("/os/managePrograms")
  151.             elseif MOption == ProgNumber + 1 then
  152.                 shell.run("/os/managePrograms")
  153.             end
  154.         elseif ProgNumber > ListSpace then
  155.             if PageNum == 1 then
  156.                 Get_menu_input(ListLng + 2, menu)
  157.                 if MOption <= ListLng then
  158.                     fs.delete("/os/programs/"..Plist[MOption])
  159.                     shell.run("/os/managePrograms")
  160.                 elseif MOption == ListLng + 1 then
  161.                     PageNum = PageNum + 1
  162.                     MOption = 1
  163.                     Draw_menu()
  164.                     menu()
  165.                 elseif MOption == ListLng + 2 then
  166.                     shell.run("/os/managePrograms")
  167.                 end
  168.             elseif PageNum > 1 then
  169.                 if ProgLeft > ListLng then
  170.                     Get_menu_input(ListLng + 3, menu)
  171.                     if MOption <= ListLng then
  172.                         fs.delete("/os/programs/"..Plist[MOption + Ci])
  173.                         shell.run("/os/managePrograms")
  174.                     elseif MOption == ListLng + 1 then
  175.                         PageNum = PageNum + 1
  176.                         MOption = 1
  177.                         Draw_menu()
  178.                         menu()
  179.                     elseif MOption == ListLng + 2 then
  180.                         PageNum = PageNum - 1
  181.                         MOption = 1
  182.                         Draw_menu()
  183.                         menu()
  184.                     elseif MOption == ListLng + 3 then
  185.                         shell.run("/os/managePrograms")
  186.                     end
  187.                 elseif ProgLeft <= ListLng then
  188.                     Get_menu_input(ProgLeft + 2, menu)
  189.                     if MOption <= ProgLeft then
  190.                         fs.delete("/os/programs/"..Plist[MOption + Ci])
  191.                         shell.run("/os/managePrograms")
  192.                     elseif MOption == ProgLeft + 1 then
  193.                         if PageNum > 1 then
  194.                             PageNum = PageNum - 1
  195.                         end
  196.                         MOption = 1
  197.                         Draw_menu()
  198.                         menu()
  199.                     elseif MOption == ProgLeft + 2 then
  200.                         shell.run("/os/managePrograms")
  201.                     end
  202.                 end
  203.             end
  204.         end
  205.     end
  206. end
  207.  
  208. function Update_program()
  209.     Ci = ListLng * (PageNum - 1)
  210.     function Update()
  211.         local file = fs.open("/os/programs/"..Plist[MOption + Ci], "r")
  212.         local pastId = file.readLine()
  213.         file.close()
  214.         local pName = Plist[MOption]
  215.         fs.delete("/os/programs/"..Plist[MOption])
  216.         shell.run("pastebin", "get", string.sub(pastId,10), "/os/programs/"..pName)
  217.         shell.run("/os/managePrograms")
  218.     end
  219.     MOption = 1
  220.     local menu = Draw_prog_menu
  221.     Draw_menu()
  222.     menu()
  223.     while true do
  224.         if ProgNumber <= ListSpace then
  225.             Get_menu_input(ProgNumber + 1, menu )
  226.             if MOption <= ProgNumber then
  227.                 Update()
  228.                 shell.run("/os/managePrograms")
  229.             elseif MOption == ProgNumber + 1 then
  230.                 shell.run("/os/managePrograms")
  231.             end
  232.         elseif ProgNumber > ListSpace then
  233.             if PageNum == 1 then
  234.                 Get_menu_input(ListLng + 2, menu)
  235.                 if MOption <= ListLng then
  236.                     Update()
  237.                     shell.run("/os/managePrograms")
  238.                 elseif MOption == ListLng + 1 then
  239.                     PageNum = PageNum + 1
  240.                     MOption = 1
  241.                     Draw_menu()
  242.                     menu()
  243.                 elseif MOption == ListLng + 2 then
  244.                     shell.run("/os/managePrograms")
  245.                 end
  246.             elseif PageNum > 1 then
  247.                 if ProgLeft > ListLng then
  248.                     Get_menu_input(ListLng + 3, menu)
  249.                     if MOption <= ListLng then
  250.                         Update()
  251.                         shell.run("/os/managePrograms")
  252.                     elseif MOption == ListLng + 1 then
  253.                         PageNum = PageNum + 1
  254.                         MOption = 1
  255.                         Draw_menu()
  256.                         menu()
  257.                     elseif MOption == ListLng + 2 then
  258.                         PageNum = PageNum - 1
  259.                         MOption = 1
  260.                         Draw_menu()
  261.                         menu()
  262.                     elseif MOption == ListLng + 3 then
  263.                         shell.run("/os/managePrograms")
  264.                     end
  265.                 elseif ProgLeft <= ListLng then
  266.                     Get_menu_input(ProgLeft + 2, menu)
  267.                     if MOption <= ProgLeft then
  268.                         Update()
  269.                         shell.run("/os/managePrograms")
  270.                     elseif MOption == ProgLeft + 1 then
  271.                         if PageNum > 1 then
  272.                             PageNum = PageNum - 1
  273.                         end
  274.                         MOption = 1
  275.                         Draw_menu()
  276.                         menu()
  277.                     elseif MOption == ProgLeft + 2 then
  278.                         shell.run("/os/managePrograms")
  279.                     end
  280.                 end
  281.             end
  282.         end
  283.     end
  284. end
  285.  
  286. MOption = 1
  287. Draw_menu()
  288. Draw_front_end()
  289. Get_menu_input(4, Draw_front_end)
  290. if MOption == 1 then
  291.     Add_program()
  292. elseif MOption == 2 then
  293.     Delete_program()
  294. elseif MOption == 3 then
  295.     Update_program()
  296. elseif MOption == 4 then
  297.     shell.run("/os/programMenu")
  298. end
Add Comment
Please, Sign In to add comment