mirror of https://github.com/Hilbis/Hilbish
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 itwindows-fixes
parent
afa40dd1a1
commit
bcc249a4bd
|
@ -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
|
||||
}
|
10
hilbish.go
10
hilbish.go
|
@ -28,7 +28,6 @@ func hilbishLoader(L *lua.LState) int {
|
|||
|
||||
host, _ := os.Hostname()
|
||||
username := curuser.Username
|
||||
// this will be baked into binary since GOOS is a constant
|
||||
if runtime.GOOS == "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, "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.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
|
||||
|
||||
// hilbish.userDir table
|
||||
hshuser := L.NewTable()
|
||||
userConfigDir, _ := os.UserConfigDir()
|
||||
userDataDir := ""
|
||||
|
@ -57,7 +58,12 @@ func hilbishLoader(L *lua.LState) int {
|
|||
util.Document(L, hshuser, "User directories to store configs and/or modules.")
|
||||
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)
|
||||
|
||||
return 1
|
||||
|
|
2
lua.go
2
lua.go
|
@ -129,7 +129,7 @@ func hshalias(L *lua.LState) int {
|
|||
alias := L.CheckString(1)
|
||||
source := L.CheckString(2)
|
||||
|
||||
aliases[alias] = source
|
||||
aliases.Add(alias, source)
|
||||
|
||||
return 1
|
||||
}
|
||||
|
|
1
main.go
1
main.go
|
@ -24,7 +24,6 @@ var (
|
|||
lr *lineReader
|
||||
|
||||
commands = map[string]*lua.LFunction{}
|
||||
aliases = map[string]string{}
|
||||
luaCompletions = map[string]*lua.LFunction{}
|
||||
|
||||
confDir string
|
||||
|
|
13
rl.go
13
rl.go
|
@ -35,18 +35,7 @@ func newLineReader(prompt string) *lineReader {
|
|||
return []string{}
|
||||
}
|
||||
|
||||
for aliases[fields[0]] != "" {
|
||||
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
|
||||
}
|
||||
}
|
||||
ctx = aliases.Resolve(ctx)
|
||||
|
||||
if len(fields) == 1 {
|
||||
prefixes := []string{"./", "../", "/", "~/"}
|
||||
|
|
29
shell.go
29
shell.go
|
@ -16,21 +16,9 @@ import (
|
|||
|
||||
func runInput(input string) {
|
||||
running = true
|
||||
cmdArgs, cmdString := splitInput(input)
|
||||
cmdString := aliases.Resolve(input)
|
||||
|
||||
// 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)
|
||||
|
||||
// First try to load input, essentially compiling to bytecode
|
||||
|
@ -99,19 +87,8 @@ func execCommand(cmd string) error {
|
|||
_, argstring := splitInput(strings.Join(args, " "))
|
||||
|
||||
// If alias was found, use command alias
|
||||
for aliases[args[0]] != "" {
|
||||
alias := aliases[args[0]]
|
||||
argstring = alias + strings.TrimPrefix(argstring, args[0])
|
||||
cmdArgs, _ := splitInput(argstring)
|
||||
args = cmdArgs
|
||||
|
||||
if aliases[args[0]] == alias {
|
||||
break
|
||||
}
|
||||
if aliases[args[0]] != "" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
argstring = aliases.Resolve(argstring)
|
||||
args, _ = splitInput(argstring)
|
||||
|
||||
// If command is defined in Lua then run it
|
||||
luacmdArgs := l.NewTable()
|
||||
|
|
Loading…
Reference in New Issue