From db01a8d741a44e784639def603c8aed9483e2dc5 Mon Sep 17 00:00:00 2001 From: Ponali Date: Sat, 4 Oct 2025 14:08:36 +0200 Subject: [PATCH] added the touch app --- halyde/apps/cat.lua | 4 ++-- halyde/apps/helpdb/touch.txt | 6 ++++++ halyde/apps/touch.lua | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 halyde/apps/helpdb/touch.txt create mode 100644 halyde/apps/touch.lua diff --git a/halyde/apps/cat.lua b/halyde/apps/cat.lua index 2b18745..5ac8b69 100644 --- a/halyde/apps/cat.lua +++ b/halyde/apps/cat.lua @@ -1,11 +1,11 @@ -local files = {...} +local files = { ... } local shell = require("shell") local fs = require("filesystem") if not files or not files[1] then shell.run("help cat") return end -for _, file in ipairs(files) do +for _, file in ipairs(files) do if file:sub(1, 1) ~= "/" then file = fs.concat(shell.getWorkingDirectory(), file) end diff --git a/halyde/apps/helpdb/touch.txt b/halyde/apps/helpdb/touch.txt new file mode 100644 index 0000000..8ab5804 --- /dev/null +++ b/halyde/apps/helpdb/touch.txt @@ -0,0 +1,6 @@ +Usage: touch [FLAGS] [FILE] +Creates a file with empty content. + + FLAGS Specifies extra options when executing the command. + -o, --overwrite Allows emptying out a file if it already exists. + FILE The path of the file to create.² diff --git a/halyde/apps/touch.lua b/halyde/apps/touch.lua new file mode 100644 index 0000000..acfece7 --- /dev/null +++ b/halyde/apps/touch.lua @@ -0,0 +1,25 @@ +local cliparse = require("cliparse") +cliparse.config({ + ["o"] = 0, + ["overwrite"] = 0, +}) +local parsed = cliparse.parse(...) +local file = parsed.args[1] +local fs = require("filesystem") +local shell = require("shell") + +if not file then + return shell.run("help touch") +end + +if file:sub(1, 1) ~= "/" then + file = fs.concat(shell.getWorkingDirectory(), file) +end + +if fs.exists(file) and not (parsed.flags.o or parsed.flags.overwrite) then + return print("\x1b[91mFile already exists.\n│ To empty file contents, use -o.") +end + +local handle = fs.open(file, "w") +handle:write("") -- just in case +handle:close()