Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions example/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package opts

import (
"reflect"
"strings"

"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/net/ssh"
Expand Down Expand Up @@ -32,13 +31,18 @@ type Machine string

// Complete provides user@host completions.
func (m *Machine) Complete(ctx carapace.Context) carapace.Action {
if strings.Contains(ctx.Value, "@") {
prefix := strings.SplitN(ctx.Value, "@", 2)[0]

return ssh.ActionHosts().Invoke(ctx).Prefix(prefix + "@").ToA()
} else {
return os.ActionUsers().Suffix("@").NoSpace('@')
}
action := carapace.ActionMultiParts("@", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return os.ActionUsers().Invoke(ctx).Suffix("@").ToA().NoSpace('@')
case 1:
return ssh.ActionHosts()
default:
return carapace.ActionValues()
}
})

return action
}

func (m *Machine) String() string {
Expand Down
3 changes: 1 addition & 2 deletions internal/completions/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func getCompletionAction(name, value, desc string) carapace.Action {
// it then checks the slice's element type.
func typeCompleter(val reflect.Value) (carapace.CompletionCallback, bool, bool) {
var callback carapace.CompletionCallback
isRepeatable := (val.Type().Kind() == reflect.Slice)
isRepeatable := val.Type().Kind() == reflect.Slice || val.Type().Kind() == reflect.Map
itemsImplement := false

// Always check that the type itself does implement, even if
Expand All @@ -117,7 +117,6 @@ func typeCompleter(val reflect.Value) (carapace.CompletionCallback, bool, bool)
itemsImplement = true
callback = impl.Complete
} else if val.CanAddr() {
isRepeatable = true
if impl, ok := val.Addr().Interface().(interfaces.Completer); ok && impl != nil {
itemsImplement = true
callback = impl.Complete
Expand Down
2 changes: 0 additions & 2 deletions internal/gen/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,12 @@ func buildFlagCompleter(flag *parser.Flag, opts *parser.Opts) (carapace.Action,
// Then, and irrespectively of where the completer comes from,
// we adapt it considering the kind of type we're dealing with.
if isRepeatable {

// List separator
separator := ","
if flag.Separator != nil && *flag.Separator != "none" {
separator = *flag.Separator
}
action = action.UniqueList(separator)

}

return action, true
Expand Down
Loading