added a cd() builtin to the shell, for changing working directory

This commit is contained in:
Izaya 2019-07-28 13:03:04 +10:00
parent ead102f131
commit 12df3de7df

View File

@ -3,6 +3,25 @@ local shenv = {}
function shenv.quit() function shenv.quit()
os.setenv("run",nil) os.setenv("run",nil)
end end
function shenv.cd(p)
if p:sub(1,1) == "/" then
if fs.list(p) then
os.setenv("PWD",p)
else
print("no such directory: "..p)
end
else
local np = {}
for k,v in pairs(fs.segments(os.getenv("PWD").."/"..p)) do
if v == ".." then
np[#np] = nil
else
np[#np+1] = v
end
end
os.setenv("PWD","/"..table.concat(np,"/"))
end
end
setmetatable(shenv,{__index=function(_,k) setmetatable(shenv,{__index=function(_,k)
if _G[k] then if _G[k] then
return _G[k] return _G[k]