/** Copies the text representation that we built up from the options into the specified StringBuilder. * @param text the StringBuilder to write into * @return the specified StringBuilder object (to allow method chaining and a more fluid API) */ public StringBuilder toString(StringBuilder text) { int columnCount = this.columns.length; StringBuilder row = new StringBuilder(tableWidth); for (int i = 0; i < columnValues.size(); i++) { Text column = columnValues.get(i); row.append(column.toString()); row.append(new String(spaces(columns[i % columnCount].width - column.length))); if (i % columnCount == columnCount - 1) { int lastChar = row.length() - 1; while (lastChar >= 0 && row.charAt(lastChar) == ' ') {lastChar--;} // rtrim row.setLength(lastChar + 1); text.append(row.toString()).append(System.getProperty("line.separator")); row.setLength(0); } } return text; } public String toString() { return toString(new StringBuilder()).toString(); }
public boolean equals(Object obj) { return toString().equals(String.valueOf(obj)); } public int hashCode() { return toString().hashCode(); }
private int unindent(Text[] values) { if (columns.length <= LONG_OPTION_COLUMN) { return 0; } int oldIndent = columns[LONG_OPTION_COLUMN].indent; if ("=".equals(values[OPTION_SEPARATOR_COLUMN].toString())) { columns[LONG_OPTION_COLUMN].indent = 0; } return oldIndent; } private void reindent(int oldIndent) {
@Test public void testUsageWithCustomColorScheme() throws UnsupportedEncodingException { CommandLine.Help.ColorScheme scheme = new CommandLine.Help.ColorScheme(Ansi.ON) .options(Ansi.Style.bg_magenta).parameters(Ansi.Style.bg_cyan).optionParams(Ansi.Style.bg_yellow).commands(Ansi.Style.reverse); class Args { @CommandLine.Parameters(description = "param desc") String[] params; @CommandLine.Option(names = "-x", description = "option desc") String[] options; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); CommandLine.usage(new Args(), new PrintStream(baos, true, "UTF8"), scheme); String actual = baos.toString("UTF8"); String expected = String.format("" + "Usage: @|reverse <main class>|@ [@|bg_magenta -x|@=@|bg_yellow <options>|@]... [@|bg_cyan <params>|@...]%n" + " [@|bg_cyan <params>|@...] param desc%n" + " @|bg_magenta -x|@=@|bg_yellow <|@@|bg_yellow options>|@ option desc%n"); assertEquals(Ansi.ON.new Text(expected).toString(), actual); }
public int hashCode() { return toString().hashCode(); }
@Test public void testDefaultParameterRenderer_appliesToPositionalArgumentsIgnoresSeparator() { class WithLabel { @Parameters(paramLabel = "POSITIONAL_ARGS") String positional; } class WithoutLabel { @Parameters() String positional; } Help withLabel = new Help(new WithLabel()); Help.IParamLabelRenderer equals = withLabel.createDefaultParamLabelRenderer(); withLabel.commandSpec().parser().separator("="); Help.IParamLabelRenderer spaced = withLabel.createDefaultParamLabelRenderer(); Text withSpace = spaced.renderParameterLabel(withLabel.positionalParameters().get(0), withLabel.ansi(), Collections.<IStyle>emptyList()); assertEquals(withSpace.toString(), "POSITIONAL_ARGS", withSpace.toString()); Text withEquals = equals.renderParameterLabel(withLabel.positionalParameters().get(0), withLabel.ansi(), Collections.<IStyle>emptyList()); assertEquals(withEquals.toString(), "POSITIONAL_ARGS", withEquals.toString()); Help withoutLabel = new Help(new WithoutLabel()); withSpace = spaced.renderParameterLabel(withoutLabel.positionalParameters().get(0), withoutLabel.ansi(), Collections.<IStyle>emptyList()); assertEquals(withSpace.toString(), "<positional>", withSpace.toString()); withEquals = equals.renderParameterLabel(withoutLabel.positionalParameters().get(0), withoutLabel.ansi(), Collections.<IStyle>emptyList()); assertEquals(withEquals.toString(), "<positional>", withEquals.toString()); }
@Test public void testTextConstructorWithStyle() { assertEquals("\u001B[1m--NoAnsiFormat\u001B[21m\u001B[0m", Ansi.ON.new Text("@|bold --NoAnsiFormat|@").toString()); }
@Test public void testPalette236ColorBackgroundIndex() { assertEquals("\u001B[48;5;77mabc\u001B[49m\u001B[0m", Ansi.ON.new Text("@|bg(77) abc|@").toString()); }
@Test public void testCommandAliasAnnotationSubcommandUsageHelp() { CommandLine commandLine = new CommandLine(new TopLevelCommand()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); commandLine.getSubcommands().get("sub").usage(new PrintStream(baos), CommandLine.Help.defaultColorScheme(Help.Ansi.ON).commands(Help.Ansi.Style.underline)); // add underline String expected = Help.Ansi.ON.new Text(String.format("" + "Usage: @|bold,underline top sub|@ [COMMAND]%n" + "I'm subcommand No. 1!%n" + "Commands:%n" + " @|bold,underline subsub|@, @|bold,underline ss|@, @|bold,underline sbsb|@ I'm like a 3rd rate command but great bang for your buck%n")).toString(); assertEquals(expected, baos.toString()); } }
@Test public void testPalette236ColorForegroundIndex() { assertEquals("\u001B[38;5;45mabc\u001B[39m\u001B[0m", Ansi.ON.new Text("@|fg(45) abc|@").toString()); }
@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 testTextAdjacentStyles() { assertEquals("\u001B[3m<commit\u001B[23m\u001B[0m\u001B[3m>\u001B[23m\u001B[0m%n", Ansi.ON.new Text("@|italic <commit|@@|italic >|@%n").toString()); }
@Test public void testTextWithMultipleStyledSections() { assertEquals("\u001B[1m<main class>\u001B[21m\u001B[0m [\u001B[33m-v\u001B[39m\u001B[0m] [\u001B[33m-c\u001B[39m\u001B[0m [\u001B[3m<count>\u001B[23m\u001B[0m]]", Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@ [@|italic <count>|@]]").toString()); }
@Test public void testCommandAliasAnnotationUsageHelp() { CommandLine commandLine = new CommandLine(new TopLevelCommand()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); commandLine.usage(new PrintStream(baos), CommandLine.Help.defaultColorScheme(Help.Ansi.ON).commands(Help.Ansi.Style.underline)); // add underline String expected = Help.Ansi.ON.new Text(String.format("" + "Usage: @|bold,underline top|@ [COMMAND]%n" + "top level command%n" + "Commands:%n" + " @|bold,underline sub|@, @|bold,underline s|@, @|bold,underline sb|@ I'm subcommand No. 1!%n" + " @|bold,underline sub2|@, @|bold,underline s2|@, @|bold,underline sb2|@ I'm subcommand 2 but pretty good still%n")).toString(); assertEquals(expected, baos.toString()); }
/** * 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 testHelpCreateDetailedSynopsisOptionsText() { Help help = new Help(CommandSpec.create().addOption(OptionSpec.builder("xx").build()), new ColorScheme(Help.Ansi.OFF)); Text text = help.createDetailedSynopsisOptionsText(null, true); assertEquals(" [xx]", text.toString()); }
@Test public void testTextConstructorPlain() { assertEquals("--NoAnsiFormat", Ansi.ON.new Text("--NoAnsiFormat").toString()); }
/** 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(); } /**