/** Returns a 2-column list with command names and the first line of their header or (if absent) description. * @return a usage help section describing the added commands */ public String commandList() { if (subcommands().isEmpty()) { return ""; } int commandLength = maxLength(subcommands().keySet()); Help.TextTable textTable = Help.TextTable.forColumns(ansi(), new Help.Column(commandLength + 2, 2, Help.Column.Overflow.SPAN), new Help.Column(width() - (commandLength + 2), 2, Help.Column.Overflow.WRAP)); for (Map.Entry<String, Help> entry : subcommands().entrySet()) { Help help = entry.getValue(); UsageMessageSpec usage = help.commandSpec().usageMessage(); String header = !empty(usage.header()) ? usage.header()[0] : (!empty(usage.description()) ? usage.description()[0] : ""); Text[] lines = ansi().text(format(header)).splitLines(); for (int i = 0; i < lines.length; i++) { textTable.addRowValues(i == 0 ? help.commandNamesText(", ") : Ansi.EMPTY_TEXT, lines[i]); } } return textTable.toString(); } private static int maxLength(Collection<String> any) {
@Test public void testCreateDescriptionFirstLines() throws Exception { Method m = Help.class.getDeclaredMethod("createDescriptionFirstLines", Help.ColorScheme.class, Model.ArgSpec.class, String[].class, boolean[].class); m.setAccessible(true); String[][] input = new String[][] { new String[0], new String[] {""}, new String[] {"a", "b", "c"} }; Help.Ansi.Text[][] expectedOutput = new Help.Ansi.Text[][] { new Help.Ansi.Text[] {Help.Ansi.OFF.text("")}, new Help.Ansi.Text[] {Help.Ansi.OFF.text("")}, new Help.Ansi.Text[] {Help.Ansi.OFF.text("a"), Help.Ansi.OFF.text("b"), Help.Ansi.OFF.text("c")} }; for (int i = 0; i < input.length; i++) { String[] description = input[i]; Help.Ansi.Text[] result = (Help.Ansi.Text[]) m.invoke(null, new Help.ColorScheme(Help.Ansi.OFF), null, description, new boolean[3]); Help.Ansi.Text[] expected = expectedOutput[i]; for (int j = 0; j < result.length; j++) { assertEquals(expected[j], result[j]); } } }
@Test public void testTextTablePutValue_DisallowsInvalidRowIndex() { TextTable tt = new TextTable(Help.Ansi.OFF, new Help.Column[] {new Help.Column(30, 2, Help.Column.Overflow.SPAN)}); try { tt.putValue(1, 0, Help.Ansi.OFF.text("abc")); } catch (IllegalArgumentException ex) { assertEquals("Cannot write to row 1: rowCount=0", ex.getMessage()); } }
/** Returns a 2-column list with command names and the first line of their header or (if absent) description. * @return a usage help section describing the added commands */ public String commandList() { if (subcommands().isEmpty()) { return ""; } int commandLength = maxLength(subcommands().keySet()); Help.TextTable textTable = Help.TextTable.forColumns(ansi(), new Help.Column(commandLength + 2, 2, Help.Column.Overflow.SPAN), new Help.Column(width() - (commandLength + 2), 2, Help.Column.Overflow.WRAP)); for (Map.Entry<String, Help> entry : subcommands().entrySet()) { Help help = entry.getValue(); UsageMessageSpec usage = help.commandSpec().usageMessage(); String header = !empty(usage.header()) ? usage.header()[0] : (!empty(usage.description()) ? usage.description()[0] : ""); Text[] lines = ansi().text(format(header)).splitLines(); for (int i = 0; i < lines.length; i++) { textTable.addRowValues(i == 0 ? help.commandNamesText(", ") : Ansi.EMPTY_TEXT, lines[i]); } } return textTable.toString(); } private static int maxLength(Collection<String> any) {
@Test public void testCreateDescriptionFirstLines() throws Exception { Method m = Help.class.getDeclaredMethod("createDescriptionFirstLines", Help.ColorScheme.class, Model.ArgSpec.class, String[].class, boolean[].class); m.setAccessible(true); String[][] input = new String[][] { new String[0], new String[] {""}, new String[] {"a", "b", "c"} }; Help.Ansi.Text[][] expectedOutput = new Help.Ansi.Text[][] { new Help.Ansi.Text[] {Help.Ansi.OFF.text("")}, new Help.Ansi.Text[] {Help.Ansi.OFF.text("")}, new Help.Ansi.Text[] {Help.Ansi.OFF.text("a"), Help.Ansi.OFF.text("b"), Help.Ansi.OFF.text("c")} }; for (int i = 0; i < input.length; i++) { String[] description = input[i]; Help.Ansi.Text[] result = (Help.Ansi.Text[]) m.invoke(null, new Help.ColorScheme(Help.Ansi.OFF), null, description, new boolean[3]); Help.Ansi.Text[] expected = expectedOutput[i]; for (int j = 0; j < result.length; j++) { assertEquals(expected[j], result[j]); } } }
@Test public void testAnsiText() { String markup = "@|bg(red),white,underline some text|@"; Help.Ansi.Text txt = Help.Ansi.ON.text(markup); Help.Ansi.Text txt2 = Help.Ansi.ON.new Text(markup); assertEquals(txt, txt2); }
@Test public void testTextTablePutValue_DisallowsInvalidRowIndex() { TextTable tt = new TextTable(Help.Ansi.OFF, new Help.Column[] {new Help.Column(30, 2, Help.Column.Overflow.SPAN)}); try { tt.putValue(1, 0, Help.Ansi.OFF.text("abc")); } catch (IllegalArgumentException ex) { assertEquals("Cannot write to row 1: rowCount=0", ex.getMessage()); } }
@Test public void testAnsiText() { String markup = "@|bg(red),white,underline some text|@"; Help.Ansi.Text txt = Help.Ansi.ON.text(markup); Help.Ansi.Text txt2 = Help.Ansi.ON.new Text(markup); assertEquals(txt, txt2); }