/** @deprecated use {@link #concat(String)} instead */ @Deprecated public Text append(String string) { return concat(string); } /** @deprecated use {@link #concat(Text)} instead */
/** @deprecated use {@link #concat(Text)} instead */ @Deprecated public Text append(Text text) { return concat(text); }
/** Returns a {@code Text} object containing the command name and all aliases, separated with the specified separator. * Command names will use the {@link ColorScheme#commandText(String) command style} for the color scheme of this Help. * @since 3.9 */ public Text commandNamesText(String separator) { Text result = colorScheme().commandText(aliases().get(0)); for (int i = 1; i < aliases().size(); i++) { result = result.concat(separator).concat(colorScheme().commandText(aliases().get(i))); } return result; } private static String join(String[] names, int offset, int length, String separator) {
@SuppressWarnings("deprecation") @Test public void testTextAppendText() { Ansi ansi = Ansi.ON; Ansi.Text xyz = ansi.new Text("xyz"); assertEquals(ansi.new Text("a").append(xyz), ansi.new Text("a").concat(xyz)); }
/** 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)); }
optionText = optionText.concat(" ").concat(colorScheme.optionText(clusteredRequired.toString())); optionText = optionText.concat(" [").concat(colorScheme.optionText(clusteredOptional.toString())).concat("]"); Text param = parameterLabelRenderer().renderParameterLabel(option, colorScheme.ansi(), colorScheme.optionParamStyles); if (option.required()) { // e.g., -x=VAL optionText = optionText.concat(" ").concat(name).concat(param).concat(""); if (option.isMultiValue()) { // e.g., -x=VAL [-x=VAL]... optionText = optionText.concat(" [").concat(name).concat(param).concat("]..."); optionText = optionText.concat(" [").concat(name).concat(param).concat("]"); if (option.isMultiValue()) { // add ellipsis to show option is repeatable optionText = optionText.concat("...");
int paramCount = 1; if (unlimitedSplit) { repeating = paramName.concat("[" + split).concat(paramName).concat("...]"); paramCount++; mandatorySep = " "; result = result.concat(mandatorySep).concat(repeating); // " PARAM" or ",PARAM" paramCount += paramCount; result = result.concat(optionalSep).concat(paramName); // " [PARAM" or "[,PARAM" paramCount++; result = result.concat("]"); result = result.concat(optionalSep).concat(repeating).concat("]"); // PARAM[,PARAM]... result = ansi.new Text("(").concat(result).concat(")"); // repeating group result = result.concat("..."); // PARAM... result = ansi.new Text(sep2).concat(result).concat("]"); } else { result = ansi.new Text(optionSeparator).concat(result);
@Test public void testTextSubString() { Ansi ansi = Ansi.ON; Ansi.Text txt = ansi.new Text("@|bold 01234|@").concat("56").concat("@|underline 7890|@"); assertEquals(ansi.new Text("@|bold 01234|@56@|underline 7890|@"), txt.substring(0)); assertEquals(ansi.new Text("@|bold 1234|@56@|underline 7890|@"), txt.substring(1));
@Test public void testTextSplitLinesStartEndIntermediate() { Ansi ansi = Ansi.ON; Ansi.Text[] all = { ansi.new Text("\n@|bold 012\n\n\n34|@").concat("5\n\n\nAA\n\n\n6").concat("@|underline 78\n90|@\n"), ansi.new Text("\r@|bold 012\r\r\r34|@").concat("5\r\r\rAA\r\r\r6").concat("@|underline 78\r90|@\r"), ansi.new Text("\r\n@|bold 012\r\n\r\n\r\n34|@").concat("5\r\n\r\n\r\nAA\r\n\r\n\r\n6").concat("@|underline 78\r\n90|@\r\n"), }; for (Ansi.Text text : all) { Ansi.Text[] lines = text.splitLines(); int i = 0; assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text("@|bold 012|@"), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text("@|bold 34|@5"), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text("AA"), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text("6@|underline 78|@"), lines[i++]); assertEquals(ansi.new Text("@|underline 90|@"), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); } }
@Test public void testTextSplitLinesStartEnd() { Ansi ansi = Ansi.ON; Ansi.Text[] all = { ansi.new Text("\n@|bold 012\n34|@").concat("5\nAA\n6").concat("@|underline 78\n90|@\n"), ansi.new Text("\r@|bold 012\r34|@").concat("5\rAA\r6").concat("@|underline 78\r90|@\r"), ansi.new Text("\r\n@|bold 012\r\n34|@").concat("5\r\nAA\r\n6").concat("@|underline 78\r\n90|@\r\n"), }; for (Ansi.Text text : all) { Ansi.Text[] lines = text.splitLines(); int i = 0; assertEquals(ansi.new Text(""), lines[i++]); assertEquals(ansi.new Text("@|bold 012|@"), lines[i++]); assertEquals(ansi.new Text("@|bold 34|@5"), lines[i++]); assertEquals(ansi.new Text("AA"), lines[i++]); assertEquals(ansi.new Text("6@|underline 78|@"), lines[i++]); assertEquals(ansi.new Text("@|underline 90|@"), lines[i++]); assertEquals(ansi.new Text(""), lines[i++]); } } @Test
@Test public void testTextSplitLines() { Ansi ansi = Ansi.ON; Ansi.Text[] all = { ansi.new Text("@|bold 012\n34|@").concat("5\nAA\n6").concat("@|underline 78\n90|@"), ansi.new Text("@|bold 012\r34|@").concat("5\rAA\r6").concat("@|underline 78\r90|@"), ansi.new Text("@|bold 012\r\n34|@").concat("5\r\nAA\r\n6").concat("@|underline 78\r\n90|@"), }; for (Ansi.Text text : all) { Ansi.Text[] lines = text.splitLines(); int i = 0; assertEquals(ansi.new Text("@|bold 012|@"), lines[i++]); assertEquals(ansi.new Text("@|bold 34|@5"), lines[i++]); assertEquals(ansi.new Text("AA"), lines[i++]); assertEquals(ansi.new Text("6@|underline 78|@"), lines[i++]); assertEquals(ansi.new Text("@|underline 90|@"), lines[i++]); } } @Test
/** * Returns the detailed synopsis text by inserting the command name before the specified text with options and positional parameters details. * @param synopsisHeadingLength length of the synopsis heading string to be displayed on the same line as the first synopsis line. * For example, if the synopsis heading is {@code "Usage: "}, this value is 7. * @param optionsAndPositionalsAndCommandsDetails formatted string with options, positional parameters and subcommands. * Follows the unix convention of showing optional options and parameters in square brackets ({@code [ ]}). * @return the detailed synopsis text, in multiple lines if the length exceeds the usage width */ protected String insertSynopsisCommandName(int synopsisHeadingLength, Text optionsAndPositionalsAndCommandsDetails) { // Fix for #142: first line of synopsis overshoots max. characters String commandName = commandSpec.qualifiedName(); int firstColumnLength = commandName.length() + synopsisHeadingLength; // synopsis heading ("Usage: ") may be on the same line, so adjust column width TextTable textTable = TextTable.forColumnWidths(ansi(), firstColumnLength, width() - firstColumnLength); textTable.indentWrappedLines = 1; // don't worry about first line: options (2nd column) always start with a space // right-adjust the command name by length of synopsis heading Text PADDING = Ansi.OFF.new Text(stringOf('X', synopsisHeadingLength)); textTable.addRowValues(PADDING.concat(colorScheme.commandText(commandName)), optionsAndPositionalsAndCommandsDetails); return textTable.toString().substring(synopsisHeadingLength); // cut off leading synopsis heading spaces }
/** Returns a Text object containing a partial detailed synopsis showing only the positional parameters, starting with a {@code " "} space. * Follows the unix convention of showing optional options and parameters in square brackets ({@code [ ]}). * @return the formatted positional parameters, starting with a {@code " "} space, or an empty Text if this command has no positional parameters * @since 3.9 */ protected Text createDetailedSynopsisPositionalsText() { Text positionalParamText = ansi().new Text(0); for (PositionalParamSpec positionalParam : commandSpec.positionalParameters()) { if (!positionalParam.hidden()) { positionalParamText = positionalParamText.concat(" "); Text label = parameterLabelRenderer().renderParameterLabel(positionalParam, colorScheme.ansi(), colorScheme.parameterStyles); positionalParamText = positionalParamText.concat(label); } } return positionalParamText; }
private Text createLongOptionText(OptionSpec option, IParamLabelRenderer renderer, ColorScheme scheme, String longOption) { Text paramLabelText = renderer.renderParameterLabel(option, scheme.ansi(), scheme.optionParamStyles); // if no long option, fill in the space between the short option name and the param label value if (paramLabelText.length > 0 && longOption.length() == 0) { sep = renderer.separator(); // #181 paramLabelText may be =LABEL or [=LABEL...] int sepStart = paramLabelText.plainString().indexOf(sep); Text prefix = paramLabelText.substring(0, sepStart); paramLabelText = prefix.concat(paramLabelText.substring(sepStart + sep.length())); } Text longOptionText = scheme.optionText(longOption); longOptionText = longOptionText.concat(paramLabelText); return longOptionText; }
/** Generates a detailed synopsis message showing all options and parameters. Follows the unix convention of * showing optional options and parameters in square brackets ({@code [ ]}). * @param synopsisHeadingLength the length of the synopsis heading that will be displayed on the same line * @param optionSort comparator to sort options or {@code null} if options should not be sorted * @param clusterBooleanOptions {@code true} if boolean short options should be clustered into a single string * @return a detailed synopsis * @since 3.0 */ public String detailedSynopsis(int synopsisHeadingLength, Comparator<OptionSpec> optionSort, boolean clusterBooleanOptions) { Text optionText = createDetailedSynopsisOptionsText(optionSort, clusterBooleanOptions); Text positionalParamText = createDetailedSynopsisPositionalsText(); Text commandText = createDetailedSynopsisCommandText(); Text text = optionText.concat(positionalParamText).concat(commandText); return insertSynopsisCommandName(synopsisHeadingLength, text); }
/** 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)); }
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]) }}; } }
/** Returns a Text object containing a partial detailed synopsis showing only the subcommands, starting with a {@code " "} space. * Follows the unix convention of showing optional elements in square brackets ({@code [ ]}). * @return this implementation returns a hard-coded string {@code " [COMMAND]"} if this command has subcommands, an empty Text otherwise * @since 3.9 */ protected Text createDetailedSynopsisCommandText() { Text commandText = ansi().new Text(0); if (!commandSpec.subcommands().isEmpty()){ commandText = commandText.concat(" [") .concat("COMMAND") .concat("]"); } return commandText; }
@SuppressWarnings("deprecation") @Test public void testTextAppendText() { Ansi ansi = Ansi.ON; Ansi.Text xyz = ansi.new Text("xyz"); assertEquals(ansi.new Text("a").append(xyz), ansi.new Text("a").concat(xyz)); }
@SuppressWarnings("deprecation") @Test public void testTextAppendString() { Ansi ansi = Ansi.ON; assertEquals(ansi.new Text("a").append("xyz"), ansi.new Text("a").concat("xyz")); }