2022-03-20 19:15:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-20 18:49:43 +00:00
|
|
|
"hilbish/moonlight"
|
2022-04-04 10:40:02 +00:00
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2022-03-20 19:15:44 +00:00
|
|
|
)
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface runner
|
|
|
|
// interactive command runner customization
|
2023-12-26 03:08:29 +00:00
|
|
|
/* The runner interface contains functions that allow the user to change
|
|
|
|
how Hilbish interprets interactive input.
|
|
|
|
Users can add and change the default runner for interactive input to any
|
|
|
|
language or script of their choosing. A good example is using it to
|
|
|
|
write command in Fennel.
|
|
|
|
|
|
|
|
Runners are functions that evaluate user input. The default runners in
|
|
|
|
Hilbish can run shell script and Lua code.
|
|
|
|
|
|
|
|
A runner is passed the input and has to return a table with these values.
|
|
|
|
All are not required, only the useful ones the runner needs to return.
|
|
|
|
(So if there isn't an error, just omit `err`.)
|
|
|
|
|
|
|
|
- `exitCode` (number): A numerical code to indicate the exit result.
|
|
|
|
- `input` (string): The user input. This will be used to add
|
|
|
|
to the history.
|
|
|
|
- `err` (string): A string to indicate an interal error for the runner.
|
|
|
|
It can be set to a few special values for Hilbish to throw the right hooks and have a better looking message:
|
|
|
|
|
|
|
|
`[command]: not-found` will throw a command.not-found hook based on what `[command]` is.
|
|
|
|
|
|
|
|
`[command]: not-executable` will throw a command.not-executable hook.
|
|
|
|
- `continue` (boolean): Whether to prompt the user for more input.
|
|
|
|
|
|
|
|
Here is a simple example of a fennel runner. It falls back to
|
|
|
|
shell script if fennel eval has an error.
|
|
|
|
```lua
|
|
|
|
local fennel = require 'fennel'
|
|
|
|
|
|
|
|
hilbish.runnerMode(function(input)
|
|
|
|
local ok = pcall(fennel.eval, input)
|
|
|
|
if ok then
|
|
|
|
return {
|
|
|
|
input = input
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return hilbish.runner.sh(input)
|
|
|
|
end)
|
|
|
|
```
|
|
|
|
*/
|
2024-07-20 18:49:43 +00:00
|
|
|
func runnerModeLoader(rtm *moonlight.Runtime) *moonlight.Table {
|
|
|
|
exports := map[string]moonlight.Export{
|
2022-04-04 10:40:02 +00:00
|
|
|
"sh": {shRunner, 1, false},
|
|
|
|
"lua": {luaRunner, 1, false},
|
|
|
|
"setMode": {hlrunnerMode, 1, false},
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 18:49:43 +00:00
|
|
|
mod := moonlight.NewTable()
|
|
|
|
rtm.SetExports(mod, exports)
|
2022-03-20 19:15:44 +00:00
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface runner
|
|
|
|
// setMode(cb)
|
2023-12-26 03:08:29 +00:00
|
|
|
// This is the same as the `hilbish.runnerMode` function.
|
|
|
|
// It takes a callback, which will be used to execute all interactive input.
|
2022-12-15 04:00:54 +00:00
|
|
|
// In normal cases, neither callbacks should be overrided by the user,
|
|
|
|
// as the higher level functions listed below this will handle it.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param cb function
|
2022-12-15 04:00:54 +00:00
|
|
|
func _runnerMode() {}
|
|
|
|
|
|
|
|
// #interface runner
|
|
|
|
// sh(cmd)
|
|
|
|
// Runs a command in Hilbish's shell script interpreter.
|
|
|
|
// This is the equivalent of using `source`.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param cmd string
|
2024-07-20 18:49:43 +00:00
|
|
|
func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
if err := mlr.Check1Arg(c); err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-20 18:49:43 +00:00
|
|
|
cmd, err := mlr.StringArg(c, 0)
|
2022-03-20 19:15:44 +00:00
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 01:00:28 +00:00
|
|
|
_, exitCode, cont, err := execSh(aliases.Resolve(cmd))
|
2024-07-20 18:49:43 +00:00
|
|
|
var luaErr moonlight.Value = rt.NilValue
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
2024-07-20 18:49:43 +00:00
|
|
|
luaErr = moonlight.StringValue(err.Error())
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
2024-07-20 18:49:43 +00:00
|
|
|
runnerRet := moonlight.NewTable()
|
|
|
|
runnerRet.SetField("input", moonlight.StringValue(cmd))
|
|
|
|
runnerRet.SetField("exitCode", moonlight.IntValue(int(exitCode)))
|
|
|
|
runnerRet.SetField("continue", moonlight.BoolValue(cont))
|
|
|
|
runnerRet.SetField("err", luaErr)
|
2022-03-20 19:15:44 +00:00
|
|
|
|
2024-07-20 18:49:43 +00:00
|
|
|
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface runner
|
|
|
|
// lua(cmd)
|
|
|
|
// Evaluates `cmd` as Lua input. This is the same as using `dofile`
|
|
|
|
// or `load`, but is appropriated for the runner interface.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param cmd string
|
2024-07-20 18:49:43 +00:00
|
|
|
func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
if err := mlr.Check1Arg(c); err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-20 18:49:43 +00:00
|
|
|
cmd, err := mlr.StringArg(c, 0)
|
2022-03-20 19:15:44 +00:00
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 14:11:38 +00:00
|
|
|
input, exitCode, err := handleLua(cmd)
|
2024-07-20 18:49:43 +00:00
|
|
|
var luaErr moonlight.Value = rt.NilValue
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
2024-07-20 18:49:43 +00:00
|
|
|
luaErr = moonlight.StringValue(err.Error())
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
2024-07-20 18:49:43 +00:00
|
|
|
runnerRet := moonlight.NewTable()
|
|
|
|
runnerRet.SetField("input", moonlight.StringValue(input))
|
|
|
|
runnerRet.SetField("exitCode", moonlight.IntValue(int(exitCode)))
|
|
|
|
runnerRet.SetField("err", luaErr)
|
2022-03-20 19:15:44 +00:00
|
|
|
|
2024-07-20 18:49:43 +00:00
|
|
|
|
|
|
|
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|