Advertisement
DYankee

Program Menu

Mar 20th, 2022 (edited)
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.79 KB | None | 0 0
  1. PasteId = "AHEsJqjW"
  2.  
  3. --initialze
  4. V = settings.get("Version")
  5. W,H = term.getSize()
  6.  
  7. --text pos func
  8. function Print_left(y,s)
  9.     local w,h = term.getSize()
  10.     local x = 1
  11.     term.setCursorPos(x,y)
  12.     term.clearLine()
  13.     term.write(s)
  14. end
  15.  
  16. function Print_centered(y,s)
  17.     local w,h = term.getSize()
  18.     local x = math.floor((w - string.len(s)) /2)
  19.     term.setCursorPos(x,y)
  20.     term.clearLine()
  21.     term.write(s)
  22. end
  23.  
  24. function Print_right(y,s)
  25.     local w,h = term.getSize()
  26.     local x = math.floor(w - string.len(s))
  27.     term.setCursorPos(x,y)
  28.     term.clearLine()
  29.     term.write(s)
  30. end
  31.  
  32. --Get program list
  33. if fs.isDir("/os/programs") == false then
  34.     fs.makeDir"/os/programs"
  35. end
  36. Plist = fs.list("/os/programs")
  37. ProgNumber = #Plist
  38.  
  39. --getsizes
  40. UsedSpace = 9
  41. ListSpace = H - UsedSpace
  42. MOption = 1
  43. PageNum = 1
  44. if ProgNumber <= ListSpace then
  45.     ListLng = ProgNumber
  46. elseif ProgNumber > ListSpace then
  47.     ListLng = ListSpace
  48. end
  49. function Get_list_space()
  50.     ListSpace = H - UsedSpace
  51.     if ProgNumber <= ListSpace then
  52.         ListLng = ProgNumber
  53.     elseif ProgNumber > ListSpace then
  54.         ListLng = ListSpace
  55.     end
  56. end
  57.  
  58. --Draw menu function
  59. function Draw_menu()
  60.     term.clear()
  61.     Print_centered(1,"Lapis OS " .. V )
  62.     term.setCursorPos(W-11,1)
  63.     term.write(MOption .. " " .. PageNum)
  64. end
  65.  
  66. function Draw_prog_menu()
  67.     Get_list_space()
  68.     ProgLeft = ProgNumber - (ListLng * (PageNum - 1))
  69.     Print_centered(2, "")
  70.     Print_centered(3, "Program list")
  71.     Print_centered(4, "")
  72.     if ProgNumber <= ListSpace then
  73.         for i = 1, ProgNumber, 1 do
  74.             Print_centered(4 + i, ((MOption == i and "[ " .. Plist[i] .. " ]") or Plist[i]))
  75.         end
  76.         Print_centered(H - 1, ((MOption == ProgNumber + 1 and "[ Manage Programs ]") or "Manage Programs"))
  77.         Print_centered(H, ((MOption == ProgNumber + 2 and "[ Back ]") or " Back " ))
  78.     elseif ProgNumber > ListSpace then
  79.         if PageNum == 1 then
  80.             for i = 1, ListSpace, 1 do
  81.                 Print_centered(4 + i, ((MOption == i and "[ " .. Plist[i] .. " ]") or Plist[i]))
  82.             end
  83.             Print_centered(H - 4 ,((MOption == ListLng + 1 and "[Next page]") or "Next page"))
  84.             Print_centered(H - 1, ((MOption == ListLng + 2 and "[ Manage Programs ]") or "Manage Programs"))
  85.             Print_centered(H, ((MOption == ListLng + 3 and "[ Back ]") or " Back " ))
  86.         elseif PageNum > 1 then
  87.             if ProgLeft > ListLng then
  88.                 Ci = ListLng * (PageNum - 1)
  89.                 for i = Ci + 1, ListLng + 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 == ListLng + 1 and "[Next page]") or "Next page"))
  93.                 Print_centered(H - 3,((MOption == ListLng + 2 and "[Prev page]") or "Prev page"))
  94.                 Print_centered(H - 1,((MOption == ListLng + 3 and "[ Manage Programs ]") or "Manage Programs"))
  95.                 Print_centered(H, ((MOption == ListLng + 4 and "[ Back ]") or " Back " ))
  96.             elseif ProgLeft <= ListLng then
  97.                 Ci = ListLng * (PageNum - 1)
  98.                 for i = Ci + 1, ProgLeft + Ci, 1 do
  99.                     Print_centered(4 + i - Ci, ((MOption == i - Ci  and "[ " .. Plist[i] .. " ]") or Plist[i]))
  100.                 end
  101.                 Print_centered(H - 4,((MOption == ProgLeft + 1 and "[Prev page]") or "Prev page"))
  102.                 Print_centered(H - 1,((MOption == ProgLeft + 2 and "[ Manage Programs ]") or "Manage Programs"))
  103.                 Print_centered(H, ((MOption == ProgLeft + 3 and "[ Back ]") or " Back " ))
  104.             end
  105.         end
  106.     end
  107. end
  108.  
  109.  
  110. --Display
  111. PageNum = 1
  112. Draw_menu()
  113. Draw_prog_menu()
  114.  
  115. --get user imput for menu
  116.  
  117.  
  118. --menu logic
  119. function Get_menu_input()
  120.     function Get_menu_input_P1(max)
  121.         while true do
  122.             local event, key = os.pullEvent()
  123.             if event == "key" then
  124.                 if key == 265 or key == 87 then
  125.                     if MOption > 1 then
  126.                         MOption = MOption - 1
  127.                         Draw_menu()
  128.                         Draw_prog_menu()
  129.                     end
  130.                 elseif key == 264 or key == 83 then
  131.                     if MOption < max then
  132.                         MOption = MOption + 1
  133.                         Draw_menu()
  134.                         Draw_prog_menu()
  135.                     end
  136.                 elseif key == 257 then
  137.                     break
  138.                 end
  139.             end
  140.         end
  141.     end
  142.     while true do
  143.         if ProgNumber <= ListSpace then
  144.             Get_menu_input_P1(ProgNumber + 2)
  145.             break
  146.         elseif ProgNumber > ListSpace then
  147.             if PageNum == 1 then
  148.                 Get_menu_input_P1(ListLng + 3)
  149.                 break
  150.             elseif PageNum > 1 then
  151.                 if ProgLeft > ListLng then
  152.                     Get_menu_input_P1(ListLng + 4)
  153.                     break
  154.                 elseif ProgLeft <= ListLng then
  155.                     Get_menu_input_P1(ProgLeft + 3)
  156.                     break
  157.                 end
  158.             end
  159.         end
  160.     end
  161. end
  162.  
  163. function Menu_logic()
  164.     Ci = ListLng * (PageNum - 1)
  165.     if ProgNumber <= ListSpace then
  166.         if MOption <= ProgNumber then
  167.             os.run({}, "/os/programs/"..Plist[MOption + Ci])
  168.             Run = false
  169.         elseif MOption == ProgNumber + 1 then
  170.             shell.run("/os/managePrograms")
  171.         elseif MOption == ProgNumber + 2 then
  172.             shell.run("/os/menu")
  173.         end
  174.     elseif ProgNumber > ListSpace then
  175.         if PageNum == 1 then
  176.             if MOption <= ListLng then
  177.                 os.run({}, "/os/programs/"..Plist[MOption + Ci])
  178.                 Run = false
  179.             elseif MOption == ListLng + 1 then
  180.                 PageNum = PageNum + 1
  181.                 MOption = 1
  182.                 Draw_menu()
  183.                 Draw_prog_menu()
  184.             elseif MOption == ListLng + 2 then
  185.                 shell.run("/os/managePrograms")
  186.             elseif MOption == ListLng + 3 then
  187.                 shell.run("/os/menu")
  188.             end
  189.         elseif PageNum > 1 then
  190.             if ProgLeft > ListLng then
  191.                 if MOption <= ListLng then
  192.                     os.run({}, "/os/programs/"..Plist[MOption + Ci])
  193.                     Run = false
  194.                 elseif MOption == ListLng + 1 then
  195.                     PageNum = PageNum + 1
  196.                     MOption = 1
  197.                     Draw_menu()
  198.                     Draw_prog_menu()
  199.                 elseif MOption == ListLng + 2 then
  200.                     PageNum = PageNum - 1
  201.                     MOption = 1
  202.                     Draw_menu()
  203.                     Draw_prog_menu()
  204.                 elseif MOption == ListLng + 3 then
  205.                     shell.run("/os/managePrograms")
  206.                 elseif MOption == ListLng + 4 then
  207.                     shell.run("/os/menu")
  208.                 end
  209.             elseif ProgLeft <= ListLng then
  210.                 if MOption <= ProgLeft then
  211.                     os.run({}, "/os/programs/"..Plist[MOption + Ci])
  212.                     Run = false
  213.                 elseif MOption == ProgLeft + 1 then
  214.                     PageNum = PageNum - 1
  215.                     MOption = 1
  216.                     Draw_menu()
  217.                     Draw_prog_menu()
  218.                 elseif MOption == ProgLeft + 2 then
  219.                     shell.run("/os/managePrograms")
  220.                 elseif MOption == ProgLeft + 3 then
  221.                     shell.run("/os/menu")
  222.                 end
  223.             end
  224.         end
  225.     end
  226. end
  227.  
  228. function Run()
  229.     Draw_menu()
  230.     Draw_prog_menu()
  231.     Run = true
  232.     while true do
  233.         Get_menu_input()
  234.         Menu_logic()
  235.     end
  236. end
  237. Run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement