2021-03-20 22:49:15 +00:00
|
|
|
package commander
|
|
|
|
|
|
|
|
import (
|
2021-10-16 16:40:53 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
2021-03-20 22:49:15 +00:00
|
|
|
"github.com/chuckpreslar/emission"
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Commander struct{
|
|
|
|
Events *emission.Emitter
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() Commander {
|
|
|
|
return Commander{
|
|
|
|
Events: emission.NewEmitter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commander) Loader(L *lua.LState) int {
|
|
|
|
var exports = map[string]lua.LGFunction{
|
2021-10-16 14:21:05 +00:00
|
|
|
"register": c.cregister,
|
|
|
|
"deregister": c.cderegister,
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
mod := L.SetFuncs(L.NewTable(), exports)
|
2021-10-16 16:40:53 +00:00
|
|
|
util.Document(L, mod, "Commander is Hilbish's custom command library, a way to write commands with the shell in Lua.")
|
2021-03-20 22:49:15 +00:00
|
|
|
L.Push(mod)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// register(name, cb)
|
|
|
|
// Register a command with `name` that runs `cb` when ran
|
|
|
|
func (c *Commander) cregister(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
cmdName := L.CheckString(1)
|
|
|
|
cmd := L.CheckFunction(2)
|
2021-03-20 22:49:15 +00:00
|
|
|
|
|
|
|
c.Events.Emit("commandRegister", cmdName, cmd)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// deregister(name)
|
|
|
|
// Deregisters any command registered with `name`
|
|
|
|
func (c *Commander) cderegister(L *lua.LState) int {
|
2021-06-12 14:30:47 +00:00
|
|
|
cmdName := L.CheckString(1)
|
|
|
|
|
|
|
|
c.Events.Emit("commandDeregister", cmdName)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|