v0.7.0 - Added basic CLI tools: ls, cd, cp, mv, rm, lua
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
local args = {...}
|
||||
local file = args[1]
|
||||
args = nil
|
||||
local fs = import("filesystem")
|
||||
|
||||
if file:sub(1, 1) ~= "/" then
|
||||
file = 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
|
||||
@@ -0,0 +1,26 @@
|
||||
local args = {...}
|
||||
local directory = args[1]
|
||||
args = nil
|
||||
local fs = import("filesystem")
|
||||
|
||||
if directory == ".." then
|
||||
local backDirectory = shell.workingDirectory:match("(.+)/.-/")
|
||||
if backDirectory then
|
||||
backDirectory = backDirectory .. "/"
|
||||
else
|
||||
backDirectory = "/"
|
||||
end
|
||||
shell.workingDirectory = backDirectory
|
||||
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("error: no such directory")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
clear()
|
||||
-- truly so much going on here
|
||||
@@ -0,0 +1,25 @@
|
||||
local args = {...}
|
||||
local fromFile, toFile = args[1], args[2]
|
||||
args = nil
|
||||
local fs = import("filesystem")
|
||||
|
||||
if fromFile:sub(1, 1) ~= "/" then
|
||||
fromFile = shell.workingDirectory .. fromFile
|
||||
end
|
||||
if toFile:sub(1, 1) ~= "/" then
|
||||
toFile = shell.workingDirectory .. toFile
|
||||
end
|
||||
if fromFile == toFile then
|
||||
print("\27[91mSource and destination are the same.")
|
||||
end
|
||||
if not fs.exists(fromFile) then
|
||||
print("\27[91mSource file does not exist.")
|
||||
end
|
||||
if fs.exists(toFile) then
|
||||
print("Destination file already exists. Overwrite it? [Y/n] ", false)
|
||||
if read():lower() == "n" then
|
||||
print("Aborted.")
|
||||
return
|
||||
end
|
||||
end
|
||||
fs.copy(fromFile, toFile)
|
||||
@@ -0,0 +1 @@
|
||||
print("\27[40m \27[41m \27[42m \27[43m \27[44m \27[45m \27[46m \27[47m \n\27[100m \27[101m \27[102m \27[103m \27[104m \27[105m \27[106m \27[107m ")
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
local args = {...}
|
||||
local target = args[1]
|
||||
args = nil
|
||||
local fs = import("filesystem")
|
||||
local maxLength = 0
|
||||
local margin = 2 -- minimum space between filename and size
|
||||
local dirTable = {}
|
||||
local fileTable = {}
|
||||
|
||||
if target then
|
||||
if target:sub(1, 1) ~= "/" then
|
||||
target = shell.workingDirectory .. target
|
||||
end
|
||||
if target:sub(-1, -1) ~= "/" then
|
||||
target = target .. "/"
|
||||
end
|
||||
else
|
||||
target = shell.workingDirectory
|
||||
end
|
||||
|
||||
local files = fs.list(target)
|
||||
|
||||
for _, file in pairs(files) do
|
||||
if file:sub(-1, -1) == "/" then
|
||||
table.insert(dirTable, file)
|
||||
file = file:sub(1, -2)
|
||||
else
|
||||
table.insert(fileTable, file)
|
||||
end
|
||||
if unicode.wlen(file) > maxLength then
|
||||
maxLength = unicode.wlen(file)
|
||||
end
|
||||
end
|
||||
table.sort(dirTable)
|
||||
table.sort(fileTable)
|
||||
files = {}
|
||||
for _, v in ipairs(dirTable) do
|
||||
table.insert(files, v)
|
||||
end
|
||||
for _, v in ipairs(fileTable) do
|
||||
table.insert(files, v)
|
||||
end
|
||||
dirTable, fileTable = nil, nil
|
||||
for _, file in ipairs(files) do
|
||||
local dir = false
|
||||
local filetext
|
||||
if file:sub(-1, -1) == "/" then -- i think this is a more efficient way to check if it's a directory
|
||||
dir = true
|
||||
filetext = "\27[93m"..file:sub(1, -2)
|
||||
elseif file:find(".") and file:match("[^.]+$") == "lua" then
|
||||
filetext = "\27[92m"..file
|
||||
end
|
||||
filetext = (filetext or file)..string.rep(" ", maxLength - unicode.wlen(file) + margin)
|
||||
if dir then
|
||||
print(filetext.." \27[0m[DIR]")
|
||||
else
|
||||
local size = fs.size(target .. file)
|
||||
local sizeString
|
||||
if convert(size, "B", "GiB") >= 1 then
|
||||
sizeString = tostring(math.floor(convert(size, "B", "GiB") * 100 + 0.5) / 100).." GiB"
|
||||
elseif convert(size, "B", "MiB") >= 1 then
|
||||
sizeString = tostring(math.floor(convert(size, "B", "MiB") * 100 + 0.5) / 100).." MiB"
|
||||
elseif convert(size, "B", "KiB") >= 1 then
|
||||
sizeString = tostring(math.floor(convert(size, "B", "KiB") * 100 + 0.5) / 100).." KiB"
|
||||
else
|
||||
sizeString = tostring(size).." B"
|
||||
end
|
||||
print(filetext.."\27[0m"..sizeString)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
print("\27[44m".._VERSION.."\27[0m shell")
|
||||
print('Type "exit" to exit.')
|
||||
while true do
|
||||
print("\27[44mlua>\27[0m ", false)
|
||||
local command = read()
|
||||
if command == "exit" then
|
||||
return
|
||||
else
|
||||
local function runCommand()
|
||||
assert(load(command))()
|
||||
end
|
||||
local result, reason = xpcall(runCommand, function(errMsg)
|
||||
return errMsg .. "\n\n" .. debug.traceback()
|
||||
end)
|
||||
if not result then
|
||||
print("\27[91m" .. reason)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
local args = {...}
|
||||
local fromFile, toFile = args[1], args[2]
|
||||
args = nil
|
||||
local fs = import("filesystem")
|
||||
|
||||
if fromFile:sub(1, 1) ~= "/" then
|
||||
fromFile = shell.workingDirectory .. fromFile
|
||||
end
|
||||
if toFile:sub(1, 1) ~= "/" then
|
||||
toFile = shell.workingDirectory .. toFile
|
||||
end
|
||||
if fromFile == toFile then
|
||||
print("\27[91mSource and destination are the same.")
|
||||
end
|
||||
if not fs.exists(fromFile) then
|
||||
print("\27[91mSource file does not exist.")
|
||||
end
|
||||
if fs.exists(toFile) then
|
||||
print("Destination file already exists. Overwrite it? [Y/n] ", false)
|
||||
if read():lower() == "n" then
|
||||
print("Aborted.")
|
||||
return
|
||||
end
|
||||
end
|
||||
fs.rename(fromFile, toFile)
|
||||
@@ -0,0 +1,12 @@
|
||||
local args = {...}
|
||||
local file = args[1]
|
||||
args = nil
|
||||
local fs = import("filesystem")
|
||||
|
||||
if file:sub(1, 1) ~= "/" then
|
||||
file = shell.workingDirectory .. file
|
||||
end
|
||||
if not fs.exists(file) then
|
||||
print("\27[91mFile does not exist.")
|
||||
end
|
||||
fs.remove(file)
|
||||
Reference in New Issue
Block a user