Unique wraps a completion func and removes completion results that are already consumed (i.e., appear in "args"). For example: # initial completion: args is empty, so all results are shown command one two three # "one" is already used so omitted command one two three
(fn cobra.CompletionFunc)
| 237 | // command one <tab> |
| 238 | // two three |
| 239 | func Unique(fn cobra.CompletionFunc) cobra.CompletionFunc { |
| 240 | return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 241 | all, dir := fn(cmd, args, toComplete) |
| 242 | if len(all) == 0 || len(args) == 0 { |
| 243 | return all, dir |
| 244 | } |
| 245 | |
| 246 | alreadyCompleted := make(map[string]struct{}, len(args)) |
| 247 | for _, a := range args { |
| 248 | alreadyCompleted[a] = struct{}{} |
| 249 | } |
| 250 | |
| 251 | out := make([]string, 0, len(all)) |
| 252 | for _, c := range all { |
| 253 | if _, ok := alreadyCompleted[c]; !ok { |
| 254 | out = append(out, c) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return out, dir |
| 259 | } |
| 260 | } |
no outgoing calls
searching dependent graphs…