Skip to content

Commit

Permalink
Fix the completion command's zsh completions
Browse files Browse the repository at this point in the history
It's now possible to do `source <(selenium completion zsh)` in zsh and
have that Do The Right Thing. You can also copy the output to a
`_selenium` completion file and have zsh load that properly to avoid
executing a command every time a shell starts.

We don't currently properly handle sub-sub commands (eg. `selenium
info tab-tab` won't produce any useful results), but this is a step in
the right direction.
  • Loading branch information
shs96c committed Oct 17, 2021
1 parent ee3ecbd commit 23dd179
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String getDescription() {

@Override
public Set<Role> getConfigurableRoles() {
return ALL_ROLES;
return Collections.singleton(Role.of("completion"));
}

@Override
Expand Down Expand Up @@ -114,10 +114,10 @@ private void outputZshCompletions(PrintStream out) {
Map<CliCommand, Set<DescribedOption>> allCommands = listKnownCommands();

// My kingdom for multiline strings
out.println("#compdef selenium");
out.println("local context state state_descr line");
out.println("#compdef _selenium selenium");
out.println("typeset -A opt_args");
out.println("_selenium() {");
out.println(" local context state state_descr line");
out.println(" _arguments -C \\");
out.println(" '(- :)--ext[Amend the classpath for Grid]: :->arg' \\");
out.println(" '(-): :->command' \\");
Expand All @@ -130,7 +130,7 @@ private void outputZshCompletions(PrintStream out) {
allCommands.keySet().stream()
.sorted(Comparator.comparing(CliCommand::getName))
.forEach(cmd -> {
out.println(String.format(" '%s:%s'", cmd.getName(), cmd.getDescription().replace("'", "\\'")));
out.println(String.format(" '%s:%s'", cmd.getName(), cmd.getDescription().replace("'", "'\\''")));
});

out.println(" )");
Expand Down Expand Up @@ -161,7 +161,11 @@ private void outputZshCompletions(PrintStream out) {
.filter(opt -> !opt.flags().isEmpty())
.sorted(Comparator.comparing(opt -> opt.flags().iterator().next()))
.forEach(opt -> {
String quotedDesc = opt.description.replace("'", "\\''").replace(":", "\\:");
String quotedDesc = opt.description().replace("'", "'\\''");
int index = quotedDesc.indexOf("\n");
if (index != -1) {
quotedDesc = quotedDesc.substring(0, index);
}

if (opt.flags().size() == 1) {
out.println(String.format(" '%s[%s]%s'", opt.flags().iterator().next(), quotedDesc, getZshType(opt)));
Expand All @@ -181,8 +185,6 @@ private void outputZshCompletions(PrintStream out) {
out.println(" _arguments $args && return");
out.println("}\n\n");
});

out.println("_selenium");
}

private String getZshType(DescribedOption option) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String getDescription() {

@Override
public Set<Role> getConfigurableRoles() {
return Collections.emptySet();
return Collections.singleton(Role.of("info"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class DockerFlags implements HasRoles {
@Parameter(
names = {"--docker", "-D"},
description = "Docker configs which map image name to stereotype capabilities (example " +
"`-D selenium/standalone-firefox:latest '{\"browserName\": \"firefox\"}')",
"`-D selenium/standalone-firefox:latest '{\"browserName\": \"firefox\"}'`)",
arity = 2,
variableArity = true,
splitter = NonSplittingSplitter.class)
Expand Down

0 comments on commit 23dd179

Please sign in to comment.