JustDoesGames

PCM

Aug 28th, 2020 (edited)
2,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.61 KB | None | 0 0
  1. --[[
  2.  
  3. PCM (PC Manager)
  4. Just Does Games
  5.  
  6. Usages:
  7. Ctrl-f or 'toggleconsole' = toggle bar
  8. 'scanfile' = scan file for key api words
  9.  
  10. Version 1.0
  11.  
  12. release
  13.  
  14. Version 1.1
  15.  
  16. +Added PED
  17. +Added version and color requirement
  18. +Added scanfile command
  19.  
  20. Version 1.2
  21.  
  22. +Patched pasting to console
  23. +Added support for multi-arg for scanfile command and redesigned program
  24. +Added history support for console (still a work in progress)
  25. +Fixed issues when toggling the bar (graphical and control):
  26.  - Able to open menu when toggled
  27.  - Toggling menu while open causes then console to softlock
  28.  - Toggling menu causes PED to display weird
  29.  - Toggling menu is now more consistant
  30. +Added storage percent monitor (bottom-left)
  31.  
  32. Version 1.3
  33.  
  34. +Fixed minor display issue with PED
  35. +Fixed bug where console background never reset back to black
  36. +Fixed issue with fs.getCapacity()
  37.  
  38. --]]
  39.  
  40. local w,h = term.getSize()
  41. local clr,cp,st,sb = term.clear, term.setCursorPos, term.setTextColor, term.setBackgroundColor
  42.  
  43. local function minver(version) -- Thanks JackMacWindows#9776 <<- Discord
  44.     local res
  45.     if _CC_VERSION then res = version <= _CC_VERSION
  46.     elseif not _HOST then res = version <= os.version:gsub("CraftOS ", "")
  47.     else res = version <= _HOST:match("ComputerCraft ([0-9%.]+)") end
  48.     assert(res, "This program requires ComputerCraft " .. version .. " or later.")
  49. end
  50. minver("1.85.0")
  51. if not term.isColor() then print("Advanced Computer Required.") return end
  52.  
  53. local desktopColors = {
  54.     bar = colors.cyan,
  55.     home = colors.white,
  56.     home_background = colors.white,
  57.     numbers = colors.white,
  58.     selectedWindow = colors.lightGray,
  59. }
  60. local timecolors = {
  61.     {color = colors.black,time = 0},
  62.     {color = colors.lightGray, time = 2},
  63.     {color = colors.orange, time = 6},
  64.     {color = colors.yellow, time = 12},
  65.     {color = colors.orange, time = 16},
  66.     {color = colors.lightGray, time = 20},
  67. }
  68.  
  69. local desktop = term.current()
  70. local console = window.create(desktop, 1,1,w,h-1)
  71.  
  72. local pr, pe
  73. local newEvent = "PED Loaded."
  74.  
  75. local draw, inmenu, u, disabled = true, false, true, false
  76.  
  77. local function togglebar()
  78.     if inmenu then return false end
  79.     if draw then
  80.         console.reposition(1,1,w,h)
  81.     else
  82.         console.reposition(1,1,w,h-1)
  83.     end
  84.     console.redraw() console.restoreCursor()
  85.     draw = not draw u = true
  86. end
  87.  
  88. local function scanfile(loc)
  89.     local loc = loc or nil
  90.     local function getTotal(tot,str)
  91.         local t = {}
  92.         local i = 0
  93.         while true do
  94.           i = string.find(tot, str, i+1)
  95.           if i == nil then break end
  96.           table.insert(t, i)
  97.         end
  98.         return #t
  99.     end
  100.     if type(loc) ~= "string" then
  101.         st(colors.white) sb(colors.black)
  102.         write("File location: ") loc = read()
  103.     end
  104.     if fs.exists(loc) then
  105.         if fs.isDir(loc) then printError("Cannot scan a directory.") return end
  106.         local f = fs.open(loc, "r")
  107.         if not f then printError("Failed to open "..loc.."\46") return end
  108.         local r = f.readAll() f.close()
  109.         local list = {
  110.             {"bit", colors.white},
  111.             {"colors", colors.white},
  112.             {"commands", colors.purple},
  113.             {"coroutine", colors.white},
  114.             {"disk", colors.white},
  115.             {"fs", colors.red},
  116.             {"gps", colors.white},
  117.             {"http", colors.red},
  118.             {"keys", colors.white},
  119.             {"math", colors.white},
  120.             {"multishell", colors.orange},
  121.             {"os", colors.yellow},
  122.             {"paintutils", colors.white},
  123.             {"parallel", colors.white},
  124.             {"peripheral", colors.white},
  125.             {"rednet", colors.orange},
  126.             {"redstone", colors.white},
  127.             {"settings", colors.red},
  128.             {"shell", colors.white},
  129.             {"string", colors.white},
  130.             {"table", colors.white},
  131.             {"term", colors.white},
  132.             {"textutils", colors.white},
  133.             {"turtle", colors.lime},
  134.             {"vector", colors.white},
  135.             {"window", colors.yellow},
  136.         }
  137.         local terms = {
  138.             "virus", -- do not judge me lol. just threw this in here because... yes.
  139.             "pastebin"
  140.         }
  141.         for i=1, #list do
  142.             local tot = getTotal(r, list[i][1].."%.")
  143.             if tot ~= 0 then
  144.                 st(list[i][2]) write(list[i][1]) st(colors.white) print(": "..tot) sleep(0.1)
  145.             end
  146.         end
  147.         for i=1, #terms do
  148.             local tot = getTotal(r, terms[i])
  149.             if tot ~= 0 then
  150.                 st(colors.red) write("found mention of ")
  151.                 st(colors.white) write(terms[i]) write(": "..tot) st(colors.red) print("!")sleep(0.1)
  152.             end
  153.         end
  154.     else
  155.         printError("File does not exists.")
  156.     end
  157. end
  158.  
  159. local function runConsole() -- Run Console (PID 1)
  160.     pr,pe = pcall(function()
  161.         local running, hist, histT = true, {}, 0
  162.         while running do
  163.            
  164.             local function run()
  165.                 local command = ""
  166.                 local cmdlist = {
  167.                     exit = function() running = false end,
  168.                     togglebar = togglebar,
  169.                     scanfile = function(a) scanfile(a) end,
  170.                 }
  171.                 local px, py, u
  172.                 while running do
  173.                     st(colors.lightBlue) sb(colors.black)
  174.                     if shell.dir() == "" then write("/") else write(shell.dir()) end
  175.                     st(colors.yellow) write(" > ")
  176.                     st(colors.white)
  177.                     command, aCommand, histT = "", {}, 0
  178.                     px,py = term.getCursorPos() term.setCursorBlink(true)
  179.                     while true do
  180.                         if u then cp(px,py) write(command.." ") cp(px+string.len(command),py) u = false end
  181.                         a,b = os.pullEvent()
  182.                         if not inmenu or disabled then
  183.                             if a == "char" then
  184.                                 command = command..b u = true
  185.                             elseif a == "key" then
  186.                                 if b == keys.enter then
  187.                                     break
  188.                                 elseif b == keys.backspace then
  189.                                     command = string.sub(command, 1,string.len(command)-1) u = true
  190.                                 elseif b == keys.up then
  191.                                     histT = math.min(histT+1, #hist)
  192.                                     local x,y = term.getCursorPos()
  193.                                     cp(x-string.len(command)-3, y)
  194.                                     if histT >= 1 then
  195.                                         st(colors.gray) write(string.char(histT+96)) st(colors.yellow) write("> ") st(colors.lightGray)
  196.                                     else
  197.                                         st(colors.yellow) write(" > ") st(colors.white)
  198.                                     end
  199.                                     for i=1, string.len(command) do write(" ") end
  200.                                    
  201.                                     command = hist[histT] or command u = true
  202.                                 elseif b == keys.down then
  203.                                     histT = math.max(histT-1, 0)
  204.                                     local x,y = term.getCursorPos()
  205.                                     cp(x-string.len(command)-3, y)
  206.                                     if histT >= 1 then
  207.                                         st(colors.gray) write(string.char(histT+96)) st(colors.yellow) write("> ") st(colors.lightGray)
  208.                                     else
  209.                                         st(colors.yellow) write(" > ") st(colors.white)
  210.                                     end
  211.                                     for i=1, string.len(command) do write(" ") end
  212.                                     command = hist[histT] or "" u = true
  213.                                 end
  214.                             elseif a == "paste" then
  215.                                 command = command..b u = true
  216.                             end
  217.                         end
  218.                     end
  219.                     local t = ""
  220.                     for i=1, string.len(command) do
  221.                         if string.sub(command, i,i) == " " and aCommand[#aCommand] ~= " " then
  222.                             aCommand[#aCommand+1] = t
  223.                             t = ""
  224.                         else
  225.                             t = t..string.sub(command, i,i)
  226.                         end
  227.                     end
  228.                     aCommand[#aCommand+1] = t
  229.                     if string.gsub(command, " ", "") ~= "" and hist[#hist] ~= command then
  230.                         if #hist >= 26 then
  231.                             hist[1] = command
  232.                         else
  233.                             hist[#hist+1] = command
  234.                         end
  235.                     end
  236.                     print("")
  237.                     if cmdlist[string.lower(aCommand[1])] then cmdlist[string.lower(aCommand[1])](aCommand[2]) else shell.run(command) end
  238.                 end
  239.             end
  240.            
  241.             term.redirect(console)
  242.             run()
  243.         end
  244.     end)
  245. end
  246.  
  247. local function drawTime()
  248.     if draw then
  249.         -- Display the Clock --
  250.         term.setCursorBlink(false)
  251.         st(colors.white) sb(desktopColors.bar) cp(w-string.len( " "..textutils.formatTime( os.time() ).."." ), h)
  252.         write( " "..textutils.formatTime( os.time() ) )
  253.         for i=1, #timecolors do
  254.             if timecolors[i].time < os.time() then st(timecolors[i].color) end
  255.         end
  256.         write("\7")
  257.         if fs.getCapacity then
  258.             local perc = math.ceil(((fs.getCapacity("/")-fs.getFreeSpace("/"))/fs.getCapacity("/"))*100)
  259.             if perc > 80 then st(colors.red) else st(colors.white) end
  260.             cp(2,h) write(perc.."%")
  261.         end
  262.         -- Display the Clock --
  263.     end
  264. end
  265.  
  266. local function runTime() -- Run the clock (PID 2)
  267.     pr,pe = pcall(function()
  268.         while true do
  269.            
  270.             -- Redirect and Save info --
  271.             term.redirect(desktop)
  272.             local t,t2,t3 = term.getCursorBlink(), term.getTextColor(), term.getBackgroundColor()
  273.             local t4,t5 = term.getCursorPos()
  274.             -- Redirect and Save info --
  275.            
  276.             drawTime()
  277.            
  278.             -- Redirect and Load info --
  279.             term.setCursorBlink(t) st(t2) sb(t3) cp(t4,t5)
  280.             term.redirect(console)
  281.             sleep(1)
  282.             -- Redirect and Load info --
  283.         end
  284.     end)
  285. end
  286.  
  287. local function runBar() -- Run Bar (PID 3)
  288.     pr, pe = pcall(function()
  289.        
  290.         local run = true
  291.         local mOptions = {
  292.             { -- Bottom
  293.                 name = "Reboot",
  294.                 namecolor = colors.black,
  295.                 icon = "\177",
  296.                 iconcolor = colors.yellow,
  297.                 process = function() os.reboot() end
  298.             },
  299.             {
  300.                 name = "Shutdown",
  301.                 namecolor = colors.black,
  302.                 icon = "\177",
  303.                 iconcolor = colors.red,
  304.                 process = function() os.shutdown() end
  305.             },
  306.             { -- Top
  307.                 name = "CraftOS",
  308.                 namecolor = colors.black,
  309.                 icon = "\169",
  310.                 iconcolor = colors.orange,
  311.                 process = function() run = false end
  312.             },
  313.         }
  314.         local mX = math.max(6,math.floor(w/4))
  315.        
  316.         local function display()
  317.             if draw then
  318.                 term.redirect(desktop)
  319.                 paintutils.drawLine(1,h,w,h,desktopColors.bar) drawTime()
  320.                 if inmenu then
  321.                     cp(1,h) st(desktopColors.home) write("\6")
  322.                     --st(colors.black)
  323.                     paintutils.drawLine(1,h-(#mOptions+1),mX,h-(#mOptions+1),desktopColors.bar)
  324.                     paintutils.drawFilledBox(1,h-(#mOptions),mX,h-1, desktopColors.home_background)
  325.                     cp(1,h-(#mOptions))
  326.                     for i=1, #mOptions do
  327.                         st(mOptions[#mOptions-i+1].iconcolor)
  328.                         write(mOptions[#mOptions-i+1].icon)
  329.                         st(mOptions[#mOptions-i+1].namecolor)
  330.                         print(mOptions[#mOptions-i+1].name)
  331.                     end
  332.                 else
  333.                     cp(1,h) st(desktopColors.home) write("\7")
  334.                     console.redraw()
  335.                     term.redirect(console) console.restoreCursor()
  336.                 end
  337.             end
  338.         end
  339.        
  340.         while run do
  341.             if u then display() u = false end
  342.             a,b,x,y = os.pullEvent() newEvent = a
  343.             if a == "mouse_click" and draw then
  344.                 if inmenu then
  345.                     if x == 1 and y == h then
  346.                         inmenu = false u = true
  347.                     elseif x <= mX and y >= h-#mOptions and y ~= h then
  348.                         --error(h-y)
  349.                         if not mOptions[h-y] then error("Failed to find proccess "..h-y..".") end
  350.                         mOptions[h-y].process()
  351.                     else
  352.                         inmenu = false u = true
  353.                     end
  354.                 else
  355.                     if x == 1 and y == h then
  356.                         inmenu = true u = true
  357.                     end
  358.                 end
  359.             elseif a == "key" then
  360.                 if inmenu then inmenu = false u = true end
  361.                 if b == keys.leftCtrl or b == keys.rightCtrl and not inmenu then
  362.                     local a,b = os.pullEvent("key")
  363.                     if b == keys.f then
  364.                         togglebar()
  365.                     end
  366.                 end
  367.             elseif a == "term_resize" then
  368.                 if inmenu then inmenu = false u = true end
  369.                 term.redirect(desktop)
  370.                 w,h = term.getSize() -- desktop width and height
  371.                 if not draw then
  372.                     console.reposition(1,1,w,h)
  373.                 else
  374.                     console.reposition(1,1,w,h-1)
  375.                 end
  376.                 mX = math.max(6,math.floor(w/4)) -- scale the menu to a reasonable size
  377.                 u = true
  378.                 term.redirect(console) console.redraw()
  379.             end
  380.         end
  381.     end)
  382. end
  383.  
  384. local function runPED() -- Run PullEventDisplay (PID 4)
  385.     pr, pe = pcall(function()
  386.         local oldEvent = ""
  387.         while true do
  388.             if draw then -- main one to avoid drawing it total
  389.                 a,b,x,y = os.pullEvent()
  390.                 if a ~= "timer" and a ~= "key_up" and a ~= "mouse_up" then
  391.                     if draw then -- second one to avoid drawing on toggle
  392.                         term.redirect(desktop) cp(6,h) st(colors.gray) sb(desktopColors.bar)
  393.                         if a == "char" then
  394.                             write(b.."           ")
  395.                         elseif a == "key" then
  396.                             if b == keys.right then
  397.                                 write("\16")
  398.                             elseif b == keys.left then
  399.                                 write("\17")
  400.                             elseif b == keys.up then
  401.                                 write("\30")
  402.                             elseif b == keys.down then
  403.                                 write("\31")
  404.                             elseif b == keys.enter then
  405.                                 write("\26")
  406.                             elseif b == keys.backspace then
  407.                                 write("\171")
  408.                             else
  409.                                 write(keys.getName(b))
  410.                             end
  411.                             write("          ")
  412.                         elseif a == "mouse_click" or a == "mouse_drag" then
  413.                             write(x..","..y.."       ")
  414.                         else
  415.                             write(a.."               ")
  416.                         end
  417.                         term.redirect(console) console.restoreCursor()
  418.                     end
  419.                 end
  420.             else
  421.                 sleep(0.1)
  422.             end
  423.         end
  424.     end)
  425. end
  426.  
  427. local PT = {"Console", "Time", "Bar", "PED"}
  428. local PID = parallel.waitForAny(runConsole, runTime, runBar, runPED)
  429. term.redirect(desktop) term.setCursorBlink(false)
  430.  
  431. if not pr then -- Crashed
  432.     st(colors.orange) sb(colors.black) clr() cp(1,1)
  433.     print("PCM crashed!")
  434.     write("PID: ") st(colors.white)
  435.     print(PID.." ("..PT[PID]..")") st(colors.orange)
  436.     write("Error: ") st(colors.white)
  437.     print(pe) st(colors.gray)
  438.     sleep(5) cp(1,h) write("Press and key to exit.") os.pullEvent()
  439. end
  440. st(colors.white) sb(colors.black) clr() cp(1,1)
Add Comment
Please, Sign In to add comment