mirror of https://github.com/Hilbis/Hilbish
feat: allow substitution in aliases by numbered args
parent
caff604d95
commit
9e80b4bf50
20
aliases.go
20
aliases.go
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -46,9 +48,27 @@ func (a *aliasModule) Resolve(cmdstr string) string {
|
||||||
a.mu.RLock()
|
a.mu.RLock()
|
||||||
defer a.mu.RUnlock()
|
defer a.mu.RUnlock()
|
||||||
|
|
||||||
|
arg, _ := regexp.Compile(`[\\]?%\d+`)
|
||||||
|
|
||||||
args := strings.Split(cmdstr, " ")
|
args := strings.Split(cmdstr, " ")
|
||||||
for a.aliases[args[0]] != "" {
|
for a.aliases[args[0]] != "" {
|
||||||
alias := 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])
|
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
||||||
cmdArgs, _ := splitInput(cmdstr)
|
cmdArgs, _ := splitInput(cmdstr)
|
||||||
args = cmdArgs
|
args = cmdArgs
|
||||||
|
|
Loading…
Reference in New Issue