feat: make prompt optional in hilbish.read

pull/312/head
TorchedSammy 2022-10-10 18:40:29 -04:00
parent 3bec2c91a8
commit 3ee2b03330
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 11 additions and 7 deletions

18
api.go
View File

@ -250,23 +250,27 @@ func hlcwd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
} }
// read(prompt) -> input? // read(prompt?) -> input?
// Read input from the user, using Hilbish's line editor/input reader. // Read input from the user, using Hilbish's line editor/input reader.
// This is a separate instance from the one Hilbish actually uses. // 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) // Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen)
// --- @param prompt string // --- @param prompt string
func hlread(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func hlread(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil { luaprompt := c.Arg(0)
return nil, err if typ := luaprompt.Type(); typ != rt.StringType && typ != rt.NilType {
return nil, errors.New("expected #1 to be a string")
} }
luaprompt, err := c.StringArg(0) prompt, ok := luaprompt.TryString()
if err != nil { if !ok {
return nil, err // if we are here and `luaprompt` is not a string, it's nil
// substitute with an empty string
prompt = ""
} }
lualr := &lineReader{ lualr := &lineReader{
rl: readline.NewInstance(), rl: readline.NewInstance(),
} }
lualr.SetPrompt(luaprompt) lualr.SetPrompt(prompt)
input, err := lualr.Read() input, err := lualr.Read()
if err != nil { if err != nil {