feat: allow substitution in aliases by numbered args

pull/258/head
sammyette 2023-07-11 13:28:58 -04:00
parent caff604d95
commit 9e80b4bf50
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 24 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package main
import (
"regexp"
"strconv"
"strings"
"sync"
@ -46,9 +48,27 @@ func (a *aliasModule) Resolve(cmdstr string) string {
a.mu.RLock()
defer a.mu.RUnlock()
arg, _ := regexp.Compile(`[\\]?%\d+`)
args := strings.Split(cmdstr, " ")
for a.aliases[args[0]] != "" {
alias := a.aliases[args[0]]
alias = arg.ReplaceAllStringFunc(alias, func(a string) string {
if strings.HasPrefix(a, "\\") {
return strings.TrimPrefix(a, "\\")
}
idx, _ := strconv.Atoi(a[1:])
if idx > len(args) {
return a
}
val := args[idx]
args = cut(args, idx)
cmdstr = strings.Join(args, " ")
return val
})
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
cmdArgs, _ := splitInput(cmdstr)
args = cmdArgs

View File

@ -324,3 +324,7 @@ func getVersion() string {
return v.String()
}
func cut(slice []string, idx int) []string {
return append(slice[:idx], slice[idx + 1:]...)
}