v1.4.0 - Added better path handling to the filesystem library, cleaned up mv, cp, cd, cat and their help files.

This commit is contained in:
TheWahlolly
2025-05-22 21:00:38 +03:00
parent f52d0f7015
commit 500419de59
12 changed files with 95 additions and 86 deletions
+15 -15
View File
@@ -1,20 +1,20 @@
local args = {...}
local file = args[1]
args = nil
local files = {...}
local fs = import("filesystem")
if not file then
if not files or not files[1] then
shell.run("help cat")
return
end
if file:sub(1, 1) ~= "/" then
file = shell.workingDirectory .. file
for _, file in ipairs(files) do
if file:sub(1, 1) ~= "/" then
file = fs.concat(shell.workingDirectory, file)
end
if not fs.exists(file) then
print("\27[91mFile does not exist.")
end
local handle = fs.open(file, "r")
local data
repeat
data = handle:read(math.huge or math.maxinteger)
print(data, false)
until not data
end
if not fs.exists(file) then
print("\27[91mFile does not exist.")
end
local handle = fs.open(file, "r")
local data
repeat
data = handle:read(math.huge or math.maxinteger)
print(data, false)
until not data
+7 -22
View File
@@ -1,29 +1,14 @@
local args = {...}
local directory = args[1]
args = nil
local directory = ...
local fs = import("filesystem")
if not directory then
return
end
if directory == ".." then
local backDirectory = shell.workingDirectory:match("(.+)/.-/")
if backDirectory then
backDirectory = backDirectory .. "/"
else
backDirectory = "/"
end
shell.workingDirectory = backDirectory
if directory:sub(1, 1) ~= "/" then
directory = fs.concat(shell.workingDirectory, directory)
end
if fs.exists(directory) and fs.isDirectory(directory) then
shell.workingDirectory = fs.canonical(directory)
else
if directory:sub(-1, -1) ~= "/" then
directory = directory .. "/"
end
if directory:sub(1, 1) ~= "/" then
directory = shell.workingDirectory .. directory
end
if fs.exists(directory) and fs.isDirectory(directory) or fs.exists(shell.workingDirectory .. directory) and fs.isDirectory(shell.workingDirectory .. directory) then
shell.workingDirectory = directory
else
print("\27[91mNo such directory.")
end
print("\27[91mNo such directory.")
end
+3 -5
View File
@@ -1,6 +1,4 @@
local args = {...}
local fromFile, toFile = args[1], args[2]
args = nil
local fromFile, toFile = ...
local fs = import("filesystem")
if not fromFile or not toFile then
@@ -8,10 +6,10 @@ if not fromFile or not toFile then
return
end
if fromFile:sub(1, 1) ~= "/" then
fromFile = shell.workingDirectory .. fromFile
fromFile = fs.concat(shell.workingDirectory, fromFile)
end
if toFile:sub(1, 1) ~= "/" then
toFile = shell.workingDirectory .. toFile
toFile = fs.concat(shell.workingDirectory, toFile)
end
if fromFile == toFile then
print("\27[91mSource and destination are the same.")
+4 -4
View File
@@ -1,8 +1,8 @@
Usage: cat [FILE]
Usage: cat [FILES]...
Concatenates and prints a file.
FILE Specifies the path to the file to print.
FILES Specifies the paths to the files to print.
Examples:
cat /init.lua Concatenates and prints init.lua in the root directory.
cat help.lua Concatenates and prints help.lua in the current working directory.
cat /init.lua Concatenates and prints init.lua in the root directory.
cat help.lua cat.lua Concatenates and prints help.lua and cat.lua in the current working directory.
+4 -3
View File
@@ -1,9 +1,10 @@
Usage: cp [FLAGS] [SOURCE] [DESTINATION]
Copies a file.
-o, --overwrite Allows any file that might be at the destination to be overwritten.
SOURCE Specifies the file to be copied.
DESTINATION Specifies the path to copy the file to.
FLAGS Specifies extra options when executing the command.
-o, --overwrite Allows any file that might be at the destination to be overwritten.
SOURCE Specifies the file to be copied.
DESTINATION Specifies the path to copy the file to.
Examples:
cp /home/a.txt /b.txt Copies the file at /home/a.txt to /b.txt.
+4 -3
View File
@@ -1,9 +1,10 @@
Usage: mv [FLAGS] [SOURCE] [DESTINATION]
Moves/renames a file.
-o, --overwrite Allows any file that might be at the destination to be overwritten.
SOURCE Specifies the file to be moved/renamed.
DESTINATION Specifies the path/filename to move/rename the file to.
FLAGS Specifies extra options when executing the command.
-o, --overwrite Allows any file that might be at the destination to be overwritten.
SOURCE Specifies the file to be moved/renamed.
DESTINATION Specifies the path/filename to move/rename the file to.
Examples:
mv /home/a.txt /b.txt Moves the file at /home/a.txt to /b.txt.
+2 -4
View File
@@ -1,6 +1,4 @@
local args = {...}
local directory = args[1]
args = nil
local directory = ...
local fs = import("filesystem")
if not directory then
@@ -8,7 +6,7 @@ if not directory then
return
end
if directory:sub(1, 1) ~= "/" then
directory = shell.workingDirectory .. directory
directory = fs.concat(shell.workingDirectory, directory)
end
if fs.exists(directory) then
print("\27[91mAn object already exists at the specified path.")
+3 -5
View File
@@ -1,6 +1,4 @@
local args = {...}
local fromFile, toFile = args[1], args[2]
args = nil
local fromFile, toFile = ...
local fs = import("filesystem")
if not fromFile or not toFile then
@@ -8,10 +6,10 @@ if not fromFile or not toFile then
return
end
if fromFile:sub(1, 1) ~= "/" then
fromFile = shell.workingDirectory .. fromFile
fromFile = fs.concat(shell.workingDirectory, fromFile)
end
if toFile:sub(1, 1) ~= "/" then
toFile = shell.workingDirectory .. toFile
toFile = fs.concat(shell.workingDirectory, toFile)
end
if fromFile == toFile then
print("\27[91mSource and destination are the same.")
+3 -4
View File
@@ -1,6 +1,4 @@
local args = {...}
local file = args[1]
args = nil
local file = ...
local fs = import("filesystem")
if not file then
@@ -8,9 +6,10 @@ if not file then
return
end
if file:sub(1, 1) ~= "/" then
file = shell.workingDirectory .. file
file = fs.concat(shell.workingDirectory, file)
end
if not fs.exists(file) then
print("\27[91mFile does not exist.")
return
end
fs.remove(file)