2021-12-15 00:54:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-12-20 02:37:44 +00:00
|
|
|
"sync"
|
2021-12-15 00:54:23 +00:00
|
|
|
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
|
|
|
var aliases *hilbishAliases
|
|
|
|
|
|
|
|
type hilbishAliases struct {
|
|
|
|
aliases map[string]string
|
2021-12-20 02:37:44 +00:00
|
|
|
mu *sync.RWMutex
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize aliases map
|
|
|
|
func NewAliases() *hilbishAliases {
|
|
|
|
return &hilbishAliases{
|
|
|
|
aliases: make(map[string]string),
|
2021-12-20 02:37:44 +00:00
|
|
|
mu: &sync.RWMutex{},
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) Add(alias, cmd string) {
|
2021-12-20 02:37:44 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
h.aliases[alias] = cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) All() map[string]string {
|
|
|
|
return h.aliases
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) Delete(alias string) {
|
2021-12-20 02:37:44 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
delete(h.aliases, alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hilbishAliases) Resolve(cmdstr string) string {
|
2021-12-20 02:37:44 +00:00
|
|
|
h.mu.RLock()
|
|
|
|
defer h.mu.RUnlock()
|
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
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
|
|
|
|
}
|