1
1
mirror of https://git.shadowkat.net/izaya/OC-PsychOS2.git synced 2024-11-22 12:04:20 -05:00
OC-PsychOS2/lib/shutil.lua

157 lines
4.1 KiB
Lua
Raw Normal View History

2020-03-17 10:11:53 -04:00
local component = require "component"
local fs = require "fs"
local shell = require "shell"
local ed = require "ed"
local doc = require "doc"
2020-03-17 10:11:53 -04:00
local shutil = {}
shutil.ed = ed.interactive
shutil.vi = ed.visual
shutil.buffers = ed.ifunc.buffers
2020-03-17 10:11:53 -04:00
2020-03-17 10:57:05 -04:00
local function wrapUnits(n)
local scale = {"K","M","G","T","P"}
local count = 0
while n >= 1024 do
count = count + 1
if not scale[count] then return "inf" end
n = n / 1024
end
return tostring(math.floor(n))..(scale[count] or "")
end
function shutil.import(lib) -- string -- boolean -- Imports the functions from library *lib* into the shell environment.
2020-03-17 10:11:53 -04:00
local cE = os.getenv("INCLUDE") or shell.include
local nE = {}
for k,v in pairs(cE) do
nE[#nE+1] = v
end
require(lib)
nE[#nE+1] = lib
os.setenv("INCLUDE",nE)
return true
end
function shutil.unimport(lib) -- string -- boolean -- Removes the functions from *lib* from the shell environment.
local cE = os.getenv("INCLUDE") or shell.include
local nE = {}
for k,v in pairs(cE) do
if v ~= lib then
nE[#nE+1] = v
end
end
os.setenv("INCLUDE",nE)
2020-03-17 10:11:53 -04:00
return true
end
function shutil.ls(...) -- string -- -- Prints contents of directories specified as *...*.
2020-03-17 10:11:53 -04:00
local tA = {...}
if not tA[1] then tA[1] = "." end
for _,d in ipairs(tA) do
if #tA > 1 then
print(d..":")
end
for _,f in ipairs(fs.list(d)) do
print(" "..f)
end
end
end
function shutil.cat(...) -- string -- -- Outputs the contents of files specified in *...* to the standard output.
2020-03-17 10:11:53 -04:00
for _,fn in ipairs({...}) do
local f = io.open(fn,"rb")
io.write(f:read("*a"))
f:close()
end
end
function shutil.ps() -- Prints the processes running on the system.
2020-03-17 10:11:53 -04:00
print("PID# Parent | Name")
for k,v in pairs(os.tasks()) do
local t = os.taskInfo(v)
print(string.format("%4d %4d | %s",v,t.parent,t.name))
end
end
function shutil.df() -- Prints free disk space.
2020-03-17 10:11:53 -04:00
local mt = fs.mounts()
local ml = 0
for k,v in pairs(mt) do
if v:len() > ml then
ml = v:len()
end
end
local fstr = "%-"..tostring(ml).."s %5s %5s"
print("fs"..(" "):rep(ml-2).." size used")
for k,v in pairs(mt) do
2023-07-30 01:04:41 -04:00
local st, su = fs.spaceTotal("/"..v), fs.spaceUsed("/"..v)
2020-03-17 10:11:53 -04:00
print(string.format(fstr,v,wrapUnits(st),wrapUnits(su)))
end
end
function shutil.mount(addr,path) -- string string -- boolean string -- Mounts filesystem component with address *addr* to *path* in the filesystem.
2020-03-17 10:11:53 -04:00
if not addr then
local mt = fs.mounts()
for k,v in pairs(mt) do
v = "/"..table.concat(fs.segments(v),"/")
2020-03-17 10:11:53 -04:00
print(tostring(fs.address(v)).." on "..tostring(v).." type "..fs.type(v))
end
else
local fscomp = component.list("filesystem")
if not fscomp[addr] then
for k,v in pairs(fscomp) do
if k:find(addr) then
print(tostring(addr).." not found, assuming you meant "..k)
addr = k
break
end
end
end
local proxy = component.proxy(addr)
if not proxy then
return false, "no such filesystem component"
end
print(fs.mount(path,proxy))
end
end
function shutil.free() -- Displays used and free memory.
2020-03-17 10:57:05 -04:00
print("Total Used Free")
print(string.format("%5s %5s %5s",wrapUnits(computer.totalMemory()),wrapUnits(computer.totalMemory()-computer.freeMemory()),wrapUnits(computer.freeMemory())))
end
2023-06-06 10:16:26 -04:00
function shutil.which(name)
local fpath
for _,dir in ipairs(os.getenv("PATH")) do
fpath = fpath or fs.exists(string.format("%s/%s.lua",dir,name)) and string.format("%s/%s.lua",dir,name) or fs.exists(string.format("%s/%s",dir,name)) and string.format("%s/%s",dir,name)
end
return fpath
end
2020-03-17 10:11:53 -04:00
shutil.cd = os.chdir
shutil.mkdir = fs.makeDirectory
shutil.cp = fs.copy
shutil.rm = fs.remove
2020-03-17 10:11:53 -04:00
2023-06-06 10:16:26 -04:00
return setmetatable({}, {__index = function(t,k)
if shutil[k] then
return shutil[k]
end
local path = shutil.which(k)
if path then
local fn, e = loadfile(path)
if not fn then error(string.format("\n - %s",e)) end
2023-07-04 04:25:11 -04:00
return function(...)
local tA = {...}
local pid = os.spawn(function() return fn(table.unpack(tA)) end,path)
2023-06-06 10:16:26 -04:00
local ret = {require("event").pull("process_finished",pid)}
if not ret[3] then
error(string.format("\n - %s",ret[4]))
end
for i = 1, 3 do
table.remove(ret,1)
end
return table.unpack(ret)
end
end
end})