/** Returns the number of characters the synopsis heading will take on the same line as the synopsis. * @return the number of characters the synopsis heading will take on the same line as the synopsis. * @see #detailedSynopsis(int, Comparator, boolean) */ public int synopsisHeadingLength() { String[] lines = Ansi.OFF.new Text(commandSpec.usageMessage().synopsisHeading()).toString().split("\\r?\\n|\\r|%n", -1); return lines[lines.length - 1].length(); } /**
@Test public void testPalette236ColorForegroundRgb() { int num = 16 + 36 * 5 + 6 * 5 + 5; assertEquals("\u001B[38;5;" + num + "mabc\u001B[39m\u001B[0m", Ansi.ON.new Text("@|fg(5;5;5) abc|@").toString()); }
@Test public void testTextApply() { Ansi.Text txt = Ansi.ON.apply("--p", Arrays.<Ansi.IStyle>asList(Ansi.Style.fg_red, Ansi.Style.bold)); assertEquals(Ansi.ON.new Text("@|fg(red),bold --p|@"), txt); }
@Test public void testAnsiString() { String msg = "some text"; String markup = "@|bg(red),white,underline " + msg + "|@"; String ansiTxt = Help.Ansi.ON.string(markup); String ansiTxt2 = Help.Ansi.ON.new Text(markup).toString(); assertEquals(ansiTxt, ansiTxt2); }
@Test public void testAbreviatedSynopsis_withoutParameters_ANSI() { @Command(abbreviateSynopsis = true) class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}) int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.ON); assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [OPTIONS]" + LINESEP).toString(), help.synopsis(0)); }
@Test public void testUsageSubcommandGitStatus_ANSI() { String actual = usageString(new Demo.GitStatus(), Ansi.ON); assertEquals(Ansi.ON.new Text(String.format(Demo.EXPECTED_USAGE_GITSTATUS_ANSI)), actual); }
@Test public void testSynopsis_withSeparator_withLabeledParameters_ANSI() { @CommandLine.Command(separator = "=") class App { @CommandLine.Option(names = {"--verbose", "-v"}) boolean verbose; @CommandLine.Option(names = {"--count", "-c"}) int count; @CommandLine.Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; @CommandLine.Parameters(paramLabel = "FILE") File[] files; } CommandLine.Help help = new CommandLine.Help(new App(), Ansi.ON); assertEquals(Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@=@|italic <count>|@] [@|yellow FILE|@...]" + LINESEP), help.synopsis(0)); }
/** Returns a copy of this {@code Text} instance with the specified text concatenated to the end. Does not modify this instance! * @param string the text to concatenate to the end of this Text * @return a new Text instance * @since 3.0 */ public Text concat(String string) { return concat(new Text(string)); }
/** * Returns a new Text object for this Ansi mode, encapsulating the specified string * which may contain markup like {@code @|bg(red),white,underline some text|@}. * <p> * Calling {@code toString()} on the returned Text will either include ANSI escape codes * (if this Ansi mode is ON), or suppress ANSI escape codes (if this Ansi mode is OFF). * <p> * Equivalent to {@code this.new Text(stringWithMarkup)}. * @since 3.4 */ public Text text(String stringWithMarkup) { return this.new Text(stringWithMarkup); }
public Text[][] render(OptionSpec option, IParamLabelRenderer parameterLabelRenderer, ColorScheme scheme) { Text optionText = scheme.optionText(option.names()[0]); Text paramLabelText = parameterLabelRenderer.renderParameterLabel(option, scheme.ansi(), scheme.optionParamStyles); optionText = optionText.concat(paramLabelText); return new Text[][] {{ optionText, scheme.ansi().new Text(option.description().length == 0 ? "" : option.description()[0]) }}; } }
@Test public void testUsageSubcommandGitCommit_ANSI() { String actual = usageString(new Demo.GitCommit(), Ansi.ON); assertEquals(Ansi.ON.new Text(String.format(Demo.EXPECTED_USAGE_GITCOMMIT_ANSI)), actual); }
@Test public void testSynopsis_withSeparator_withLabeledRequiredParameters_ANSI() { @CommandLine.Command(separator = "=") class App { @CommandLine.Option(names = {"--verbose", "-v"}) boolean verbose; @CommandLine.Option(names = {"--count", "-c"}) int count; @CommandLine.Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; @CommandLine.Parameters(paramLabel = "FILE", arity = "1..*") File[] files; } CommandLine.Help help = new CommandLine.Help(new App(), Ansi.ON); assertEquals(Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@=@|italic <count>|@] @|yellow FILE|@..." + LINESEP), help.synopsis(0)); }
/** * Returns a String where any markup like * {@code @|bg(red),white,underline some text|@} is converted to ANSI escape codes * if this Ansi is ON, or suppressed if this Ansi is OFF. * <p> * Equivalent to {@code this.new Text(stringWithMarkup).toString()}. * @since 3.4 */ public String string(String stringWithMarkup) { return this.new Text(stringWithMarkup).toString(); }
@Test public void testTextSplitLinesEmpty() { Ansi ansi = Ansi.ON; Ansi.Text text = ansi.new Text("abc\n\n\n"); Ansi.Text[] lines = text.splitLines(); assertEquals(4, lines.length); assertEquals(ansi.new Text("abc"), lines[0]); assertEquals(ansi.new Text(""), lines[1]); assertEquals(ansi.new Text(""), lines[2]); assertEquals(ansi.new Text(""), lines[3]); } @Test
public Text[][] render(PositionalParamSpec param, IParamLabelRenderer parameterLabelRenderer, ColorScheme scheme) { return new Text[][] {{ parameterLabelRenderer.renderParameterLabel(param, scheme.ansi(), scheme.parameterStyles), scheme.ansi().new Text(param.description().length == 0 ? "" : param.description()[0]) }}; } }
@Test public void testSynopsis_optionalOptionArity0_1_withSpaceSeparator_ANSI() { @Command(separator = " ") class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}, arity = "0..1") int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.ON); assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@ [@|italic <count>|@]]" + LINESEP), help.synopsis(0)); }
@Test public void testTextDefaultColorScheme() { Ansi ansi = Ansi.ON; CommandLine.Help.ColorScheme scheme = CommandLine.Help.defaultColorScheme(ansi); assertEquals(scheme.ansi().new Text("@|yellow -p|@"), scheme.optionText("-p")); assertEquals(scheme.ansi().new Text("@|bold command|@"), scheme.commandText("command")); assertEquals(scheme.ansi().new Text("@|yellow FILE|@"), scheme.parameterText("FILE")); assertEquals(scheme.ansi().new Text("@|italic NUMBER|@"), scheme.optionParamText("NUMBER")); }