From 84ec3d085d1e7c77b5a22dff9bc4630d72968edf Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Sat, 30 Oct 2021 19:53:42 -0400 Subject: [PATCH] feat: add hilbish.read function `hilbish.read` will read input from the user, using hilbish's line editor library (hilbiline or readline) --- docs/hilbish.txt | 4 ++++ hilbish.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/docs/hilbish.txt b/docs/hilbish.txt index e04e975..b74c69c 100644 --- a/docs/hilbish.txt +++ b/docs/hilbish.txt @@ -2,5 +2,9 @@ cwd() > Returns the current directory of the shell flag(f) > Checks if the `f` flag has been passed to Hilbish. +read(prompt) -> input? > Read input from the user, using Hilbish's line editor/input reader. +This is a separate instance from the one Hilbish actually uses. +Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen) + run(cmd) > Runs `cmd` in Hilbish's sh interpreter diff --git a/hilbish.go b/hilbish.go index 24f705b..eef61c9 100644 --- a/hilbish.go +++ b/hilbish.go @@ -19,6 +19,7 @@ var exports = map[string]lua.LGFunction { "run": hlrun, "flag": hlflag, "cwd": hlcwd, + "read": hlread, } func HilbishLoader(L *lua.LState) int { @@ -94,3 +95,22 @@ func getenv(key, fallback string) string { } return value } + +// read(prompt) -> input? +// Read input from the user, using Hilbish's line editor/input reader. +// This is a separate instance from the one Hilbish actually uses. +// Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen) +func hlread(L *lua.LState) int { + luaprompt := L.CheckString(1) + lualr := NewLineReader(luaprompt) + + input, err := lualr.Read() + if err != nil { + L.Push(lua.LNil) + return 1 + } + + L.Push(lua.LString(input)) + return 1 +} +