2021-12-15 00:54:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
|
|
|
var aliases *hilbishAliases
|
|
|
|
|
|
|
|
type hilbishAliases struct {
|
|
|
|
aliases map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize aliases map
|
|
|
|
func NewAliases() *hilbishAliases {
|
|
|
|
return &hilbishAliases{
|
|
|
|
aliases: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) Add(alias, cmd string) {
|
|
|
|
h.aliases[alias] = cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) All() map[string]string {
|
|
|
|
return h.aliases
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) Delete(alias string) {
|
|
|
|
delete(h.aliases, alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) Resolve(cmdstr string) string {
|
|
|
|
args := strings.Split(cmdstr, " ")
|
|
|
|
for h.aliases[args[0]] != "" {
|
|
|
|
alias := h.aliases[args[0]]
|
|
|
|
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
|
|
|
cmdArgs, _ := splitInput(cmdstr)
|
|
|
|
args = cmdArgs
|
|
|
|
|
|
|
|
if h.aliases[args[0]] == alias {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if h.aliases[args[0]] != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmdstr
|
|
|
|
}
|
|
|
|
|
|
|
|
// lua section
|
|
|
|
|
|
|
|
func (h *hilbishAliases) Loader(L *lua.LState) *lua.LTable {
|
|
|
|
// create a lua module with our functions
|
|
|
|
hshaliasesLua := map[string]lua.LGFunction{
|
|
|
|
"add": h.luaAdd,
|
|
|
|
"list": h.luaList,
|
|
|
|
"del": h.luaDelete,
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := L.SetFuncs(L.NewTable(), hshaliasesLua)
|
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) luaAdd(L *lua.LState) int {
|
|
|
|
alias := L.CheckString(1)
|
|
|
|
cmd := L.CheckString(2)
|
|
|
|
h.Add(alias, cmd)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) luaList(L *lua.LState) int {
|
|
|
|
aliasesList := L.NewTable()
|
|
|
|
for k, v := range h.All() {
|
|
|
|
aliasesList.RawSetString(k, lua.LString(v))
|
|
|
|
}
|
|
|
|
|
2021-12-19 16:54:15 +00:00
|
|
|
L.Push(aliasesList)
|
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) luaDelete(L *lua.LState) int {
|
|
|
|
alias := L.CheckString(1)
|
|
|
|
h.Delete(alias)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|