1
1
mirror of https://git.shadowkat.net/izaya/OC-PsychOS2.git synced 2024-11-29 06:05:35 -05:00
OC-PsychOS2/lib/shell.lua

48 lines
1.1 KiB
Lua
Raw Normal View History

local serial = require "serialization"
2020-03-17 10:11:53 -04:00
local shell = {}
shell.include = {"shutil"}
local function shindex(self,k)
if rawget(self,k) then return rawget(self,k) end
for _,v in pairs(os.getenv("INCLUDE") or shell.include) do
if require(v)[k] then return require(v)[k] end
end
if package.loaded[k] then return package.loaded[k] end
return _G[k]
end
2020-03-17 23:03:23 -04:00
local function formatValue(v)
if type(v) == "table" then
local w, rs = pcall(serial.serialize,v,true)
2020-03-17 23:03:23 -04:00
if w then return rs end
end
return tostring(v)
end
2020-03-17 10:11:53 -04:00
function shell.interactive()
local shenv = setmetatable({}, {__index=shindex})
local run = true
function shenv.quit()
run = false
end
2020-03-17 10:11:53 -04:00
while run do
2020-03-25 23:41:06 -04:00
io.write(string.format("\27[32m%s:%s>\27[0m ",os.getenv("HOSTNAME") or "localhost",(os.getenv("PWD") or _VERSION)))
2020-03-17 10:11:53 -04:00
local input = io.read()
if input:sub(1,1) == "=" then
input = "return "..input:sub(2)
end
local f, r = load(input, "shell", "t", shenv)
if not f then
2020-03-25 23:41:06 -04:00
print("\27[31m"..r)
2020-03-17 10:11:53 -04:00
else
local rt = {pcall(f)}
local rs = table.remove(rt,1)
2020-03-25 23:41:06 -04:00
if not rs then io.write("\27[31m") end
2020-03-17 10:11:53 -04:00
for k,v in pairs(rt) do
2020-03-17 23:03:23 -04:00
print(formatValue(v))
2020-03-17 10:11:53 -04:00
end
end
end
end
return shell