Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- PCM (PC Manager)
- Just Does Games
- Usages:
- Ctrl-f or 'toggleconsole' = toggle bar
- 'scanfile' = scan file for key api words
- Version 1.0
- release
- Version 1.1
- +Added PED
- +Added version and color requirement
- +Added scanfile command
- Version 1.2
- +Patched pasting to console
- +Added support for multi-arg for scanfile command and redesigned program
- +Added history support for console (still a work in progress)
- +Fixed issues when toggling the bar (graphical and control):
- - Able to open menu when toggled
- - Toggling menu while open causes then console to softlock
- - Toggling menu causes PED to display weird
- - Toggling menu is now more consistant
- +Added storage percent monitor (bottom-left)
- Version 1.3
- +Fixed minor display issue with PED
- +Fixed bug where console background never reset back to black
- +Fixed issue with fs.getCapacity()
- --]]
- local w,h = term.getSize()
- local clr,cp,st,sb = term.clear, term.setCursorPos, term.setTextColor, term.setBackgroundColor
- local function minver(version) -- Thanks JackMacWindows#9776 <<- Discord
- local res
- if _CC_VERSION then res = version <= _CC_VERSION
- elseif not _HOST then res = version <= os.version:gsub("CraftOS ", "")
- else res = version <= _HOST:match("ComputerCraft ([0-9%.]+)") end
- assert(res, "This program requires ComputerCraft " .. version .. " or later.")
- end
- minver("1.85.0")
- if not term.isColor() then print("Advanced Computer Required.") return end
- local desktopColors = {
- bar = colors.cyan,
- home = colors.white,
- home_background = colors.white,
- numbers = colors.white,
- selectedWindow = colors.lightGray,
- }
- local timecolors = {
- {color = colors.black,time = 0},
- {color = colors.lightGray, time = 2},
- {color = colors.orange, time = 6},
- {color = colors.yellow, time = 12},
- {color = colors.orange, time = 16},
- {color = colors.lightGray, time = 20},
- }
- local desktop = term.current()
- local console = window.create(desktop, 1,1,w,h-1)
- local pr, pe
- local newEvent = "PED Loaded."
- local draw, inmenu, u, disabled = true, false, true, false
- local function togglebar()
- if inmenu then return false end
- if draw then
- console.reposition(1,1,w,h)
- else
- console.reposition(1,1,w,h-1)
- end
- console.redraw() console.restoreCursor()
- draw = not draw u = true
- end
- local function scanfile(loc)
- local loc = loc or nil
- local function getTotal(tot,str)
- local t = {}
- local i = 0
- while true do
- i = string.find(tot, str, i+1)
- if i == nil then break end
- table.insert(t, i)
- end
- return #t
- end
- if type(loc) ~= "string" then
- st(colors.white) sb(colors.black)
- write("File location: ") loc = read()
- end
- if fs.exists(loc) then
- if fs.isDir(loc) then printError("Cannot scan a directory.") return end
- local f = fs.open(loc, "r")
- if not f then printError("Failed to open "..loc.."\46") return end
- local r = f.readAll() f.close()
- local list = {
- {"bit", colors.white},
- {"colors", colors.white},
- {"commands", colors.purple},
- {"coroutine", colors.white},
- {"disk", colors.white},
- {"fs", colors.red},
- {"gps", colors.white},
- {"http", colors.red},
- {"keys", colors.white},
- {"math", colors.white},
- {"multishell", colors.orange},
- {"os", colors.yellow},
- {"paintutils", colors.white},
- {"parallel", colors.white},
- {"peripheral", colors.white},
- {"rednet", colors.orange},
- {"redstone", colors.white},
- {"settings", colors.red},
- {"shell", colors.white},
- {"string", colors.white},
- {"table", colors.white},
- {"term", colors.white},
- {"textutils", colors.white},
- {"turtle", colors.lime},
- {"vector", colors.white},
- {"window", colors.yellow},
- }
- local terms = {
- "virus", -- do not judge me lol. just threw this in here because... yes.
- "pastebin"
- }
- for i=1, #list do
- local tot = getTotal(r, list[i][1].."%.")
- if tot ~= 0 then
- st(list[i][2]) write(list[i][1]) st(colors.white) print(": "..tot) sleep(0.1)
- end
- end
- for i=1, #terms do
- local tot = getTotal(r, terms[i])
- if tot ~= 0 then
- st(colors.red) write("found mention of ")
- st(colors.white) write(terms[i]) write(": "..tot) st(colors.red) print("!")sleep(0.1)
- end
- end
- else
- printError("File does not exists.")
- end
- end
- local function runConsole() -- Run Console (PID 1)
- pr,pe = pcall(function()
- local running, hist, histT = true, {}, 0
- while running do
- local function run()
- local command = ""
- local cmdlist = {
- exit = function() running = false end,
- togglebar = togglebar,
- scanfile = function(a) scanfile(a) end,
- }
- local px, py, u
- while running do
- st(colors.lightBlue) sb(colors.black)
- if shell.dir() == "" then write("/") else write(shell.dir()) end
- st(colors.yellow) write(" > ")
- st(colors.white)
- command, aCommand, histT = "", {}, 0
- px,py = term.getCursorPos() term.setCursorBlink(true)
- while true do
- if u then cp(px,py) write(command.." ") cp(px+string.len(command),py) u = false end
- a,b = os.pullEvent()
- if not inmenu or disabled then
- if a == "char" then
- command = command..b u = true
- elseif a == "key" then
- if b == keys.enter then
- break
- elseif b == keys.backspace then
- command = string.sub(command, 1,string.len(command)-1) u = true
- elseif b == keys.up then
- histT = math.min(histT+1, #hist)
- local x,y = term.getCursorPos()
- cp(x-string.len(command)-3, y)
- if histT >= 1 then
- st(colors.gray) write(string.char(histT+96)) st(colors.yellow) write("> ") st(colors.lightGray)
- else
- st(colors.yellow) write(" > ") st(colors.white)
- end
- for i=1, string.len(command) do write(" ") end
- command = hist[histT] or command u = true
- elseif b == keys.down then
- histT = math.max(histT-1, 0)
- local x,y = term.getCursorPos()
- cp(x-string.len(command)-3, y)
- if histT >= 1 then
- st(colors.gray) write(string.char(histT+96)) st(colors.yellow) write("> ") st(colors.lightGray)
- else
- st(colors.yellow) write(" > ") st(colors.white)
- end
- for i=1, string.len(command) do write(" ") end
- command = hist[histT] or "" u = true
- end
- elseif a == "paste" then
- command = command..b u = true
- end
- end
- end
- local t = ""
- for i=1, string.len(command) do
- if string.sub(command, i,i) == " " and aCommand[#aCommand] ~= " " then
- aCommand[#aCommand+1] = t
- t = ""
- else
- t = t..string.sub(command, i,i)
- end
- end
- aCommand[#aCommand+1] = t
- if string.gsub(command, " ", "") ~= "" and hist[#hist] ~= command then
- if #hist >= 26 then
- hist[1] = command
- else
- hist[#hist+1] = command
- end
- end
- print("")
- if cmdlist[string.lower(aCommand[1])] then cmdlist[string.lower(aCommand[1])](aCommand[2]) else shell.run(command) end
- end
- end
- term.redirect(console)
- run()
- end
- end)
- end
- local function drawTime()
- if draw then
- -- Display the Clock --
- term.setCursorBlink(false)
- st(colors.white) sb(desktopColors.bar) cp(w-string.len( " "..textutils.formatTime( os.time() ).."." ), h)
- write( " "..textutils.formatTime( os.time() ) )
- for i=1, #timecolors do
- if timecolors[i].time < os.time() then st(timecolors[i].color) end
- end
- write("\7")
- if fs.getCapacity then
- local perc = math.ceil(((fs.getCapacity("/")-fs.getFreeSpace("/"))/fs.getCapacity("/"))*100)
- if perc > 80 then st(colors.red) else st(colors.white) end
- cp(2,h) write(perc.."%")
- end
- -- Display the Clock --
- end
- end
- local function runTime() -- Run the clock (PID 2)
- pr,pe = pcall(function()
- while true do
- -- Redirect and Save info --
- term.redirect(desktop)
- local t,t2,t3 = term.getCursorBlink(), term.getTextColor(), term.getBackgroundColor()
- local t4,t5 = term.getCursorPos()
- -- Redirect and Save info --
- drawTime()
- -- Redirect and Load info --
- term.setCursorBlink(t) st(t2) sb(t3) cp(t4,t5)
- term.redirect(console)
- sleep(1)
- -- Redirect and Load info --
- end
- end)
- end
- local function runBar() -- Run Bar (PID 3)
- pr, pe = pcall(function()
- local run = true
- local mOptions = {
- { -- Bottom
- name = "Reboot",
- namecolor = colors.black,
- icon = "\177",
- iconcolor = colors.yellow,
- process = function() os.reboot() end
- },
- {
- name = "Shutdown",
- namecolor = colors.black,
- icon = "\177",
- iconcolor = colors.red,
- process = function() os.shutdown() end
- },
- { -- Top
- name = "CraftOS",
- namecolor = colors.black,
- icon = "\169",
- iconcolor = colors.orange,
- process = function() run = false end
- },
- }
- local mX = math.max(6,math.floor(w/4))
- local function display()
- if draw then
- term.redirect(desktop)
- paintutils.drawLine(1,h,w,h,desktopColors.bar) drawTime()
- if inmenu then
- cp(1,h) st(desktopColors.home) write("\6")
- --st(colors.black)
- paintutils.drawLine(1,h-(#mOptions+1),mX,h-(#mOptions+1),desktopColors.bar)
- paintutils.drawFilledBox(1,h-(#mOptions),mX,h-1, desktopColors.home_background)
- cp(1,h-(#mOptions))
- for i=1, #mOptions do
- st(mOptions[#mOptions-i+1].iconcolor)
- write(mOptions[#mOptions-i+1].icon)
- st(mOptions[#mOptions-i+1].namecolor)
- print(mOptions[#mOptions-i+1].name)
- end
- else
- cp(1,h) st(desktopColors.home) write("\7")
- console.redraw()
- term.redirect(console) console.restoreCursor()
- end
- end
- end
- while run do
- if u then display() u = false end
- a,b,x,y = os.pullEvent() newEvent = a
- if a == "mouse_click" and draw then
- if inmenu then
- if x == 1 and y == h then
- inmenu = false u = true
- elseif x <= mX and y >= h-#mOptions and y ~= h then
- --error(h-y)
- if not mOptions[h-y] then error("Failed to find proccess "..h-y..".") end
- mOptions[h-y].process()
- else
- inmenu = false u = true
- end
- else
- if x == 1 and y == h then
- inmenu = true u = true
- end
- end
- elseif a == "key" then
- if inmenu then inmenu = false u = true end
- if b == keys.leftCtrl or b == keys.rightCtrl and not inmenu then
- local a,b = os.pullEvent("key")
- if b == keys.f then
- togglebar()
- end
- end
- elseif a == "term_resize" then
- if inmenu then inmenu = false u = true end
- term.redirect(desktop)
- w,h = term.getSize() -- desktop width and height
- if not draw then
- console.reposition(1,1,w,h)
- else
- console.reposition(1,1,w,h-1)
- end
- mX = math.max(6,math.floor(w/4)) -- scale the menu to a reasonable size
- u = true
- term.redirect(console) console.redraw()
- end
- end
- end)
- end
- local function runPED() -- Run PullEventDisplay (PID 4)
- pr, pe = pcall(function()
- local oldEvent = ""
- while true do
- if draw then -- main one to avoid drawing it total
- a,b,x,y = os.pullEvent()
- if a ~= "timer" and a ~= "key_up" and a ~= "mouse_up" then
- if draw then -- second one to avoid drawing on toggle
- term.redirect(desktop) cp(6,h) st(colors.gray) sb(desktopColors.bar)
- if a == "char" then
- write(b.." ")
- elseif a == "key" then
- if b == keys.right then
- write("\16")
- elseif b == keys.left then
- write("\17")
- elseif b == keys.up then
- write("\30")
- elseif b == keys.down then
- write("\31")
- elseif b == keys.enter then
- write("\26")
- elseif b == keys.backspace then
- write("\171")
- else
- write(keys.getName(b))
- end
- write(" ")
- elseif a == "mouse_click" or a == "mouse_drag" then
- write(x..","..y.." ")
- else
- write(a.." ")
- end
- term.redirect(console) console.restoreCursor()
- end
- end
- else
- sleep(0.1)
- end
- end
- end)
- end
- local PT = {"Console", "Time", "Bar", "PED"}
- local PID = parallel.waitForAny(runConsole, runTime, runBar, runPED)
- term.redirect(desktop) term.setCursorBlink(false)
- if not pr then -- Crashed
- st(colors.orange) sb(colors.black) clr() cp(1,1)
- print("PCM crashed!")
- write("PID: ") st(colors.white)
- print(PID.." ("..PT[PID]..")") st(colors.orange)
- write("Error: ") st(colors.white)
- print(pe) st(colors.gray)
- sleep(5) cp(1,h) write("Press and key to exit.") os.pullEvent()
- end
- st(colors.white) sb(colors.black) clr() cp(1,1)
Add Comment
Please, Sign In to add comment