feat: implement aliases

lua5.4
TorchedSammy 2022-03-29 18:03:57 -04:00
parent 8cf101cee2
commit 183b22e565
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 65 additions and 36 deletions

View File

@ -4,7 +4,9 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/yuin/gopher-lua" "hilbish/util"
rt "github.com/arnodel/golua/runtime"
) )
var aliases *aliasHandler var aliases *aliasHandler
@ -64,41 +66,55 @@ func (a *aliasHandler) Resolve(cmdstr string) string {
// lua section // lua section
func (a *aliasHandler) Loader(L *lua.LState) *lua.LTable { func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
// create a lua module with our functions // create a lua module with our functions
hshaliasesLua := map[string]lua.LGFunction{ hshaliasesLua := map[string]util.LuaExport{
"add": a.luaAdd, "add": util.LuaExport{a.luaAdd, 2, false},
"list": a.luaList, "list": util.LuaExport{a.luaList, 0, false},
"del": a.luaDelete, "del": util.LuaExport{a.luaDelete, 1, false},
} }
mod := rt.NewTable()
mod := L.SetFuncs(L.NewTable(), hshaliasesLua) util.SetExports(rtm, mod, hshaliasesLua)
return mod return mod
} }
func (a *aliasHandler) luaAdd(L *lua.LState) int { func (a *aliasHandler) luaAdd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
alias := L.CheckString(1) if err := c.CheckNArgs(2); err != nil {
cmd := L.CheckString(2) return nil, err
a.Add(alias, cmd) }
alias, err := c.StringArg(0)
return 0 if err != nil {
} return nil, err
}
func (a *aliasHandler) luaList(L *lua.LState) int { cmd, err := c.StringArg(1)
aliasesList := L.NewTable() if err != nil {
for k, v := range a.All() { return nil, err
aliasesList.RawSetString(k, lua.LString(v))
} }
L.Push(aliasesList) a.Add(alias, cmd)
return 1 return c.Next(), nil
} }
func (a *aliasHandler) luaDelete(L *lua.LState) int { func (a *aliasHandler) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
alias := L.CheckString(1) aliasesList := rt.NewTable()
for k, v := range a.All() {
aliasesList.Set(rt.StringValue(k), rt.StringValue(v))
}
return c.PushingNext1(t.Runtime, rt.TableValue(aliasesList)), nil
}
func (a *aliasHandler) luaDelete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
alias, err := c.StringArg(0)
if err != nil {
return nil, err
}
a.Delete(alias) a.Delete(alias)
return 0 return c.Next(), nil
} }

34
api.go
View File

@ -22,8 +22,8 @@ import (
) )
var exports = map[string]util.LuaExport{ var exports = map[string]util.LuaExport{
"alias": util.LuaExport{hlalias, 2, false},
/* /*
"alias": hlalias,
"appendPath": hlappendPath, "appendPath": hlappendPath,
*/ */
"complete": util.LuaExport{hlcomplete, 2, false}, "complete": util.LuaExport{hlcomplete, 2, false},
@ -82,7 +82,8 @@ Check out the {blue}{bold}guide{reset} command to get started.
util.SetField(rtm, mod, "dataDir", rt.StringValue(dataDir), "Directory for Hilbish's data files") util.SetField(rtm, mod, "dataDir", rt.StringValue(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.SetField(rtm, mod, "greeting", rt.StringValue(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.") util.SetField(rtm, mod, "greeting", rt.StringValue(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.")
/*util.SetField(l, mod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)") /*util.SetField(l, mod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
util.SetField(l, hshMod, "exitCode", lua.LNumber(0), "Exit code of last exected command") util.SetField(l, hshMod, "exitCode", lua.LNumber(0), "Exit code of last exected command")
@ -111,11 +112,11 @@ Check out the {blue}{bold}guide{reset} command to get started.
// hilbish.aliases table // hilbish.aliases table
aliases = newAliases() aliases = newAliases()
/* aliasesModule := aliases.Loader(rtm)
aliasesModule := aliases.Loader(L) //util.Document(L, aliasesModule, "Alias inferface for Hilbish.")
util.Document(L, aliasesModule, "Alias inferface for Hilbish.") mod.Set(rt.StringValue("aliases"), rt.TableValue(aliasesModule))
L.SetField(mod, "aliases", aliasesModule)
/*
// hilbish.history table // hilbish.history table
historyModule := lr.Loader(L) historyModule := lr.Loader(L)
util.Document(L, historyModule, "History interface for Hilbish.") util.Document(L, historyModule, "History interface for Hilbish.")
@ -294,20 +295,31 @@ func hlmlprompt(L *lua.LState) int {
return 0 return 0
} }
*/
// alias(cmd, orig) // alias(cmd, orig)
// Sets an alias of `cmd` to `orig` // Sets an alias of `cmd` to `orig`
// --- @param cmd string // --- @param cmd string
// --- @param orig string // --- @param orig string
func hlalias(L *lua.LState) int { func hlalias(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
alias := L.CheckString(1) if err := c.CheckNArgs(2); err != nil {
source := L.CheckString(2) return nil, err
}
cmd, err := c.StringArg(0)
if err != nil {
return nil, err
}
orig, err := c.StringArg(1)
if err != nil {
return nil, err
}
aliases.Add(alias, source) aliases.Add(cmd, orig)
return 1 return c.Next(), nil
} }
/*
// appendPath(dir) // appendPath(dir)
// Appends `dir` to $PATH // Appends `dir` to $PATH
// --- @param dir string|table // --- @param dir string|table

View File

@ -23,6 +23,7 @@ func Document(L *lua.LState, module lua.LValue, doc string) {
// SetField sets a field in a table, adding docs for it. // SetField sets a field in a table, adding docs for it.
// It is accessible via the __docProp metatable. It is a table of the names of the fields. // It is accessible via the __docProp metatable. It is a table of the names of the fields.
func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, doc string) { func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, doc string) {
// TODO: ^ rtm isnt needed, i should remove it
mt := module.Metatable() mt := module.Metatable()
if mt == nil { if mt == nil {