From 614c9011cf8f1f2a028474a73e103c8748bce364 Mon Sep 17 00:00:00 2001 From: TheWahlolly Date: Sun, 27 Apr 2025 18:25:12 +0300 Subject: [PATCH] v0.8.0 - Added help and fetch. --- halyde/apps/cat.lua | 5 ++- halyde/apps/cp.lua | 11 +++--- halyde/apps/fetch.lua | 69 +++++++++++++++++++++++++++++++++- halyde/apps/help.lua | 26 +++++++++++++ halyde/apps/helpdb/cat.txt | 7 ++++ halyde/apps/helpdb/cd.txt | 10 +++++ halyde/apps/helpdb/clear.txt | 5 +++ halyde/apps/helpdb/cp.txt | 10 +++++ halyde/apps/helpdb/default.txt | 15 ++++++++ halyde/apps/helpdb/echo.txt | 8 ++++ halyde/apps/helpdb/fetch.txt | 5 +++ halyde/apps/helpdb/help.txt | 8 ++++ halyde/apps/helpdb/ls.txt | 10 +++++ halyde/apps/helpdb/lua.txt | 5 +++ halyde/apps/helpdb/mv.txt | 10 +++++ halyde/apps/helpdb/rm.txt | 10 +++++ halyde/apps/mv.lua | 9 ++--- halyde/config/shell.cfg | 2 +- halyde/core/boot.lua | 2 +- halyde/core/shell.lua | 5 ++- 20 files changed, 214 insertions(+), 18 deletions(-) create mode 100644 halyde/apps/help.lua create mode 100644 halyde/apps/helpdb/cd.txt create mode 100644 halyde/apps/helpdb/clear.txt create mode 100644 halyde/apps/helpdb/cp.txt create mode 100644 halyde/apps/helpdb/default.txt create mode 100644 halyde/apps/helpdb/echo.txt create mode 100644 halyde/apps/helpdb/fetch.txt create mode 100644 halyde/apps/helpdb/help.txt create mode 100644 halyde/apps/helpdb/ls.txt create mode 100644 halyde/apps/helpdb/lua.txt create mode 100644 halyde/apps/helpdb/mv.txt create mode 100644 halyde/apps/helpdb/rm.txt diff --git a/halyde/apps/cat.lua b/halyde/apps/cat.lua index 3caffd3..a1884fc 100644 --- a/halyde/apps/cat.lua +++ b/halyde/apps/cat.lua @@ -2,7 +2,10 @@ local args = {...} local file = args[1] args = nil local fs = import("filesystem") - +if not file then + shell.run("help cat") + return +end if file:sub(1, 1) ~= "/" then file = shell.workingDirectory .. file end diff --git a/halyde/apps/cp.lua b/halyde/apps/cp.lua index f9e1ac4..e8b454f 100644 --- a/halyde/apps/cp.lua +++ b/halyde/apps/cp.lua @@ -11,15 +11,14 @@ if toFile:sub(1, 1) ~= "/" then end if fromFile == toFile then print("\27[91mSource and destination are the same.") + return end if not fs.exists(fromFile) then print("\27[91mSource file does not exist.") + return 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 +if fs.exists(toFile) and not (table.find(args, "-o") or table.find(args, "--overwrite")) then + print("\27[91mDestination file already exists. Run this command again with -o to overwrite it.") + return end fs.copy(fromFile, toFile) diff --git a/halyde/apps/fetch.lua b/halyde/apps/fetch.lua index b2279bb..d8bd8fe 100644 --- a/halyde/apps/fetch.lua +++ b/halyde/apps/fetch.lua @@ -1 +1,68 @@ -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 ") +termlib.cursorPosY = termlib.cursorPosY + 2 +print(" \27[93m┌┬┐ ┌┐ ┌┐ \n ││├─┤├┬┬┘├─┐\n │┐├┘││││││││\n ││││││││││┌┤\n └┴┴─┴┼┐├─┴─┘\n └─┘ ") +termlib.cursorPosX, termlib.cursorPosY = 17, termlib.cursorPosY - 8 +print("\27[92mOS\27[0m: ".._OSVERSION) +termlib.cursorPosX = 17 +print("\27[92mArchitecture\27[0m: ".._VERSION) +termlib.cursorPosX = 17 +local componentCounter = 0 +for _, _ in component.list() do + componentCounter = componentCounter + 1 +end +print("\27[92mComponents\27[0m: "..tostring(componentCounter)) +termlib.cursorPosX = 17 +print("\27[92mCoroutines\27[0m: "..tostring(#cormgr.corList)) +termlib.cursorPosX = 17 +print("\27[92mBattery\27[0m: "..tostring(math.floor(computer.maxEnergy() / computer.energy() * 1000 + 0.5) / 10).."%") +termlib.cursorPosX = 17 +local totalMemory = computer.totalMemory() +local usedMemory = computer.totalMemory() - computer.freeMemory() +local totalMemoryString +if convert(totalMemory, "B", "GiB") >= 1 then + totalMemoryString = tostring(math.floor(convert(totalMemory, "B", "GiB") * 100 + 0.5) / 100) .. " GiB" +elseif convert(totalMemory, "B", "MiB") >= 1 then + totalMemoryString = tostring(math.floor(convert(totalMemory, "B", "MiB") * 100 + 0.5) / 100) .. " MiB" +elseif convert(totalMemory, "B", "KiB") >= 1 then + totalMemoryString = tostring(math.floor(convert(totalMemory, "B", "KiB") * 100 + 0.5) / 100) .. " KiB" +else + totalMemoryString = tostring(totalMemory) .. " B" +end +local usedMemoryString +if convert(usedMemory, "B", "GiB") >= 1 then + usedMemoryString = tostring(math.floor(convert(usedMemory, "B", "GiB") * 100 + 0.5) / 100) .. " GiB" +elseif convert(usedMemory, "B", "MiB") >= 1 then + usedMemoryString = tostring(math.floor(convert(usedMemory, "B", "MiB") * 100 + 0.5) / 100) .. " MiB" +elseif convert(usedMemory, "B", "KiB") >= 1 then + usedMemoryString = tostring(math.floor(convert(usedMemory, "B", "KiB") * 100 + 0.5) / 100) .. " KiB" +else + usedMemoryString = tostring(usedMemory) .. " B" +end +print("\27[92mMemory\27[0m: "..usedMemoryString.." / "..totalMemoryString) +termlib.cursorPosX = 17 +local totalDisk = component.invoke(computer.getBootAddress(), "spaceTotal") +local usedDisk = component.invoke(computer.getBootAddress(), "spaceUsed") +local totalDiskString +if convert(totalDisk, "B", "GiB") >= 1 then + totalDiskString = tostring(math.floor(convert(totalDisk, "B", "GiB") * 100 + 0.5) / 100) .. " GiB" +elseif convert(totalDisk, "B", "MiB") >= 1 then + totalDiskString = tostring(math.floor(convert(totalDisk, "B", "MiB") * 100 + 0.5) / 100) .. " MiB" +elseif convert(totalDisk, "B", "KiB") >= 1 then + totalDiskString = tostring(math.floor(convert(totalDisk, "B", "KiB") * 100 + 0.5) / 100) .. " KiB" +else + totalDiskString = tostring(totalDisk) .. " B" +end +local usedDiskString +if convert(usedDisk, "B", "GiB") >= 1 then + usedDiskString = tostring(math.floor(convert(usedDisk, "B", "GiB") * 100 + 0.5) / 100) .. " GiB" +elseif convert(usedDisk, "B", "MiB") >= 1 then + usedDiskString = tostring(math.floor(convert(usedDisk, "B", "MiB") * 100 + 0.5) / 100) .. " MiB" +elseif convert(usedDisk, "B", "KiB") >= 1 then + usedDiskString = tostring(math.floor(convert(usedDisk, "B", "KiB") * 100 + 0.5) / 100) .. " KiB" +else + usedDiskString = tostring(usedDisk) .. " B" +end +print("\27[92mDisk\27[0m: "..usedDiskString.." / "..totalDiskString.."\n") +termlib.cursorPosX = 17 +print("\27[40m \27[41m \27[42m \27[43m \27[44m \27[45m \27[46m \27[47m ") +termlib.cursorPosX = 17 +print("\27[100m \27[101m \27[102m \27[103m \27[104m \27[105m \27[106m \27[107m ") diff --git a/halyde/apps/help.lua b/halyde/apps/help.lua new file mode 100644 index 0000000..8db9fe2 --- /dev/null +++ b/halyde/apps/help.lua @@ -0,0 +1,26 @@ +local fs = import("filesystem") +local args = {...} +local command = args[1] +args = nil +if not command then + local handle, data, tmpdata = fs.open("/halyde/apps/helpdb/default.txt", "r"), "", nil + repeat + tmpdata = handle:read(math.huge or math.maxinteger) + data = data .. (tmpdata or "") + until not tmpdata + print(data) + return +end +if shell.aliases[command] then + command = shell.aliases[command] +end +if fs.exists("/halyde/apps/helpdb/" .. command .. ".txt") then + local handle, data, tmpdata = fs.open("/halyde/apps/helpdb/" .. command .. ".txt", "r"), "", nil + repeat + tmpdata = handle:read(math.huge or math.maxinteger) + data = data .. (tmpdata or "") + until not tmpdata + print(data) +else + print("Could not find help file for: " .. command .. ".") +end diff --git a/halyde/apps/helpdb/cat.txt b/halyde/apps/helpdb/cat.txt index 8b13789..e6ce488 100644 --- a/halyde/apps/helpdb/cat.txt +++ b/halyde/apps/helpdb/cat.txt @@ -1 +1,8 @@ +Usage: cat [FILE] +Concatenates and prints a file. + FILE Specifies the path to the file 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. diff --git a/halyde/apps/helpdb/cd.txt b/halyde/apps/helpdb/cd.txt new file mode 100644 index 0000000..77e9898 --- /dev/null +++ b/halyde/apps/helpdb/cd.txt @@ -0,0 +1,10 @@ +Usage: cd [PATH] +Sets the shell working directory. + + PATH Specifies the path to set the shell working directory to. + +Examples: + cd /home/ Sets the shell working directory to /home/. + cd halyde Sets the shell working directory to a directory named "halyde" in the current working directory. + cd .. Sets the shell working directory back one directory. + .. Equivalent of "cd ..". diff --git a/halyde/apps/helpdb/clear.txt b/halyde/apps/helpdb/clear.txt new file mode 100644 index 0000000..f6eee9f --- /dev/null +++ b/halyde/apps/helpdb/clear.txt @@ -0,0 +1,5 @@ +Usage: clear +Clears the screen. + +Examples: + clear Clears the screen. diff --git a/halyde/apps/helpdb/cp.txt b/halyde/apps/helpdb/cp.txt new file mode 100644 index 0000000..9d828c8 --- /dev/null +++ b/halyde/apps/helpdb/cp.txt @@ -0,0 +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. + +Examples: + cp /home/a.txt /b.txt Copies the file at /home/a.txt to /b.txt. + cp -o c.lua d.txt Copies the file c.lua to another file called d.txt in the shell working directory, overwriting any file that might be there. diff --git a/halyde/apps/helpdb/default.txt b/halyde/apps/helpdb/default.txt new file mode 100644 index 0000000..09176a6 --- /dev/null +++ b/halyde/apps/helpdb/default.txt @@ -0,0 +1,15 @@ +All current Halyde shell commands: + cat Concatenates and prints a file. + cd Changes directory. + clear Clears the screen. + cp Copies a file. + echo Prints a message. + fetch Displays system information. + help Shows this. + ls Lists files. + lua Starts the Lua shell. + mv Moves/renames a file. + rm Deletes a file. + +You can get additional information on any app or command by running: + help [COMMAND] diff --git a/halyde/apps/helpdb/echo.txt b/halyde/apps/helpdb/echo.txt new file mode 100644 index 0000000..b955d02 --- /dev/null +++ b/halyde/apps/helpdb/echo.txt @@ -0,0 +1,8 @@ +Usage: echo [TEXT]... +Concatenates and prints text to the terminal. + + TEXT Text to print. + +Examples: + echo test Prints "test" to the terminal. + echo Hello World! Prints "Hello World!" to the terminal. diff --git a/halyde/apps/helpdb/fetch.txt b/halyde/apps/helpdb/fetch.txt new file mode 100644 index 0000000..e207145 --- /dev/null +++ b/halyde/apps/helpdb/fetch.txt @@ -0,0 +1,5 @@ +Usage: fetch +Displays system information including OS version, Lua version, memory, etc. + +Examples: + fetch Displays system information. diff --git a/halyde/apps/helpdb/help.txt b/halyde/apps/helpdb/help.txt new file mode 100644 index 0000000..561a765 --- /dev/null +++ b/halyde/apps/helpdb/help.txt @@ -0,0 +1,8 @@ +Usage: help [COMMAND] +Displays info on the command specified, or a list of commands if one is not specified. + + COMMAND Command to display information on. + +Examples: + help Displays a list of all default commands available. + help cp Displays information about the cp command. diff --git a/halyde/apps/helpdb/ls.txt b/halyde/apps/helpdb/ls.txt new file mode 100644 index 0000000..b1d681b --- /dev/null +++ b/halyde/apps/helpdb/ls.txt @@ -0,0 +1,10 @@ +Usage: ls [PATH] +Lists all files and directories in the specified path, or in the shell working directory if the path isn't specified. +Directories are shown in yellow, executable files are shown in green, and other files are shown in white. + + PATH Path to the folder to list files and directories from. + +Examples: + ls Lists all files and directories from the current shell working directory. + ls /halyde Lists all files and directories from /halyde. + ls apps Lists all files and directories from the apps directory in the shell working directory. diff --git a/halyde/apps/helpdb/lua.txt b/halyde/apps/helpdb/lua.txt new file mode 100644 index 0000000..11b323b --- /dev/null +++ b/halyde/apps/helpdb/lua.txt @@ -0,0 +1,5 @@ +Usage: lua +Starts the Lua shell, where you can type commands to interpret them in real time. + +Examples: + lua Starts the Lua shell. diff --git a/halyde/apps/helpdb/mv.txt b/halyde/apps/helpdb/mv.txt new file mode 100644 index 0000000..59e0e31 --- /dev/null +++ b/halyde/apps/helpdb/mv.txt @@ -0,0 +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. + +Examples: + mv /home/a.txt /b.txt Moves the file at /home/a.txt to /b.txt. + mv -o c.lua d.txt Renames the file c.lua to another file called d.txt in the shell working directory, overwriting any file that might be there. diff --git a/halyde/apps/helpdb/rm.txt b/halyde/apps/helpdb/rm.txt new file mode 100644 index 0000000..ac9c315 --- /dev/null +++ b/halyde/apps/helpdb/rm.txt @@ -0,0 +1,10 @@ +Usage: rm [FLAGS] [PATH] +Removes files and directories. + + -r, --recursive Removes directories and their contents recursively. + -f, --force Ignores nonexistent files or directories. + PATH Specifies the file to be moved/renamed. + +Examples: + rm a.txt Removes a.txt in the current shell working directory. + rm -r -f /halyde/core/ Removes everything in /halyde/core forcedly and recursively. Note that trying this on a real machine will remove critical Halyde system files and cause it to stop working. diff --git a/halyde/apps/mv.lua b/halyde/apps/mv.lua index ba8b51e..46c4630 100644 --- a/halyde/apps/mv.lua +++ b/halyde/apps/mv.lua @@ -15,11 +15,8 @@ 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 +if fs.exists(toFile) and not (table.find(args, "-o") or table.find(args, "--overwrite")) then + print("\27[91mDestination file already exists. Run this command again with -o to overwrite it.") + return end fs.rename(fromFile, toFile) diff --git a/halyde/config/shell.cfg b/halyde/config/shell.cfg index 6eebc8b..3cc8922 100644 --- a/halyde/config/shell.cfg +++ b/halyde/config/shell.cfg @@ -1,5 +1,5 @@ local shellcfg = { - ["startupMessage"] = "\n │\n │ ".._OSVERSION.." running on ".._VERSION..'\n │ Welcome! Type "help" to get started.\n │\n ', -- message shown on startup + ["startupMessage"] = "\n │\n │ ".._OSVERSION..'\n │ Welcome! Type "help" to get started.\n │\n ', -- message shown on startup ["prompt"] = "\x1b[92m%s > \x1b[0m", -- shell prompt, %s will be replaced with working directory. example: "%s > " turns to "/current/working/directory > " ["path"] = { -- default locations where programs will be run from "/halyde/apps/" diff --git a/halyde/core/boot.lua b/halyde/core/boot.lua index 1f97c68..50b2364 100644 --- a/halyde/core/boot.lua +++ b/halyde/core/boot.lua @@ -1,7 +1,7 @@ local loadfile = ... local filesystem = loadfile("/halyde/lib/filesystem.lua")(loadfile) -_G._OSVERSION = "Halyde 0.7.0" +_G._OSVERSION = "Halyde 0.8.0" function _G.import(module, ...) local args = table.pack(...) diff --git a/halyde/core/shell.lua b/halyde/core/shell.lua index 6f78ef3..fb4b750 100644 --- a/halyde/core/shell.lua +++ b/halyde/core/shell.lua @@ -8,7 +8,8 @@ _G.shell = {} _G.shell.workingDirectory = shellcfg["defaultWorkingDirectory"] _G.shell.aliases = shellcfg["aliases"] -local function parseCommand(command) +function _G.shell.run(command) + checkArg(1, command, "string") if shell.aliases[command:match("[^ ]+")] then local _, cmdend = command:find("[^ ]+") command = shell.aliases[command:match("[^ ]+")] .. command:sub(cmdend + 1) @@ -79,5 +80,5 @@ while true do -- termlib.cursorPosX = #(shell.workingDirectory .. " > ") -- termlib.cursorPosY = termlib.cursorPosY - 1 local shellCommand = read() - parseCommand(shellCommand) + shell.run(shellCommand) end