Philipou

oppm gui 2

Aug 21st, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.95 KB | None | 0 0
  1. --Mynaptic Version 2.1 made by JakobDev
  2. local component = require("component")
  3. local event = require("event")
  4. local io = require("io")
  5. local term = require("term")
  6. local shell =require("shell")
  7. local os = require("os")
  8. local filesystem = require("filesystem")
  9. local gpu = component.gpu
  10. local w, h = gpu.getResolution()
  11. local tPackages = {}
  12. local tPackagesFull = {}
  13. local tMenu = {}
  14. local nScrollpos = 0
  15. local sSearch = ""
  16. local sOppmPath ="/usr/share/mynaptic/oppm-mynaptic.lua"
  17. local sHistoryPath = "/var/mynaptic/history.txt"
  18. local sVersion = "2.1"
  19.  
  20. if not filesystem.exists(sOppmPath) then
  21.     print("oppm not found")
  22.     return
  23. end
  24.  
  25. print("Starting Mynaptic. Please wait...")
  26. os.sleep(0.01)
  27.  
  28. local tHelp = {}
  29.  
  30. tTemp = {}
  31. tTemp["title"] = "What is OPPM and Mynaptic"
  32. tTemp["content"] = "OPPM is a Package Manager made by Vexatos which allows you simperl to install, remove and update Programs. \n\nMynaptic is a GUI for OPPM"
  33. table.insert(tHelp,tTemp)
  34.  
  35. tTemp = {}
  36. tTemp["title"] = "Basic Usage"
  37. tTemp["content"] = [[You can mark a Package by clicking it:
  38. White: Package is not installed
  39. Yellow: Package will be installed
  40. Green: Package is already installed
  41. Red: Package will be removed
  42. After you marked the Packages just click "Apply".
  43. You can scroll with the mouse whell in all Menus]]
  44. table.insert(tHelp,tTemp)
  45.  
  46. tTemp = {}
  47. tTemp["title"] = "Get Information about a Package"
  48. tTemp["content"] = "To get more Information about a Package just rightclick it. It may take a few seconds to fetch the Information"
  49. table.insert(tHelp,tTemp)
  50.  
  51. tTemp = {}
  52. tTemp["title"] = "Add your own Program"
  53. tTemp["content"] = "To add your own Program to OPPM/Mynaptic please read https://ocdoc.cil.li/tutorial:program:oppm"
  54. table.insert(tHelp,tTemp)
  55.  
  56. tTemp = {}
  57. tTemp["title"] = "License"
  58. tTemp["content"] = [[Copyright (c) 2018-2019, JakobDev
  59. All rights reserved.
  60. Redistribution and use in source and binary forms, with or without
  61. modification, are permitted provided that the following conditions are met:
  62. * Redistributions of source code must retain the above copyright notice, this
  63.   list of conditions and the following disclaimer.
  64. * Redistributions in binary form must reproduce the above copyright notice,
  65.   this list of conditions and the following disclaimer in the documentation
  66.   and/or other materials provided with the distribution.
  67. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  68. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  69. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  70. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  71. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  72. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  76. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.]]
  77. table.insert(tHelp,tTemp)
  78.  
  79. tTemp = {}
  80. tTemp["title"] = "About"
  81. tTemp["content"] = "Mynaptic for OPPM Version "..sVersion.." made by JakobDev"
  82. table.insert(tHelp,tTemp)
  83.  
  84. --Packges install type
  85. --0 not installed
  86. --1 installed
  87. --2 marked to install
  88. --3 mark to remove
  89.  
  90. local tInstalled = {}
  91. local installedHandle = io.popen(sOppmPath.." list -im")
  92. for linecon in installedHandle:lines() do
  93.     tInstalled[linecon] = true
  94. end
  95. installedHandle:close()
  96.  
  97. local packageHandle = io.popen(sOppmPath.." list -m")
  98. for linecon in packageHandle:lines() do
  99.     local tTemp = {}
  100.     tTemp.name = linecon
  101.     if tInstalled[linecon] == true then
  102.         tTemp.type = 1
  103.     else
  104.         tTemp.type = 0
  105.     end
  106.     table.insert(tPackages,tTemp)
  107.     table.insert(tPackagesFull,tTemp)
  108.     tTemp = nil
  109. end
  110. packageHandle:close()
  111.  
  112. local function redrawMenuBar()
  113.     gpu.setForeground(0x000000)
  114.     gpu.setBackground(0x0000ff)
  115.     gpu.fill(1,1,w,1," ")
  116.     local sMenu = ""
  117.     for k,v in ipairs(tMenu) do
  118.         sMenu = sMenu..v.text.." "
  119.     end
  120.     gpu.set(1,1,sMenu)
  121.     gpu.setBackground(0xff0000)
  122.     gpu.set(w,1,"X")
  123. end
  124.  
  125. local function drawEntry(tEntry,nPos)
  126.     if type(tEntry) == "table" then
  127.         if tEntry.type == 1 then
  128.             gpu.setBackground(0x00ff00)
  129.         elseif tEntry.type == 2 then
  130.             gpu.setBackground(0xffff00)
  131.         elseif tEntry.type == 3 then
  132.             gpu.setBackground(0xff0000)
  133.         else
  134.             gpu.setBackground(0xFFFFFF)
  135.         end
  136.         gpu.fill(1,nPos,w,1," ")
  137.         gpu.set(1,nPos,tEntry["name"])
  138.     end
  139. end
  140.  
  141. local function redrawList()
  142.     gpu.setForeground(0x000000)
  143.     gpu.setBackground(0xFFFFFF)
  144.     gpu.fill(1,2,w,h-2," ")
  145.     for i=2,h-1 do
  146.         drawEntry(tPackages[i-1+nScrollpos],i)
  147.     end
  148. end
  149.  
  150. local function redrawSearchBar()
  151.     gpu.setForeground(0x000000)
  152.     gpu.setBackground(0x0000ff)
  153.     gpu.fill(1,h,w,1," ")
  154.     gpu.set(1,h,"Search:"..sSearch)
  155. end
  156.  
  157. local function redrawAll()
  158.     redrawMenuBar()
  159.     redrawList()
  160.     redrawSearchBar()
  161. end
  162.  
  163. local function scrollUp()
  164.     gpu.copy(1,2,w,h-3,0,1)
  165.     drawEntry(tPackages[nScrollpos+1],2)
  166. end
  167.  
  168. local function scrollDown()
  169.     gpu.copy(1,3,w,h-3,0,-1)
  170.     drawEntry(tPackages[h-2+nScrollpos],h-1)
  171. end
  172.  
  173. local function createSearch()
  174.     tPackages = {}
  175.     for k,v in ipairs(tPackagesFull) do
  176.         if v.name:find(sSearch) ~= nil then
  177.             table.insert(tPackages,v)
  178.         end
  179.     end
  180.     nScrollpos = 0
  181.     redrawList()
  182. end
  183.  
  184. local function apply()
  185.     local tInstall = {}
  186.     local tRemove = {}
  187.     for k,v in ipairs(tPackagesFull) do
  188.         if v.type == 2 then
  189.             table.insert(tInstall,v)
  190.         elseif v.type == 3 then
  191.             table.insert(tRemove,v)
  192.         end
  193.     end
  194.     local nApplyPos = 0
  195.     local tText = {}
  196.     table.insert(tText,"This Packges will be installed:")
  197.     for k,v in ipairs(tInstall) do
  198.         table.insert(tText,v.name)
  199.     end
  200.     table.insert(tText,"This Packges will be removed:")
  201.     for k,v in ipairs(tRemove) do
  202.         table.insert(tText,v.name)
  203.     end
  204.     while true do
  205.         gpu.setForeground(0x000000)
  206.         gpu.setBackground(0xFFFFFF)
  207.         gpu.fill(1,1,w,h," ")
  208.         for i=1,h-1 do
  209.             if type(tText[i+nApplyPos]) == "string" then
  210.                 gpu.set(1,i,tText[i+nApplyPos])
  211.             end
  212.         end
  213.         gpu.setBackground(0x0000ff)
  214.         gpu.fill(1,h,w,1," ")
  215.         gpu.set(1,h,"Cancel")
  216.         gpu.set(w-1,h,"OK")
  217.         local tEvent = table.pack(event.pull())
  218.         if tEvent[1] == "scroll" then
  219.             if tEvent[5] == 1 and nApplyPos ~= 0 then
  220.                 nApplyPos = nApplyPos - 1
  221.             elseif tEvent[5] == -1 and (nApplyPos ~= #tText-h+1) and (#tText > h-1) then
  222.                 nApplyPos = nApplyPos + 1
  223.             end
  224.         elseif tEvent[1] == "touch" and tEvent[4] == h then
  225.             if tEvent[3] < 7 then
  226.                 return
  227.             elseif (tEvent[3] == w) or (tEvent[3] == w-1) then
  228.                 break
  229.             end
  230.         elseif tEvent[1] == "interrupted" then
  231.             return false  
  232.         end
  233.     end
  234.     gpu.setForeground(0x000000)
  235.     gpu.setBackground(0xFFFFFF)
  236.     gpu.fill(1,1,w,h," ")
  237.     term.setCursor(1,1)
  238.     filesystem.makeDirectory(filesystem.path(sHistoryPath))
  239.     local hisfile = io.open(sHistoryPath,"a")
  240.     for k,v in ipairs(tInstall) do
  241.         print("Install "..v.name)
  242.         os.sleep(0.01)
  243.         shell.execute(sOppmPath.." install "..v.name)
  244.         v.type = 1
  245.         hisfile:write("Installed "..v.name.."\n")
  246.     end
  247.     for k,v in ipairs(tRemove) do
  248.         print("Remove "..v.name)
  249.         os.sleep(0.01)
  250.         shell.execute(sOppmPath.." uninstall "..v.name)
  251.         v.type = 0
  252.         hisfile:write("Removed "..v.name.."\n")
  253.     end
  254.     hisfile:close()
  255. end
  256.  
  257. local function drawHistoryEntry(sText,nPos)
  258.     if sText:find("Installed") == 1 then
  259.         gpu.setForeground(0x00ff00)
  260.     elseif sText:find("Removed") == 1 then
  261.         gpu.setForeground(0xff0000)
  262.     else
  263.         gpu.setForeground(0x000000)
  264.     end
  265.     gpu.set(1,nPos,sText)
  266. end
  267.  
  268. local function history()
  269.     local tHistory = {}
  270.     local nHispos = 0
  271.     if filesystem.exists(sHistoryPath) then
  272.         local hisfile = io.open(sHistoryPath,"r")
  273.         for sLine in hisfile:lines() do
  274.             table.insert(tHistory,sLine)
  275.         end
  276.         hisfile:close()
  277.     else
  278.         tHistory[1] = "History is empty"
  279.     end
  280.     gpu.setForeground(0x000000)
  281.     gpu.setBackground(0xFFFFFF)
  282.     gpu.fill(1,1,w,h," ")
  283.     for i=1,h-1 do
  284.         if tHistory[i] then
  285.            drawHistoryEntry(tHistory[i],i)
  286.         end
  287.     end
  288.     gpu.setBackground(0x0000ff)
  289.     gpu.setForeground(0x000000)
  290.     gpu.fill(1,h,w,1," ")
  291.     gpu.set(1,h,"OK")
  292.     gpu.set(w-4,h,"Clear")
  293.     gpu.setForeground(0x000000)
  294.     gpu.setBackground(0xFFFFFF)
  295.     while true do
  296.         local tEvent = table.pack(event.pull())
  297.         if tEvent[1] == "scroll" then
  298.             if tEvent[5] == 1 and nHispos ~= 0 then
  299.                 nHispos = nHispos - 1
  300.                 gpu.copy(1,1,w,h-2,0,1)
  301.                 drawHistoryEntry(tHistory[nHispos+1],1)
  302.             elseif tEvent[5] == -1 and (nHispos ~= #tHistory-h+1) and (#tHistory > h-1) then
  303.                 nHispos = nHispos + 1
  304.                 gpu.copy(1,2,w,h-2,0,-1)
  305.                 if tHistory[nHispos+h-1] then
  306.                     drawHistoryEntry(tHistory[nHispos+h-1],h-1)
  307.                 end
  308.             end    
  309.         elseif tEvent[1] == "touch" and tEvent[4] == h then
  310.             if tEvent[3] < 3 then
  311.                 return
  312.             elseif tEvent[3] > w-5 then
  313.                 tHistory = {}
  314.                 gpu.setForeground(0x000000)
  315.                 gpu.setBackground(0xFFFFFF)
  316.                 gpu.fill(1,1,w,h-1," ")
  317.                 gpu.set(1,1,"History cleared")
  318.                 filesystem.remove(sHistoryPath)
  319.             end
  320.         elseif tEvent[1] == "interrupted" then
  321.             return false
  322.         end
  323.     end
  324. end
  325.  
  326. local function showHelp(sText)
  327.     gpu.setForeground(0x000000)
  328.     gpu.setBackground(0xFFFFFF)
  329.     gpu.fill(1,1,w,h-1," ")
  330.     term.setCursor(1,1)
  331.     term.write(sText)
  332.     gpu.setBackground(0x0000ff)
  333.     gpu.fill(1,h,w,1," ")
  334.     gpu.set(1,h,"OK")
  335.     while true do
  336.         local tEvent = table.pack(event.pull())
  337.         if tEvent[1] == "touch" and tEvent[4] == h then
  338.             return
  339.         elseif tEvent[1] == "interrupted" then
  340.             return false
  341.         end
  342.     end
  343. end
  344.  
  345. local function showHelpList()
  346.     gpu.setForeground(0x000000)
  347.     gpu.setBackground(0xFFFFFF)
  348.     gpu.fill(1,1,w,h-1," ")
  349.     for i=1,h-1 do
  350.         if tHelp[i] then
  351.             gpu.set(1,i,tHelp[i]["title"])
  352.         end
  353.     end
  354.     gpu.setBackground(0x0000ff)
  355.     gpu.fill(1,h,w,1," ")
  356.     gpu.set(1,h,"OK")
  357. end
  358.  
  359. local function helpList()
  360.     showHelpList()
  361.     while true do
  362.         local tEvent = table.pack(event.pull())
  363.         if tEvent[1] == "touch" then
  364.             if tEvent[4] == h then
  365.                 return
  366.             elseif tHelp[tEvent[4]] then
  367.                 if showHelp(tHelp[tEvent[4]]["content"]) == false then
  368.                     return false
  369.                 else
  370.                     showHelpList()
  371.                 end
  372.             end
  373.         elseif tEvent[1] == "interrupted" then
  374.             return false
  375.         end
  376.     end
  377. end
  378.  
  379. tMenu[1] = {text="Apply",func=apply}
  380. tMenu[2] = {text="History",func=history}
  381. tMenu[3] = {text="Help",func=helpList}
  382.  
  383. gpu.fill(1, 1, w, h, " ")
  384. gpu.setForeground(0x000000)
  385. gpu.setBackground(0xFFFFFF)
  386. redrawAll()
  387.  
  388. local function main()
  389. while true do
  390.     local tEvent = table.pack(event.pull())
  391.     if tEvent[1] == "scroll" then
  392.         if tEvent[5] == 1 and nScrollpos ~= 0 then
  393.             nScrollpos = nScrollpos - 1
  394.             scrollUp()
  395.         elseif tEvent[5] == -1 and (nScrollpos ~= #tPackages-h+2) and (#tPackages > h-2) then
  396.             nScrollpos = nScrollpos + 1
  397.             --redrawList()
  398.             scrollDown()
  399.         end    
  400.         --redrawList()
  401.     elseif tEvent[1] == "touch" and tEvent[5] == 0 then
  402.         if tEvent[4] == 1 then
  403.             if tEvent[3] == w then
  404.                 break
  405.             else
  406.                 local nMenuPos = 0
  407.                 for k,v in ipairs(tMenu) do
  408.                     if tEvent[3] > nMenuPos and tEvent[3] < nMenuPos+#v.text+1 then
  409.                         if v.func() == false then
  410.                             return
  411.                         else
  412.                             redrawAll()
  413.                         end
  414.                         break
  415.                     else
  416.                         nMenuPos = nMenuPos+#v.text+1
  417.                     end
  418.                 end
  419.             end
  420.         elseif tEvent[4] == h then
  421.             sSearch = ""
  422.             createSearch()
  423.             redrawSearchBar()
  424.         elseif type(tPackages[tEvent[4]+nScrollpos-1]) == "table" then
  425.             local tTemp = tPackages[tEvent[4]+nScrollpos-1]
  426.             if tTemp["type"] == 0 then
  427.                 tTemp["type"] = 2
  428.             elseif tTemp["type"] == 1 then
  429.                 tTemp["type"] = 3
  430.             elseif tTemp["type"] == 2 then
  431.                 tTemp["type"] = 0
  432.             elseif tTemp["type"] == 3 then
  433.                 tTemp["type"] = 1
  434.             end
  435.             --redrawList()
  436.             drawEntry(tTemp,tEvent[4])
  437.         end
  438.     elseif tEvent[1] == "touch" and tEvent[5] == 1 then
  439.         if type(tPackages[tEvent[4]+nScrollpos-1]) == "table" then
  440.             gpu.setForeground(0x000000)
  441.             gpu.setBackground(0xFFFFFF)
  442.             gpu.fill(1, 1, w, h, " ")
  443.             term.setCursor(1,1)
  444.             term.write("Getting Information about the Package. Please wait...")
  445.             term.setCursor(1,1)
  446.             os.sleep(0.01)
  447.             shell.execute(sOppmPath.." info "..tPackages[tEvent[4]+nScrollpos-1]["name"])
  448.             gpu.setForeground(0x000000)
  449.             gpu.setBackground(0x0000ff)
  450.             gpu.fill(1,h,w,1," ")
  451.             gpu.set(1,h,"OK")
  452.             local bInteruptet = false
  453.             while true do
  454.                 local tEvent = table.pack(event.pull())
  455.                 if tEvent[1] == "touch" and tEvent[4] == h then
  456.                     redrawAll()
  457.                     break
  458.                 elseif tEvent[1] == "interrupted" then
  459.                     bInteruptet = true
  460.                     break
  461.                 end
  462.             end
  463.             if bInteruptet == true then
  464.                 break
  465.             end
  466.         end
  467.     elseif tEvent[1] == "key_down" then
  468.         if tEvent[3] == 8 then
  469.             sSearch = sSearch:sub(1,-2)
  470.         else
  471.             sSearch = sSearch..string.char(tEvent[3])
  472.         end
  473.         createSearch()
  474.         redrawSearchBar()
  475.     elseif tEvent[1] == "interrupted" then
  476.         return
  477.     end
  478. end
  479. end
  480.  
  481. main()
  482.  
  483. gpu.setBackground(0x000000)
  484. term.clear()
Add Comment
Please, Sign In to add comment