2021-12-15 00:54:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-09 17:51:58 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2021-12-15 00:54:23 +00:00
|
|
|
"strings"
|
2021-12-20 02:37:44 +00:00
|
|
|
"sync"
|
2021-12-15 00:54:23 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2021-12-15 00:54:23 +00:00
|
|
|
)
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
var aliases *aliasModule
|
2021-12-15 00:54:23 +00:00
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
type aliasModule struct {
|
2021-12-15 00:54:23 +00:00
|
|
|
aliases map[string]string
|
2021-12-20 02:37:44 +00:00
|
|
|
mu *sync.RWMutex
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize aliases map
|
2022-12-15 04:00:54 +00:00
|
|
|
func newAliases() *aliasModule {
|
|
|
|
return &aliasModule{
|
2021-12-15 00:54:23 +00:00
|
|
|
aliases: make(map[string]string),
|
2021-12-20 02:37:44 +00:00
|
|
|
mu: &sync.RWMutex{},
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
func (a *aliasModule) Add(alias, cmd string) {
|
2022-03-19 16:43:48 +00:00
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2021-12-20 02:37:44 +00:00
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
a.aliases[alias] = cmd
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
func (a *aliasModule) All() map[string]string {
|
2022-03-19 16:43:48 +00:00
|
|
|
return a.aliases
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
func (a *aliasModule) Delete(alias string) {
|
2022-03-19 16:43:48 +00:00
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2021-12-20 02:37:44 +00:00
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
delete(a.aliases, alias)
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
func (a *aliasModule) Resolve(cmdstr string) string {
|
2022-03-19 16:43:48 +00:00
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
2021-12-20 02:37:44 +00:00
|
|
|
|
2023-09-09 17:51:58 +00:00
|
|
|
arg, _ := regexp.Compile(`[\\]?%\d+`)
|
|
|
|
|
|
|
|
args, _ := splitInput(cmdstr)
|
2023-10-01 03:21:00 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
// this shouldnt reach but...????
|
2023-10-01 03:25:03 +00:00
|
|
|
return cmdstr
|
2023-10-01 03:21:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
for a.aliases[args[0]] != "" {
|
|
|
|
alias := a.aliases[args[0]]
|
2023-09-09 17:51:58 +00:00
|
|
|
alias = arg.ReplaceAllStringFunc(alias, func(a string) string {
|
|
|
|
idx, _ := strconv.Atoi(a[1:])
|
|
|
|
if strings.HasPrefix(a, "\\") || idx == 0 {
|
|
|
|
return strings.TrimPrefix(a, "\\")
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx + 1 > len(args) {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
val := args[idx]
|
|
|
|
args = cut(args, idx)
|
|
|
|
cmdstr = strings.Join(args, " ")
|
|
|
|
|
|
|
|
return val
|
|
|
|
})
|
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
|
|
|
cmdArgs, _ := splitInput(cmdstr)
|
|
|
|
args = cmdArgs
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
if a.aliases[args[0]] == alias {
|
2021-12-15 00:54:23 +00:00
|
|
|
break
|
|
|
|
}
|
2022-03-19 16:43:48 +00:00
|
|
|
if a.aliases[args[0]] != "" {
|
2021-12-15 00:54:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmdstr
|
|
|
|
}
|
|
|
|
|
|
|
|
// lua section
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface aliases
|
|
|
|
// command aliasing
|
|
|
|
// The alias interface deals with all command aliases in Hilbish.
|
|
|
|
func (a *aliasModule) Loader(rtm *rt.Runtime) *rt.Table {
|
2021-12-15 00:54:23 +00:00
|
|
|
// create a lua module with our functions
|
2022-04-04 10:40:02 +00:00
|
|
|
hshaliasesLua := map[string]util.LuaExport{
|
2024-07-19 20:54:15 +00:00
|
|
|
//"add": util.LuaExport{hlalias, 2, false},
|
2022-04-04 10:40:02 +00:00
|
|
|
"list": util.LuaExport{a.luaList, 0, false},
|
|
|
|
"del": util.LuaExport{a.luaDelete, 1, false},
|
2022-04-23 16:28:28 +00:00
|
|
|
"resolve": util.LuaExport{a.luaResolve, 1, false},
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
mod := rt.NewTable()
|
|
|
|
util.SetExports(rtm, mod, hshaliasesLua)
|
2021-12-15 00:54:23 +00:00
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface aliases
|
|
|
|
// add(alias, cmd)
|
2023-12-26 03:08:29 +00:00
|
|
|
// This is an alias (ha) for the [hilbish.alias](../#alias) function.
|
2022-12-15 04:00:54 +00:00
|
|
|
// --- @param alias string
|
|
|
|
// --- @param cmd string
|
|
|
|
func _hlalias() {}
|
|
|
|
|
|
|
|
// #interface aliases
|
2023-12-26 03:08:29 +00:00
|
|
|
// list() -> table[string, string]
|
2022-12-21 00:59:55 +00:00
|
|
|
// Get a table of all aliases, with string keys as the alias and the value as the command.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #returns table[string, string]
|
|
|
|
/*
|
|
|
|
#example
|
|
|
|
hilbish.aliases.add('hi', 'echo hi')
|
|
|
|
|
|
|
|
local aliases = hilbish.aliases.list()
|
|
|
|
-- -> {hi = 'echo hi'}
|
|
|
|
#example
|
|
|
|
*/
|
2022-12-15 04:00:54 +00:00
|
|
|
func (a *aliasModule) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-04-04 10:40:02 +00:00
|
|
|
aliasesList := rt.NewTable()
|
2022-03-19 16:43:48 +00:00
|
|
|
for k, v := range a.All() {
|
2022-04-04 10:40:02 +00:00
|
|
|
aliasesList.Set(rt.StringValue(k), rt.StringValue(v))
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.TableValue(aliasesList)), nil
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface aliases
|
|
|
|
// delete(name)
|
|
|
|
// Removes an alias.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param name string
|
2022-12-15 04:00:54 +00:00
|
|
|
func (a *aliasModule) luaDelete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-04-04 10:40:02 +00:00
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
alias, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-19 16:43:48 +00:00
|
|
|
a.Delete(alias)
|
2021-12-15 00:54:23 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
2022-04-23 16:28:28 +00:00
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface aliases
|
2023-12-26 03:08:29 +00:00
|
|
|
// resolve(alias) -> string?
|
|
|
|
// Resolves an alias to its original command. Will thrown an error if the alias doesn't exist.
|
|
|
|
// #param alias string
|
|
|
|
// #returns string
|
2022-12-15 04:00:54 +00:00
|
|
|
func (a *aliasModule) luaResolve(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-04-23 16:28:28 +00:00
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
alias, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resolved := a.Resolve(alias)
|
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(resolved)), nil
|
|
|
|
}
|