mirror of https://github.com/Hilbis/Hilbish
feat: implement aliases
parent
8cf101cee2
commit
183b22e565
58
aliases.go
58
aliases.go
|
@ -4,7 +4,9 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/yuin/gopher-lua"
|
||||
"hilbish/util"
|
||||
|
||||
rt "github.com/arnodel/golua/runtime"
|
||||
)
|
||||
|
||||
var aliases *aliasHandler
|
||||
|
@ -64,41 +66,55 @@ func (a *aliasHandler) Resolve(cmdstr string) string {
|
|||
|
||||
// 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
|
||||
hshaliasesLua := map[string]lua.LGFunction{
|
||||
"add": a.luaAdd,
|
||||
"list": a.luaList,
|
||||
"del": a.luaDelete,
|
||||
hshaliasesLua := map[string]util.LuaExport{
|
||||
"add": util.LuaExport{a.luaAdd, 2, false},
|
||||
"list": util.LuaExport{a.luaList, 0, false},
|
||||
"del": util.LuaExport{a.luaDelete, 1, false},
|
||||
}
|
||||
|
||||
mod := L.SetFuncs(L.NewTable(), hshaliasesLua)
|
||||
mod := rt.NewTable()
|
||||
util.SetExports(rtm, mod, hshaliasesLua)
|
||||
|
||||
return mod
|
||||
}
|
||||
|
||||
func (a *aliasHandler) luaAdd(L *lua.LState) int {
|
||||
alias := L.CheckString(1)
|
||||
cmd := L.CheckString(2)
|
||||
func (a *aliasHandler) luaAdd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
alias, err := c.StringArg(0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd, err := c.StringArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
a.Add(alias, cmd)
|
||||
|
||||
return 0
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func (a *aliasHandler) luaList(L *lua.LState) int {
|
||||
aliasesList := L.NewTable()
|
||||
func (a *aliasHandler) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
aliasesList := rt.NewTable()
|
||||
for k, v := range a.All() {
|
||||
aliasesList.RawSetString(k, lua.LString(v))
|
||||
aliasesList.Set(rt.StringValue(k), rt.StringValue(v))
|
||||
}
|
||||
|
||||
L.Push(aliasesList)
|
||||
|
||||
return 1
|
||||
return c.PushingNext1(t.Runtime, rt.TableValue(aliasesList)), nil
|
||||
}
|
||||
|
||||
func (a *aliasHandler) luaDelete(L *lua.LState) int {
|
||||
alias := L.CheckString(1)
|
||||
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)
|
||||
|
||||
return 0
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
|
38
api.go
38
api.go
|
@ -22,8 +22,8 @@ import (
|
|||
)
|
||||
|
||||
var exports = map[string]util.LuaExport{
|
||||
"alias": util.LuaExport{hlalias, 2, false},
|
||||
/*
|
||||
"alias": hlalias,
|
||||
"appendPath": hlappendPath,
|
||||
*/
|
||||
"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(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(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")
|
||||
|
@ -111,11 +112,11 @@ Check out the {blue}{bold}guide{reset} command to get started.
|
|||
|
||||
// hilbish.aliases table
|
||||
aliases = newAliases()
|
||||
/*
|
||||
aliasesModule := aliases.Loader(L)
|
||||
util.Document(L, aliasesModule, "Alias inferface for Hilbish.")
|
||||
L.SetField(mod, "aliases", aliasesModule)
|
||||
aliasesModule := aliases.Loader(rtm)
|
||||
//util.Document(L, aliasesModule, "Alias inferface for Hilbish.")
|
||||
mod.Set(rt.StringValue("aliases"), rt.TableValue(aliasesModule))
|
||||
|
||||
/*
|
||||
// hilbish.history table
|
||||
historyModule := lr.Loader(L)
|
||||
util.Document(L, historyModule, "History interface for Hilbish.")
|
||||
|
@ -294,20 +295,31 @@ func hlmlprompt(L *lua.LState) int {
|
|||
|
||||
return 0
|
||||
}
|
||||
*/
|
||||
|
||||
// alias(cmd, orig)
|
||||
// Sets an alias of `cmd` to `orig`
|
||||
// --- @param cmd string
|
||||
// --- @param orig string
|
||||
func hlalias(L *lua.LState) int {
|
||||
alias := L.CheckString(1)
|
||||
source := L.CheckString(2)
|
||||
|
||||
aliases.Add(alias, source)
|
||||
|
||||
return 1
|
||||
func hlalias(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
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(cmd, orig)
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
/*
|
||||
// appendPath(dir)
|
||||
// Appends `dir` to $PATH
|
||||
// --- @param dir string|table
|
||||
|
|
|
@ -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.
|
||||
// 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) {
|
||||
// TODO: ^ rtm isnt needed, i should remove it
|
||||
mt := module.Metatable()
|
||||
|
||||
if mt == nil {
|
||||
|
|
Loading…
Reference in New Issue