feat: add aliases interface (closes #87 and #86)

500th commit!
adds the `hilbish.aliases` interface
it allows you to add, delete and get all aliases

there was also a slight refactor with alias resolving in this commit,
there is a single resolve function instead of duplicate code.
with consideration this could be exported to lua side,
but i see no reason for it
windows-fixes
TorchedSammy 2021-12-14 20:54:23 -04:00
parent afa40dd1a1
commit bcc249a4bd
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
6 changed files with 105 additions and 42 deletions

92
aliases.go 100644
View File

@ -0,0 +1,92 @@
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))
}
return 1
}
func (h *hilbishAliases) luaDelete(L *lua.LState) int {
alias := L.CheckString(1)
h.Delete(alias)
return 0
}

View File

@ -28,7 +28,6 @@ func hilbishLoader(L *lua.LState) int {
host, _ := os.Hostname() host, _ := os.Hostname()
username := curuser.Username username := curuser.Username
// this will be baked into binary since GOOS is a constant
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
} }
@ -40,7 +39,9 @@ func hilbishLoader(L *lua.LState) int {
util.SetField(L, mod, "dataDir", lua.LString(dataDir), "Directory for Hilbish's data files") util.SetField(L, mod, "dataDir", lua.LString(dataDir), "Directory for Hilbish's data files")
util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell") util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell") util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")
util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
// hilbish.userDir table
hshuser := L.NewTable() hshuser := L.NewTable()
userConfigDir, _ := os.UserConfigDir() userConfigDir, _ := os.UserConfigDir()
userDataDir := "" userDataDir := ""
@ -57,7 +58,12 @@ func hilbishLoader(L *lua.LState) int {
util.Document(L, hshuser, "User directories to store configs and/or modules.") util.Document(L, hshuser, "User directories to store configs and/or modules.")
L.SetField(mod, "userDir", hshuser) L.SetField(mod, "userDir", hshuser)
util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.") // hilbish.aliases table
aliases = NewAliases()
aliasesModule := aliases.Loader(L)
util.Document(L, aliasesModule, "Alias inferface for Hilbish.")
L.SetField(mod, "aliases", aliasesModule)
L.Push(mod) L.Push(mod)
return 1 return 1

2
lua.go
View File

@ -129,7 +129,7 @@ func hshalias(L *lua.LState) int {
alias := L.CheckString(1) alias := L.CheckString(1)
source := L.CheckString(2) source := L.CheckString(2)
aliases[alias] = source aliases.Add(alias, source)
return 1 return 1
} }

View File

@ -24,7 +24,6 @@ var (
lr *lineReader lr *lineReader
commands = map[string]*lua.LFunction{} commands = map[string]*lua.LFunction{}
aliases = map[string]string{}
luaCompletions = map[string]*lua.LFunction{} luaCompletions = map[string]*lua.LFunction{}
confDir string confDir string

13
rl.go
View File

@ -35,18 +35,7 @@ func newLineReader(prompt string) *lineReader {
return []string{} return []string{}
} }
for aliases[fields[0]] != "" { ctx = aliases.Resolve(ctx)
alias := aliases[fields[0]]
ctx = alias + strings.TrimPrefix(ctx, fields[0])
fields = strings.Split(ctx, " ")
if aliases[fields[0]] == alias {
break
}
if aliases[fields[0]] != "" {
continue
}
}
if len(fields) == 1 { if len(fields) == 1 {
prefixes := []string{"./", "../", "/", "~/"} prefixes := []string{"./", "../", "/", "~/"}

View File

@ -16,21 +16,9 @@ import (
func runInput(input string) { func runInput(input string) {
running = true running = true
cmdArgs, cmdString := splitInput(input) cmdString := aliases.Resolve(input)
// If alias was found, use command alias // If alias was found, use command alias
for aliases[cmdArgs[0]] != "" {
alias := aliases[cmdArgs[0]]
cmdString = alias + strings.TrimPrefix(cmdString, cmdArgs[0])
cmdArgs, cmdString = splitInput(cmdString)
if aliases[cmdArgs[0]] == alias {
break
}
if aliases[cmdArgs[0]] != "" {
continue
}
}
hooks.Em.Emit("command.preexec", input, cmdString) hooks.Em.Emit("command.preexec", input, cmdString)
// First try to load input, essentially compiling to bytecode // First try to load input, essentially compiling to bytecode
@ -99,19 +87,8 @@ func execCommand(cmd string) error {
_, argstring := splitInput(strings.Join(args, " ")) _, argstring := splitInput(strings.Join(args, " "))
// If alias was found, use command alias // If alias was found, use command alias
for aliases[args[0]] != "" { argstring = aliases.Resolve(argstring)
alias := aliases[args[0]] args, _ = splitInput(argstring)
argstring = alias + strings.TrimPrefix(argstring, args[0])
cmdArgs, _ := splitInput(argstring)
args = cmdArgs
if aliases[args[0]] == alias {
break
}
if aliases[args[0]] != "" {
continue
}
}
// If command is defined in Lua then run it // If command is defined in Lua then run it
luacmdArgs := l.NewTable() luacmdArgs := l.NewTable()