2022-12-15 04:00:54 +00:00
|
|
|
// library for custom commands
|
2023-01-20 23:07:42 +00:00
|
|
|
/*
|
2023-12-26 03:08:29 +00:00
|
|
|
Commander is the library which handles Hilbish commands. This makes
|
|
|
|
the user able to add Lua-written commands to their shell without making
|
|
|
|
a separate script in a bin folder. Instead, you may simply use the Commander
|
|
|
|
library in your Hilbish config.
|
2023-01-20 23:07:42 +00:00
|
|
|
|
|
|
|
```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?
|
|
|
|
|
2024-04-28 01:03:54 +00:00
|
|
|
The `sinks` parameter is a table with 3 keys: `input`, `out`, and `err`.
|
|
|
|
There is an `in` alias to `input`, but it requires using the string accessor syntax (`sinks['in']`)
|
|
|
|
as `in` is also a Lua keyword, so `input` is preferred for use.
|
|
|
|
All of them are a @Sink.
|
|
|
|
In the future, `sinks.in` will be removed.
|
2023-12-26 03:08:29 +00:00
|
|
|
|
|
|
|
- `in` is the standard input.
|
|
|
|
You may use the read functions on this sink to get input from the user.
|
|
|
|
- `out` is standard output.
|
|
|
|
This is usually where command output should go.
|
|
|
|
- `err` is standard error.
|
|
|
|
This sink is for writing errors, as the name would suggest.
|
2023-01-20 23:07:42 +00:00
|
|
|
*/
|
2021-03-20 22:49:15 +00:00
|
|
|
package commander
|
|
|
|
|
|
|
|
import (
|
2024-07-20 14:50:04 +00:00
|
|
|
"hilbish/moonlight"
|
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"
|
2021-03-20 22:49:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Commander struct{
|
2022-08-17 22:01:32 +00:00
|
|
|
Events *bait.Bait
|
2024-04-28 01:29:06 +00:00
|
|
|
Commands map[string]*rt.Closure
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 14:50:04 +00:00
|
|
|
func New(rtm *moonlight.Runtime) *Commander {
|
2024-04-28 01:29:06 +00:00
|
|
|
c := &Commander{
|
2022-08-17 22:01:32 +00:00
|
|
|
Events: bait.New(rtm),
|
2024-04-28 01:29:06 +00:00
|
|
|
Commands: make(map[string]*rt.Closure),
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
|
|
|
|
return c
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 15:46:07 +00:00
|
|
|
func (c *Commander) Loader(rtm *moonlight.Runtime) moonlight.Value {
|
|
|
|
exports := map[string]moonlight.Export{
|
|
|
|
"register": {c.cregister, 2, false},
|
|
|
|
"deregister": {c.cderegister, 1, false},
|
|
|
|
"registry": {c.cregistry, 0, false},
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
2024-07-20 15:46:07 +00:00
|
|
|
mod := moonlight.NewTable()
|
|
|
|
rtm.SetExports(mod, exports)
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2024-07-20 15:46:07 +00:00
|
|
|
return moonlight.TableValue(mod)
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// register(name, cb)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Adds a new command with the given `name`. When Hilbish has to run a command with a name,
|
|
|
|
// it will run the function providing the arguments and sinks.
|
|
|
|
// #param name string Name of the command
|
|
|
|
// #param cb function Callback to handle command invocation
|
|
|
|
/*
|
|
|
|
#example
|
|
|
|
-- When you run the command `hello` in the shell, it will print `Hello world`.
|
|
|
|
-- If you run it with, for example, `hello Hilbish`, it will print 'Hello Hilbish'
|
|
|
|
commander.register('hello', function(args, sinks)
|
|
|
|
local name = 'world'
|
|
|
|
if #args > 0 then name = args[1] end
|
|
|
|
|
|
|
|
sinks.out:writeln('Hello ' .. name)
|
|
|
|
end)
|
|
|
|
#example
|
|
|
|
*/
|
2024-07-20 15:46:07 +00:00
|
|
|
func (c *Commander) cregister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
cmdName, cmd, err := util.HandleStrCallback(mlr, ct)
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2024-04-28 01:29:06 +00:00
|
|
|
c.Commands[cmdName] = cmd
|
2021-03-20 22:49:15 +00:00
|
|
|
|
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)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Removes the named command. Note that this will only remove Commander-registered commands.
|
|
|
|
// #param name string Name of the command to remove.
|
2024-07-20 15:46:07 +00:00
|
|
|
func (c *Commander) cderegister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
if err := mlr.Check1Arg(ct); err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-20 15:46:07 +00:00
|
|
|
cmdName, err := mlr.StringArg(ct, 0)
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-12 14:30:47 +00:00
|
|
|
|
2024-04-28 01:29:06 +00:00
|
|
|
delete(c.Commands, cmdName)
|
2021-06-12 14:30:47 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return ct.Next(), err
|
2021-06-12 14:30:47 +00:00
|
|
|
}
|
2024-04-28 01:29:06 +00:00
|
|
|
|
|
|
|
// registry() -> table
|
|
|
|
// Returns all registered commanders. Returns a list of tables with the following keys:
|
|
|
|
// - `exec`: The function used to run the commander. Commanders require args and sinks to be passed.
|
|
|
|
// #returns table
|
2024-07-20 15:46:07 +00:00
|
|
|
func (c *Commander) cregistry(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
registryLua := moonlight.NewTable()
|
2024-04-28 01:29:06 +00:00
|
|
|
for cmdName, cmd := range c.Commands {
|
2024-07-20 15:46:07 +00:00
|
|
|
cmdTbl := moonlight.NewTable()
|
|
|
|
cmdTbl.SetField("exec", rt.FunctionValue(cmd))
|
2024-04-28 01:29:06 +00:00
|
|
|
|
2024-07-20 15:46:07 +00:00
|
|
|
registryLua.SetField(cmdName, moonlight.TableValue(cmdTbl))
|
2024-04-28 01:29:06 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 15:46:07 +00:00
|
|
|
return mlr.PushNext1(ct, moonlight.TableValue(registryLua)), nil
|
2024-04-28 01:29:06 +00:00
|
|
|
}
|