2022-12-15 04:00:54 +00:00
|
|
|
// library for custom commands
|
2023-01-20 23:03:41 +00:00
|
|
|
/*
|
|
|
|
Commander is a library for writing custom commands in Lua.
|
|
|
|
In order to make it easier to write commands for Hilbish,
|
|
|
|
not require separate scripts and to be able to use in a config,
|
|
|
|
the Commander library exists. This is like a very simple wrapper
|
|
|
|
that works with Hilbish for writing commands. Example:
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local commander = require 'commander'
|
|
|
|
|
|
|
|
commander.register('hello', function(args, sinks)
|
|
|
|
sinks.out:writeln 'Hello world!'
|
|
|
|
end)
|
|
|
|
```
|
|
|
|
|
|
|
|
In this example, a command with the name of `hello` is created
|
|
|
|
that will print `Hello world!` to output. One question you may
|
|
|
|
have is: What is the `sinks` parameter?
|
|
|
|
|
|
|
|
A sink is a writable/readable pipe, or you can imagine a Lua
|
|
|
|
file. It's used in this case to write to the proper output,
|
|
|
|
incase a user either pipes to another command or redirects somewhere else.
|
|
|
|
|
|
|
|
So, the `sinks` parameter is a table containing 3 sinks:
|
|
|
|
`in`, `out`, and `err`.
|
|
|
|
- `in` is the standard input. You can read from this sink
|
|
|
|
to get user input. (**This is currently unimplemented.**)
|
|
|
|
- `out` is standard output. This is usually where text meant for
|
|
|
|
output should go.
|
|
|
|
- `err` is standard error. This sink is for writing errors, as the
|
|
|
|
name would suggest.
|
|
|
|
|
|
|
|
A sink has 2 methods:
|
|
|
|
- `write(str)` will write to the sink.
|
|
|
|
- `writeln(str)` will write to the sink with a newline at the end.
|
|
|
|
*/
|
2021-03-20 22:49:15 +00:00
|
|
|
package commander
|
|
|
|
|
|
|
|
import (
|
2021-10-16 16:40:53 +00:00
|
|
|
"hilbish/util"
|
2022-08-17 22:01:32 +00:00
|
|
|
"hilbish/golibs/bait"
|
2021-10-16 16:40:53 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
|
|
"github.com/arnodel/golua/lib/packagelib"
|
2021-03-20 22:49:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Commander struct{
|
2022-08-17 22:01:32 +00:00
|
|
|
Events *bait.Bait
|
2022-04-04 10:40:02 +00:00
|
|
|
Loader packagelib.Loader
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 22:01:32 +00:00
|
|
|
func New(rtm *rt.Runtime) Commander {
|
2022-04-04 10:40:02 +00:00
|
|
|
c := Commander{
|
2022-08-17 22:01:32 +00:00
|
|
|
Events: bait.New(rtm),
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
c.Loader = packagelib.Loader{
|
|
|
|
Load: c.loaderFunc,
|
|
|
|
Name: "commander",
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func (c *Commander) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
|
|
|
exports := map[string]util.LuaExport{
|
|
|
|
"register": util.LuaExport{c.cregister, 2, false},
|
|
|
|
"deregister": util.LuaExport{c.cderegister, 1, false},
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
mod := rt.NewTable()
|
|
|
|
util.SetExports(rtm, mod, exports)
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return rt.TableValue(mod), nil
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// register(name, cb)
|
|
|
|
// Register a command with `name` that runs `cb` when ran
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param name string
|
|
|
|
// --- @param cb function
|
2022-04-04 10:40:02 +00:00
|
|
|
func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
|
|
|
|
cmdName, cmd, err := util.HandleStrCallback(t, ct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-20 22:49:15 +00:00
|
|
|
|
|
|
|
c.Events.Emit("commandRegister", cmdName, cmd)
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return ct.Next(), err
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// deregister(name)
|
|
|
|
// Deregisters any command registered with `name`
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param name string
|
2022-04-04 10:40:02 +00:00
|
|
|
func (c *Commander) cderegister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := ct.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmdName, err := ct.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-12 14:30:47 +00:00
|
|
|
|
|
|
|
c.Events.Emit("commandDeregister", cmdName)
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return ct.Next(), err
|
2021-06-12 14:30:47 +00:00
|
|
|
}
|