Add bonzai.Aliases with completion

pull/53/head v0.0.30
rwxrob 2 years ago
parent aec7bee85b
commit 026c2742e4
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -218,3 +218,12 @@ func Files(dir string) []string {
}
return maps.EscSpace(maps.Prefix(names, dir))
}
// Aliases allows Bonzai tree developers to create aliases (similar to
// shell aliases) that are directly translated into arguments to the
// Bonzai tree executable by overriding the os.Args in a controlled way.
// The value of an alias is always a slice of strings that will replace
// the os.Args[2:]. A slice is used (instead of a string parsed with
// strings.Fields) to ensure that hard-coded arguments containing
// whitespace are properly handled.
var Aliases = make(map[string][]string)

@ -11,6 +11,7 @@ import (
"github.com/rwxrob/bonzai/comp"
"github.com/rwxrob/bonzai/conf"
"github.com/rwxrob/fn/each"
"github.com/rwxrob/fn/maps"
"github.com/rwxrob/structs/qstack"
)
@ -81,13 +82,33 @@ func (x *Cmd) Run() {
x.cacheAliases()
// resolve bonzai.Aliases
if len(os.Args) > 2 {
args := []string{os.Args[0]}
alias := Aliases[os.Args[1]]
if alias != nil {
args = append(args, alias...)
args = append(args, os.Args[2:]...)
os.Args = args
}
}
// bash completion context
line := os.Getenv("COMP_LINE")
if line != "" {
cmd, args := x.Seek(ArgsFrom(line)[1:])
if cmd.Completer == nil {
list := comp.Standard(cmd, args...)
var list []string
lineargs := ArgsFrom(line)
// complete aliases first
if len(lineargs) > 1 {
list = append(list, maps.KeysWithPrefix(Aliases, lineargs[1])...)
}
cmd, args := x.Seek(lineargs[1:])
if cmd.Completer == nil {
list = append(list, comp.Standard(cmd, args...)...)
each.Println(list)
Exit()
}

@ -6,7 +6,7 @@ use (
../fs
../json
../config
../yaml2json
../y2j
../scan
../structs
../term

Loading…
Cancel
Save