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]
if #args == 0 then
if not dir then
dir = shell.homePath
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)
return false
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()
if args[1] then
dir = fs.combine(shell.getDir(), args[1])
dir = shell.resolve(args[1])
end
if not fs.exists(dir) then
error("No such directory: " .. dir, 0)
return false
end
local attr = fs.getAttributes(dir)
if not attr.isDirectory then
if not fs.isDir(dir) then
error("No such directory: " .. dir, 0)
return false
end
local files = fs.list(dir)
for k,v in ipairs(files) do
local attr = fs.getAttributes(fs.combine(dir, v))
if attr.isDirectory then
if fs.isDir(fs.combine(dir, v)) then
term.setForeground(colors.lightBlue)
print(v .. "/")
else

View file

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

View file

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

View file

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

View file

@ -13,7 +13,7 @@ if #args == 0 then
end
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
error("Invalid URL", 0)

View file

@ -80,10 +80,15 @@ public class FileSystem : IPlugin
},
new()
{
name = "getAttributes",
name = "attributes",
function = L_GetAttributes,
},
new()
{
name = "isDir",
function = L_IsDirectory,
},
new()
{
name = "open",
function = L_Open,
@ -466,6 +471,17 @@ public class FileSystem : IPlugin
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)
{
var L = Lua.FromIntPtr(state);