(commands, hosts, hostnames, ips, ids []string)
| 258 | } |
| 259 | |
| 260 | func setupLiner(commands, hosts, hostnames, ips, ids []string) *liner.State { |
| 261 | l := liner.NewLiner() |
| 262 | l.SetCtrlCAborts(true) |
| 263 | |
| 264 | l.SetCompleter(func(line string) []string { |
| 265 | line = strings.TrimSpace(line) |
| 266 | var out []string |
| 267 | if line == "" { |
| 268 | out = append(out, commands...) |
| 269 | out = append(out, hosts...) |
| 270 | out = append(out, hostnames...) |
| 271 | out = append(out, ips...) |
| 272 | out = append(out, ids...) |
| 273 | return out |
| 274 | } |
| 275 | // Tab completion for commands that take host arguments |
| 276 | for _, cmdPrefix := range []string{"connect ", "edit ", "delete ", "show ", "info "} { |
| 277 | if strings.HasPrefix(line, cmdPrefix) { |
| 278 | p := strings.TrimSpace(line[len(cmdPrefix):]) |
| 279 | for _, h := range hosts { |
| 280 | if strings.HasPrefix(h, p) { |
| 281 | out = append(out, cmdPrefix+h) |
| 282 | } |
| 283 | } |
| 284 | for _, h := range hostnames { |
| 285 | if strings.HasPrefix(h, p) { |
| 286 | out = append(out, cmdPrefix+h) |
| 287 | } |
| 288 | } |
| 289 | for _, ip := range ips { |
| 290 | if strings.HasPrefix(ip, p) { |
| 291 | out = append(out, cmdPrefix+ip) |
| 292 | } |
| 293 | } |
| 294 | for _, id := range ids { |
| 295 | if strings.HasPrefix(id, p) { |
| 296 | out = append(out, cmdPrefix+id) |
| 297 | } |
| 298 | } |
| 299 | return out |
| 300 | } |
| 301 | } |
| 302 | // Tab completion for global subcommands |
| 303 | if strings.HasPrefix(line, "global ") { |
| 304 | subCmd := strings.TrimSpace(line[7:]) |
| 305 | globalSubCmds := []string{"show", "edit", "set", "unset"} |
| 306 | |
| 307 | // Complete subcommand |
| 308 | if !strings.Contains(subCmd, " ") { |
| 309 | for _, sc := range globalSubCmds { |
| 310 | if strings.HasPrefix(sc, subCmd) { |
| 311 | out = append(out, "global "+sc) |
| 312 | } |
| 313 | } |
| 314 | return out |
| 315 | } |
| 316 | |
| 317 | // Complete option names for "global set" and "global unset" |
no test coverage detected