From 23dd179349a76feb4e69bf93e26b921aecb6789f Mon Sep 17 00:00:00 2001 From: Simon Mavi Stewart Date: Mon, 18 Oct 2021 00:36:09 +0100 Subject: [PATCH] Fix the completion command's zsh completions 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. --- .../grid/commands/CompletionCommand.java | 16 +++++++++------- .../selenium/grid/commands/InfoCommand.java | 2 +- .../selenium/grid/node/docker/DockerFlags.java | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/commands/CompletionCommand.java b/java/src/org/openqa/selenium/grid/commands/CompletionCommand.java index 208d0daf7317f..45d4306b543a7 100644 --- a/java/src/org/openqa/selenium/grid/commands/CompletionCommand.java +++ b/java/src/org/openqa/selenium/grid/commands/CompletionCommand.java @@ -54,7 +54,7 @@ public String getDescription() { @Override public Set getConfigurableRoles() { - return ALL_ROLES; + return Collections.singleton(Role.of("completion")); } @Override @@ -114,10 +114,10 @@ private void outputZshCompletions(PrintStream out) { Map> 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' \\"); @@ -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(" )"); @@ -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))); @@ -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) { diff --git a/java/src/org/openqa/selenium/grid/commands/InfoCommand.java b/java/src/org/openqa/selenium/grid/commands/InfoCommand.java index 62477c6eaaf77..887aff0214929 100644 --- a/java/src/org/openqa/selenium/grid/commands/InfoCommand.java +++ b/java/src/org/openqa/selenium/grid/commands/InfoCommand.java @@ -50,7 +50,7 @@ public String getDescription() { @Override public Set getConfigurableRoles() { - return Collections.emptySet(); + return Collections.singleton(Role.of("info")); } @Override diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerFlags.java b/java/src/org/openqa/selenium/grid/node/docker/DockerFlags.java index 4673bb6035be1..ca7a292aaa33f 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerFlags.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerFlags.java @@ -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)