From c6e3fa99c8b5fafc22254b2d5d32db4fb7d50a15 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 20 Jan 2023 16:28:58 +0100 Subject: [PATCH] added fs.isDir, renamed fs.getAttributes to fs.attributes, more stuff in Lua --- Capy64/Assets/Lua/bin/cd.lua | 6 +++--- Capy64/Assets/Lua/bin/hello.lua | 18 +++++++++++++++++- Capy64/Assets/Lua/bin/ls.lua | 13 +++---------- Capy64/Assets/Lua/bin/mkdir.lua | 2 +- Capy64/Assets/Lua/bin/rm.lua | 2 +- Capy64/Assets/Lua/bin/shell.lua | 16 ++++++++++++---- Capy64/Assets/Lua/bin/wget.lua | 2 +- Capy64/LuaRuntime/Libraries/FileSystem.cs | 18 +++++++++++++++++- 8 files changed, 55 insertions(+), 22 deletions(-) diff --git a/Capy64/Assets/Lua/bin/cd.lua b/Capy64/Assets/Lua/bin/cd.lua index db4323f..3a77dfc 100644 --- a/Capy64/Assets/Lua/bin/cd.lua +++ b/Capy64/Assets/Lua/bin/cd.lua @@ -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 diff --git a/Capy64/Assets/Lua/bin/hello.lua b/Capy64/Assets/Lua/bin/hello.lua index 1385fe3..22eb1bb 100644 --- a/Capy64/Assets/Lua/bin/hello.lua +++ b/Capy64/Assets/Lua/bin/hello.lua @@ -1 +1,17 @@ -print("Hello, world!") \ No newline at end of file +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) \ No newline at end of file diff --git a/Capy64/Assets/Lua/bin/ls.lua b/Capy64/Assets/Lua/bin/ls.lua index f30f6eb..2590419 100644 --- a/Capy64/Assets/Lua/bin/ls.lua +++ b/Capy64/Assets/Lua/bin/ls.lua @@ -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 diff --git a/Capy64/Assets/Lua/bin/mkdir.lua b/Capy64/Assets/Lua/bin/mkdir.lua index f0e57c1..47cd736 100644 --- a/Capy64/Assets/Lua/bin/mkdir.lua +++ b/Capy64/Assets/Lua/bin/mkdir.lua @@ -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 diff --git a/Capy64/Assets/Lua/bin/rm.lua b/Capy64/Assets/Lua/bin/rm.lua index 51f5de8..9e798fb 100644 --- a/Capy64/Assets/Lua/bin/rm.lua +++ b/Capy64/Assets/Lua/bin/rm.lua @@ -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) \ No newline at end of file diff --git a/Capy64/Assets/Lua/bin/shell.lua b/Capy64/Assets/Lua/bin/shell.lua index c640036..e39664f 100644 --- a/Capy64/Assets/Lua/bin/shell.lua +++ b/Capy64/Assets/Lua/bin/shell.lua @@ -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) diff --git a/Capy64/Assets/Lua/bin/wget.lua b/Capy64/Assets/Lua/bin/wget.lua index 63ab7a4..5eb07ec 100644 --- a/Capy64/Assets/Lua/bin/wget.lua +++ b/Capy64/Assets/Lua/bin/wget.lua @@ -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) diff --git a/Capy64/LuaRuntime/Libraries/FileSystem.cs b/Capy64/LuaRuntime/Libraries/FileSystem.cs index 26020b6..32b9847 100644 --- a/Capy64/LuaRuntime/Libraries/FileSystem.cs +++ b/Capy64/LuaRuntime/Libraries/FileSystem.cs @@ -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);