Skip to content

Commit

Permalink
Merge pull request #106 from rsteube/fix-positionalany-callbackvalue
Browse files Browse the repository at this point in the history
fix callbackvalue for positionalany
  • Loading branch information
rsteube authored Nov 10, 2020
2 parents a2c962e + 285e2fa commit a2dd425
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions elvish/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ edit:completion:arg-completer[%v] = [@arg]{
fn _%v_callback [uid]{
# TODO there is no 'eval' in elvish and '-source' needs a file so use a tempary one for callback
tmpfile=(mktemp -t carapace_%v_callback-XXXXX.elv)
if (eq $arg[-1] "") {
arg[-1] = "''"
}
echo (str:join ' ' $arg) | xargs %v _carapace elvish $uid > $tmpfile
-source $tmpfile
rm $tmpfile
Expand Down
4 changes: 4 additions & 0 deletions example/cmd/multiparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func init() {
return carapace.ActionValuesDescribed("a", "first", "b", "second", "c", "third", "d", "fourth").Invoke(args).Filter(strings.Split(carapace.CallbackValue, "")).ToA()
}),
})

carapace.Gen(multipartsCmd).PositionalCompletion(ActionMultipartsTest(","))

carapace.Gen(multipartsCmd).PositionalAnyCompletion(ActionMultipartsTest("/"))
}

func ActionMultipartsTest(divider string) carapace.Action {
Expand Down
10 changes: 8 additions & 2 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ edit:completion:arg-completer[example] = [@arg]{
fn _example_callback [uid]{
# TODO there is no 'eval' in elvish and '-source' needs a file so use a tempary one for callback
tmpfile=(mktemp -t carapace_example_callback-XXXXX.elv)
if (eq $arg[-1] "") {
arg[-1] = "''"
}
echo (str:join ' ' $arg) | xargs example _carapace elvish $uid > $tmpfile
-source $tmpfile
rm $tmpfile
Expand Down Expand Up @@ -449,7 +452,9 @@ edit:complex-candidate 'invalid' &display='invalid' }]
[&long='slash' &desc='multiparts with / as divider' &arg-required=$true &completer=[_]{ _example_callback '_example__multiparts##slash' }]
]
arg-handlers = [
[_]{ _example_callback '_example__multiparts#1' }
[_]{ _example_callback '_example__multiparts#0' }
...
]
subargs = $arg[(subindex multiparts):]
if (> (count $subargs) 0) {
Expand Down Expand Up @@ -1348,7 +1353,8 @@ function _example__multiparts {
"--equals[multiparts with = as divider]: :{_example_callback '_example__multiparts##equals'}" \
"--none[multiparts without divider]: :{_example_callback '_example__multiparts##none'}" \
"--slash[multiparts with / as divider]: :{_example_callback '_example__multiparts##slash'}" \
"*::arg:->args"
"1: :{_example_callback '_example__multiparts#1'}" \
"*: :{_example_callback '_example__multiparts#0'}"
}
if compquote '' 2>/dev/null; then _example; else compdef _example example; fi
`
Expand Down
10 changes: 7 additions & 3 deletions uid/uid.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ func Value(cmd *cobra.Command, args []string, uid string) string {
} else if strings.Contains(uid, "#") {
split := strings.Split(uid, "#")
if index, err := strconv.Atoi(split[len(split)-1]); err == nil {
index = index - 1
if len(args)-1 >= index && index >= 0 {
return args[index]
if index > 0 {
index = index - 1
if len(args)-1 >= index && index >= 0 {
return args[index]
}
} else if len(args) > 0 && os.Args[len(os.Args)-1] != "" {
return args[len(args)-1]
}
}
}
Expand Down

0 comments on commit a2dd425

Please sign in to comment.