From 373d2e4f8230db0e1d72413c8040a68f2625784e Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Fri, 14 Oct 2022 19:09:45 -0400 Subject: [PATCH] feat: add cat command --- CHANGELOG.md | 2 +- nature/commands/cat.lua | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 nature/commands/cat.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index febac00..738991a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,7 +75,7 @@ disables commands being added to history. - A new and "safer" event emitter has been added. This causes a performance deficit, but avoids a lot of random errors introduced with the new Lua runtime (see [#197]) - `bait.release(name, catcher)` removes `handler` for the named `event` -- `exec` and `clear` builtin commands +- `exec`, `clear` and `cat` builtin commands [#197]: https://github.com/Rosettea/Hilbish/issues/197 diff --git a/nature/commands/cat.lua b/nature/commands/cat.lua new file mode 100644 index 0000000..132db5f --- /dev/null +++ b/nature/commands/cat.lua @@ -0,0 +1,25 @@ +local commander = require 'commander' +local fs = require 'fs' + +commander.register('cat', function(args) + local exit = 0 + + if #args == 0 then + print [[ +usage: cat [file]...]] + end + + for _, fName in ipairs(args) do + local f = io.open(fName) + if f == nil then + exit = 1 + print(string.format('cat: %s: no such file or directory', fName)) + goto continue + end + + io.write(f:read '*a') + ::continue:: + end + io.flush() + return exit +end)