mirror of https://github.com/Hilbis/Hilbish
refactor: rename hilbishAliases to aliasHandler for clarity
parent
f433ab8a6f
commit
579a0cd0ce
62
aliases.go
62
aliases.go
|
@ -7,54 +7,54 @@ import (
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
var aliases *hilbishAliases
|
var aliases *aliasHandler
|
||||||
|
|
||||||
type hilbishAliases struct {
|
type aliasHandler struct {
|
||||||
aliases map[string]string
|
aliases map[string]string
|
||||||
mu *sync.RWMutex
|
mu *sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize aliases map
|
// initialize aliases map
|
||||||
func NewAliases() *hilbishAliases {
|
func newAliases() *aliasHandler {
|
||||||
return &hilbishAliases{
|
return &aliasHandler{
|
||||||
aliases: make(map[string]string),
|
aliases: make(map[string]string),
|
||||||
mu: &sync.RWMutex{},
|
mu: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hilbishAliases) Add(alias, cmd string) {
|
func (a *aliasHandler) Add(alias, cmd string) {
|
||||||
h.mu.Lock()
|
a.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer a.mu.Unlock()
|
||||||
|
|
||||||
h.aliases[alias] = cmd
|
a.aliases[alias] = cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hilbishAliases) All() map[string]string {
|
func (a *aliasHandler) All() map[string]string {
|
||||||
return h.aliases
|
return a.aliases
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hilbishAliases) Delete(alias string) {
|
func (a *aliasHandler) Delete(alias string) {
|
||||||
h.mu.Lock()
|
a.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer a.mu.Unlock()
|
||||||
|
|
||||||
delete(h.aliases, alias)
|
delete(a.aliases, alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hilbishAliases) Resolve(cmdstr string) string {
|
func (a *aliasHandler) Resolve(cmdstr string) string {
|
||||||
h.mu.RLock()
|
a.mu.RLock()
|
||||||
defer h.mu.RUnlock()
|
defer a.mu.RUnlock()
|
||||||
|
|
||||||
args := strings.Split(cmdstr, " ")
|
args := strings.Split(cmdstr, " ")
|
||||||
for h.aliases[args[0]] != "" {
|
for a.aliases[args[0]] != "" {
|
||||||
alias := h.aliases[args[0]]
|
alias := a.aliases[args[0]]
|
||||||
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
||||||
cmdArgs, _ := splitInput(cmdstr)
|
cmdArgs, _ := splitInput(cmdstr)
|
||||||
args = cmdArgs
|
args = cmdArgs
|
||||||
|
|
||||||
if h.aliases[args[0]] == alias {
|
if a.aliases[args[0]] == alias {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if h.aliases[args[0]] != "" {
|
if a.aliases[args[0]] != "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,12 +64,12 @@ func (h *hilbishAliases) Resolve(cmdstr string) string {
|
||||||
|
|
||||||
// lua section
|
// lua section
|
||||||
|
|
||||||
func (h *hilbishAliases) Loader(L *lua.LState) *lua.LTable {
|
func (a *aliasHandler) Loader(L *lua.LState) *lua.LTable {
|
||||||
// create a lua module with our functions
|
// create a lua module with our functions
|
||||||
hshaliasesLua := map[string]lua.LGFunction{
|
hshaliasesLua := map[string]lua.LGFunction{
|
||||||
"add": h.luaAdd,
|
"add": a.luaAdd,
|
||||||
"list": h.luaList,
|
"list": a.luaList,
|
||||||
"del": h.luaDelete,
|
"del": a.luaDelete,
|
||||||
}
|
}
|
||||||
|
|
||||||
mod := L.SetFuncs(L.NewTable(), hshaliasesLua)
|
mod := L.SetFuncs(L.NewTable(), hshaliasesLua)
|
||||||
|
@ -77,17 +77,17 @@ func (h *hilbishAliases) Loader(L *lua.LState) *lua.LTable {
|
||||||
return mod
|
return mod
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hilbishAliases) luaAdd(L *lua.LState) int {
|
func (a *aliasHandler) luaAdd(L *lua.LState) int {
|
||||||
alias := L.CheckString(1)
|
alias := L.CheckString(1)
|
||||||
cmd := L.CheckString(2)
|
cmd := L.CheckString(2)
|
||||||
h.Add(alias, cmd)
|
a.Add(alias, cmd)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hilbishAliases) luaList(L *lua.LState) int {
|
func (a *aliasHandler) luaList(L *lua.LState) int {
|
||||||
aliasesList := L.NewTable()
|
aliasesList := L.NewTable()
|
||||||
for k, v := range h.All() {
|
for k, v := range a.All() {
|
||||||
aliasesList.RawSetString(k, lua.LString(v))
|
aliasesList.RawSetString(k, lua.LString(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,9 +96,9 @@ func (h *hilbishAliases) luaList(L *lua.LState) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hilbishAliases) luaDelete(L *lua.LState) int {
|
func (a *aliasHandler) luaDelete(L *lua.LState) int {
|
||||||
alias := L.CheckString(1)
|
alias := L.CheckString(1)
|
||||||
h.Delete(alias)
|
a.Delete(alias)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue