From 0d32a10ca3b0c047d7fa2a3e76e01143af9c7779 Mon Sep 17 00:00:00 2001 From: sammy <38820196+TorchedSammy@users.noreply.github.com> Date: Fri, 14 Oct 2022 19:15:40 -0400 Subject: [PATCH] feat: add builtins clear, exec and cat (#208) * feat: add clear and exec command * docs: add builtins to changelog * feat: add cat command --- CHANGELOG.md | 1 + nature/commands/cat.lua | 25 +++++++++++++++++++++++++ nature/commands/clear.lua | 7 +++++++ nature/commands/exec.lua | 5 +++++ 4 files changed, 38 insertions(+) create mode 100644 nature/commands/cat.lua create mode 100644 nature/commands/clear.lua create mode 100644 nature/commands/exec.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fdae95..738991a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +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`, `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) diff --git a/nature/commands/clear.lua b/nature/commands/clear.lua new file mode 100644 index 0000000..68aa197 --- /dev/null +++ b/nature/commands/clear.lua @@ -0,0 +1,7 @@ +local ansikit = require 'ansikit' +local commander = require 'commander' + +commander.register('clear', function() + ansikit.clear(true) + ansikit.cursorTo(0, 0) +end) diff --git a/nature/commands/exec.lua b/nature/commands/exec.lua new file mode 100644 index 0000000..d279e31 --- /dev/null +++ b/nature/commands/exec.lua @@ -0,0 +1,5 @@ +local commander = require 'commander' + +commander.register('exec', function(args) + hilbish.exec(args[1]) +end)