mirror of https://github.com/Hilbis/Hilbish
fix: cleanup, store lua commands in map
parent
1e1662a6b2
commit
efc956a04c
11
lua.go
11
lua.go
|
@ -45,15 +45,8 @@ func LuaInit() {
|
||||||
|
|
||||||
cmds := commander.New()
|
cmds := commander.New()
|
||||||
// When a command from Lua is added, register it for use
|
// When a command from Lua is added, register it for use
|
||||||
// TODO: maybe dont add command code to a lua table? insstead use a map
|
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
|
||||||
cmds.Events.On("commandRegister",
|
commands[cmdName] = cmd
|
||||||
func(cmdName string, cmd *lua.LFunction) {
|
|
||||||
commands[cmdName] = true
|
|
||||||
l.SetField(
|
|
||||||
l.GetTable(l.GetGlobal("commanding"),
|
|
||||||
lua.LString("__commands")),
|
|
||||||
cmdName,
|
|
||||||
cmd)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
l.PreloadModule("commander", cmds.Loader)
|
l.PreloadModule("commander", cmds.Loader)
|
||||||
|
|
2
main.go
2
main.go
|
@ -22,7 +22,7 @@ var (
|
||||||
l *lua.LState
|
l *lua.LState
|
||||||
lr *LineReader
|
lr *LineReader
|
||||||
|
|
||||||
commands = map[string]bool{}
|
commands = map[string]*lua.LFunction{}
|
||||||
aliases = map[string]string{}
|
aliases = map[string]string{}
|
||||||
|
|
||||||
homedir string
|
homedir string
|
||||||
|
|
16
shell.go
16
shell.go
|
@ -49,13 +49,9 @@ func RunInput(input string) {
|
||||||
hooks.Em.Emit("command.exit", 0)
|
hooks.Em.Emit("command.exit", 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if commands[cmdArgs[0]] {
|
if commands[cmdArgs[0]] != nil {
|
||||||
err := l.CallByParam(lua.P{
|
err := l.CallByParam(lua.P{
|
||||||
Fn: l.GetField(
|
Fn: commands[cmdArgs[0]],
|
||||||
l.GetTable(
|
|
||||||
l.GetGlobal("commanding"),
|
|
||||||
lua.LString("__commands")),
|
|
||||||
cmdArgs[0]),
|
|
||||||
NRet: 1,
|
NRet: 1,
|
||||||
Protect: true,
|
Protect: true,
|
||||||
}, luar.New(l, cmdArgs[1:]))
|
}, luar.New(l, cmdArgs[1:]))
|
||||||
|
@ -128,13 +124,9 @@ func execCommand(cmd string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If command is defined in Lua then run it
|
// If command is defined in Lua then run it
|
||||||
if commands[args[0]] {
|
if commands[args[0]] != nil {
|
||||||
err := l.CallByParam(lua.P{
|
err := l.CallByParam(lua.P{
|
||||||
Fn: l.GetField(
|
Fn: commands[args[0]],
|
||||||
l.GetTable(
|
|
||||||
l.GetGlobal("commanding"),
|
|
||||||
lua.LString("__commands")),
|
|
||||||
args[0]),
|
|
||||||
NRet: 1,
|
NRet: 1,
|
||||||
Protect: true,
|
Protect: true,
|
||||||
}, luar.New(l, args[1:]))
|
}, luar.New(l, args[1:]))
|
||||||
|
|
Loading…
Reference in New Issue