added fs.isDir, renamed fs.getAttributes to fs.attributes, more stuff in Lua

This commit is contained in:
Alessandro Proto 2023-01-20 16:28:58 +01:00
parent 5de28f78d2
commit c6e3fa99c8
8 changed files with 55 additions and 22 deletions

View file

@ -3,13 +3,13 @@ local fs = require("fs")
local dir = args[1] local dir = args[1]
if #args == 0 then if not dir then
dir = shell.homePath dir = shell.homePath
end end
dir = fs.combine(shell.getDir(), dir) dir = shell.resolve(dir)
if not fs.exists(dir) or not fs.getAttributes(dir).isDirectory then if not fs.isDir(dir) then
error("No such directory: " .. dir, 0) error("No such directory: " .. dir, 0)
return false return false
end end

View file

@ -1 +1,17 @@
print("Hello, world!") local timer = require("timer")
local colors = require("colors")
local term = require("term")
local function slowPrint(text, delay)
for i = 1, #text do
local ch = text:sub(i, i)
write(ch)
timer.sleep(delay)
end
print()
end
local color = colors[math.random(1, #colors)]
term.setForeground(color)
slowPrint("Hello, World!", 50)

View file

@ -5,24 +5,17 @@ local colors = require("colors")
local dir = shell.getDir() local dir = shell.getDir()
if args[1] then if args[1] then
dir = fs.combine(shell.getDir(), args[1]) dir = shell.resolve(args[1])
end end
if not fs.exists(dir) then if not fs.isDir(dir) then
error("No such directory: " .. dir, 0)
return false
end
local attr = fs.getAttributes(dir)
if not attr.isDirectory then
error("No such directory: " .. dir, 0) error("No such directory: " .. dir, 0)
return false return false
end end
local files = fs.list(dir) local files = fs.list(dir)
for k,v in ipairs(files) do for k,v in ipairs(files) do
local attr = fs.getAttributes(fs.combine(dir, v)) if fs.isDir(fs.combine(dir, v)) then
if attr.isDirectory then
term.setForeground(colors.lightBlue) term.setForeground(colors.lightBlue)
print(v .. "/") print(v .. "/")
else else

View file

@ -6,7 +6,7 @@ if #args == 0 then
return return
end end
local dir = fs.combine(shell.getDir(), args[1]) local dir = shell.resolve(args[1])
if fs.exists(dir) then if fs.exists(dir) then
error("Path already exists", 0) error("Path already exists", 0)
end end

View file

@ -6,5 +6,5 @@ if #args == 0 then
return return
end end
local file = fs.combine(shell.getDir(), args[1]) local file = shell.resolve(args[1])
fs.delete(file, true) fs.delete(file, true)

View file

@ -6,7 +6,7 @@ local fs = require("fs")
local exit = false local exit = false
local shell = {} local shell = {}
shell.path = "./?.lua;./?;/bin/?.lua" shell.path = "./?;./?.lua;/bin/?.lua"
shell.homePath = "/home" shell.homePath = "/home"
local currentDir = shell.homePath local currentDir = shell.homePath
@ -55,12 +55,20 @@ end
function shell.resolve(path) function shell.resolve(path)
if path:sub(1, 1) == "/" then if path:sub(1, 1) == "/" then
return path return fs.combine("", path)
end
return fs.combine(currentDir, path)
end
function shell.resolveProgram(path)
if path:sub(1, 1) == "/" then
return shell.resolve(path)
end end
for seg in shell.path:gmatch("[^;]+") do for seg in shell.path:gmatch("[^;]+") do
local resolved = seg:gsub("%?", path) local resolved = seg:gsub("%?", path)
if fs.exists(resolved) then if fs.exists(resolved) and not fs.isDir(resolved) then
return resolved return resolved
end end
end end
@ -69,7 +77,7 @@ end
function shell.run(...) function shell.run(...)
local args = tokenise(...) local args = tokenise(...)
local command = args[1] local command = args[1]
local path = shell.resolve(command) local path = shell.resolveProgram(command)
if not path then if not path then
printError("Command not found: " .. command) printError("Command not found: " .. command)

View file

@ -13,7 +13,7 @@ if #args == 0 then
end end
local outputName = args[2] or fs.getName(args[1]) local outputName = args[2] or fs.getName(args[1])
local outputPath = fs.combine(shell.getDir(), outputName) local outputPath = shell.resolve(outputName)
if not http.checkURL(args[1]) then if not http.checkURL(args[1]) then
error("Invalid URL", 0) error("Invalid URL", 0)

View file

@ -80,10 +80,15 @@ public class FileSystem : IPlugin
}, },
new() new()
{ {
name = "getAttributes", name = "attributes",
function = L_GetAttributes, function = L_GetAttributes,
}, },
new() new()
{
name = "isDir",
function = L_IsDirectory,
},
new()
{ {
name = "open", name = "open",
function = L_Open, function = L_Open,
@ -466,6 +471,17 @@ public class FileSystem : IPlugin
return 1; return 1;
} }
private static int L_IsDirectory(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var path = Resolve(L.CheckString(1));
L.PushBoolean(Directory.Exists(path));
return 1;
}
private static int L_Open(IntPtr state) private static int L_Open(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);