2020-03-18 01:11:53 +11:00
|
|
|
local component = require "component"
|
|
|
|
local fs = require "fs"
|
|
|
|
local shell = require "shell"
|
2020-04-12 03:49:44 +10:00
|
|
|
local ed = require "ed"
|
2020-03-18 01:11:53 +11:00
|
|
|
local shutil = {}
|
2020-04-12 03:49:44 +10:00
|
|
|
shutil.ed = ed.interactive
|
|
|
|
shutil.vi = ed.visual
|
|
|
|
shutil.buffers = ed.ifunc.buffers
|
2020-03-18 01:11:53 +11:00
|
|
|
|
2020-03-18 01:57:05 +11: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
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.import(lib) -- string -- boolean -- Imports the functions from library *lib* into the shell environment.
|
2020-03-18 01:11:53 +11: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
|
2020-03-18 11:55:12 +11:00
|
|
|
os.setenv("INCLUDE",nE)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.unimport(lib) -- string -- boolean -- Removes the functions from *lib* from the shell environment.
|
2020-03-18 11:55:12 +11:00
|
|
|
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-18 01:11:53 +11:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.ls(...) -- string -- -- Prints contents of directories specified as *...*.
|
2020-03-18 01:11:53 +11: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
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.cat(...) -- string -- -- Outputs the contents of files specified in *...* to the standard output.
|
2020-03-18 01:11:53 +11:00
|
|
|
for _,fn in ipairs({...}) do
|
|
|
|
local f = io.open(fn,"rb")
|
|
|
|
io.write(f:read("*a"))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.ps() -- Prints the processes running on the system.
|
2020-03-18 01:11:53 +11: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
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.df() -- Prints free disk space.
|
2020-03-18 01:11:53 +11: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
|
|
|
|
local st, su = fs.spaceTotal(v), fs.spaceUsed(v)
|
|
|
|
print(string.format(fstr,v,wrapUnits(st),wrapUnits(su)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.mount(addr,path) -- string string -- boolean string -- Mounts filesystem component with address *addr* to *path* in the filesystem.
|
2020-03-18 01:11:53 +11:00
|
|
|
if not addr then
|
|
|
|
local mt = fs.mounts()
|
|
|
|
for k,v in pairs(mt) do
|
2020-03-26 17:41:25 +11:00
|
|
|
v = "/"..table.concat(fs.segments(v),"/")
|
2020-03-18 01:11:53 +11: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
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function shutil.free() -- Displays used and free memory.
|
2020-03-18 01:57:05 +11:00
|
|
|
print("Total Used Free")
|
|
|
|
print(string.format("%5s %5s %5s",wrapUnits(computer.totalMemory()),wrapUnits(computer.totalMemory()-computer.freeMemory()),wrapUnits(computer.freeMemory())))
|
|
|
|
end
|
|
|
|
|
2020-03-18 01:11:53 +11:00
|
|
|
shutil.cd = os.chdir
|
|
|
|
shutil.mkdir = fs.makeDirectory
|
|
|
|
shutil.cp = fs.copy
|
2020-03-18 11:55:12 +11:00
|
|
|
shutil.rm = fs.remove
|
2020-03-18 01:11:53 +11:00
|
|
|
|
2020-06-04 06:06:58 +10:00
|
|
|
local function find_dir(t, dir)
|
|
|
|
local f = fs.list(dir)
|
|
|
|
for i=1, #f do
|
|
|
|
if (f[i]:sub(#f[i]) == "/") then
|
|
|
|
find_dir(t, dir..f[i])
|
|
|
|
end
|
|
|
|
t[#t+1] = dir..f[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function shutil.find(...)
|
|
|
|
local paths = {...}
|
|
|
|
if not paths[1] then paths = {"."} end
|
|
|
|
for i=1, #paths do
|
|
|
|
if (paths[i]:sub(#paths[i]) ~= "/") then
|
|
|
|
paths[i] = paths[i] .. "/"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local t = {}
|
|
|
|
for i=1, #paths do
|
|
|
|
if fs.isDirectory(paths[i]) then
|
|
|
|
find_dir(t, paths[i])
|
|
|
|
end
|
|
|
|
t[#t+1] = paths[i]
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
function shutil.filter(t, func)
|
|
|
|
local t2 = {}
|
|
|
|
for i=1, #t do
|
|
|
|
if (func(t[i])) then
|
|
|
|
t2[#t2+1] = t[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return t2
|
|
|
|
end
|
|
|
|
|
2020-03-18 01:11:53 +11:00
|
|
|
return shutil
|