diff --git a/src/main/java/sqlline/Commands.java b/src/main/java/sqlline/Commands.java index b37626f5..0f628993 100644 --- a/src/main/java/sqlline/Commands.java +++ b/src/main/java/sqlline/Commands.java @@ -32,6 +32,8 @@ import org.jline.utils.AttributedStringBuilder; import org.jline.utils.AttributedStyle; +import static sqlline.SqlLine.rpad; + /** * Collection of available commands. */ @@ -534,8 +536,8 @@ public void scan(String line, DispatchCallback callback) { driverNames.put(driver.getClass().getName(), driver); } - final String header = SqlLine.PAD.apply(sqlLine.loc("compliant"), 10) - + SqlLine.PAD.apply(sqlLine.loc("jdbc-version"), 8) + final String header = rpad(sqlLine.loc("compliant"), 10) + + rpad(sqlLine.loc("jdbc-version"), 8) + sqlLine.loc("driver-class"); sqlLine.output(new AttributedString(header, AttributedStyle.BOLD)); @@ -544,10 +546,8 @@ public void scan(String line, DispatchCallback callback) { try { final Class klass = Class.forName(name); Driver driver = (Driver) klass.getConstructor().newInstance(); - final String msg = - SqlLine.PAD.apply(driver.jdbcCompliant() ? "yes" : "no", 10) - + SqlLine.PAD.apply( - driver.getMajorVersion() + "." + driver.getMinorVersion(), 8) + final String msg = rpad(driver.jdbcCompliant() ? "yes" : "no", 10) + + rpad(driver.getMajorVersion() + "." + driver.getMinorVersion(), 8) + name; if (driver.jdbcCompliant()) { sqlLine.output(msg); @@ -775,11 +775,13 @@ public void dbinfo(String line, DispatchCallback callback) { for (String method : METHODS) { try { + final String s = + String.valueOf(sqlLine.getReflector() + .invoke(sqlLine.getDatabaseMetaData(), method)); sqlLine.output( new AttributedStringBuilder() - .append(SqlLine.PAD.apply(method, padlen)) - .append(String.valueOf(sqlLine.getReflector() - .invoke(sqlLine.getDatabaseMetaData(), method))) + .append(rpad(method, padlen)) + .append(s) .toAttributedString()); } catch (Exception e) { sqlLine.handleException(e); @@ -1324,10 +1326,9 @@ public void list(String line, DispatchCallback callback) { closed = true; } - sqlLine.output(SqlLine.PAD.apply(" #" + index++ + "", 5) - + SqlLine.PAD.apply( - closed ? sqlLine.loc("closed") : sqlLine.loc("open"), 9) - + SqlLine.PAD.apply(databaseConnection.getNickname(), 20) + sqlLine.output(rpad(" #" + index++ + "", 5) + + rpad(closed ? sqlLine.loc("closed") : sqlLine.loc("open"), 9) + + rpad(databaseConnection.getNickname(), 20) + " " + databaseConnection.getUrl()); } @@ -1683,9 +1684,8 @@ public void help(String line, DispatchCallback callback) { if (cmd.equals("set")) { help += sqlLine.loc("variables"); } - clist.add( - SqlLine.PAD.apply( - SqlLine.COMMAND_PREFIX + commandHandler.getName(), 20) + help); + clist.add(rpad(SqlLine.COMMAND_PREFIX + commandHandler.getName(), 20) + + help); } } diff --git a/src/main/java/sqlline/SqlLine.java b/src/main/java/sqlline/SqlLine.java index 18c5292f..380c66ce 100644 --- a/src/main/java/sqlline/SqlLine.java +++ b/src/main/java/sqlline/SqlLine.java @@ -26,7 +26,6 @@ import java.text.MessageFormat; import java.util.*; import java.util.Date; -import java.util.function.BiFunction; import java.util.function.Supplier; import java.util.jar.Attributes; import java.util.jar.Manifest; @@ -60,9 +59,7 @@ public class SqlLine { ResourceBundle.getBundle(SqlLine.class.getName(), Locale.ROOT); private static final String SEPARATOR = System.getProperty("line.separator"); - static final BiFunction PAD = - (value, padLen) -> String.format(Locale.ROOT, "%1$-" + padLen + "s", - Objects.toString(value, "")); + private boolean exit = false; /** Whether we are currently prompting for input (say, user name or * password). If prompting, we ignore trailing linefeeds, but for SQL input we @@ -1423,6 +1420,33 @@ String wrap(String toWrap, int len, int start) { return buff.toString(); } + /** Returns a value padded with spaces to a given length, + * with spaces added to right side. */ + static String rpad(String value, int padLen) { + return String.format(Locale.ROOT, "%1$-" + padLen + "s", + Objects.toString(value, "")); + } + + /** Returns a value padded with spaces to a given length, + * with equal numbers of spaces on left and right side. */ + static String center(String str, int len) { + final int n = len - str.length(); + if (n <= 0) { + return str; + } + final StringBuilder buf = new StringBuilder(); + final int left = n / 2; + final int right = n - left; + for (int i = 0; i < left; i++) { + buf.append(' '); + } + buf.append(str); + for (int i = 0; i < right; i++) { + buf.append(' '); + } + return buf.toString(); + } + /** * Output a progress indicator to the console. * @@ -1635,13 +1659,13 @@ void runBatch(List statements) { } output(new AttributedStringBuilder() - .append(SqlLine.PAD.apply("COUNT", 8), AttributedStyle.BOLD) + .append(rpad("COUNT", 8), AttributedStyle.BOLD) .append("STATEMENT", AttributedStyle.BOLD) .toAttributedString()); for (int i = 0; i < counts.length; i++) { output(new AttributedStringBuilder() - .append(SqlLine.PAD.apply(counts[i] + "", 8)) + .append(rpad(counts[i] + "", 8)) .append(statements.get(i)) .toAttributedString()); } @@ -1663,7 +1687,7 @@ public int runCommands(List cmds, DispatchCallback callback) { int size = cmds.size(); for (String cmd : cmds) { info(new AttributedStringBuilder() - .append(SqlLine.PAD.apply(index++ + "/" + size, 13)) + .append(rpad(index++ + "/" + size, 13)) .append(cmd) .toAttributedString()); @@ -1692,7 +1716,7 @@ void setCompletions() { void outputProperty(String key, String value) { output(new AttributedStringBuilder() - .append(SqlLine.PAD.apply(key, 20), AttributedStyles.GREEN) + .append(rpad(key, 20), AttributedStyles.GREEN) .append(value) .toAttributedString()); } diff --git a/src/main/java/sqlline/TableOutputFormat.java b/src/main/java/sqlline/TableOutputFormat.java index 18e9c245..76118142 100644 --- a/src/main/java/sqlline/TableOutputFormat.java +++ b/src/main/java/sqlline/TableOutputFormat.java @@ -15,6 +15,8 @@ import org.jline.utils.AttributedStringBuilder; import org.jline.utils.AttributedStyle; +import static sqlline.SqlLine.rpad; + /** * OutputFormat for a pretty, table-like format. */ @@ -91,15 +93,13 @@ void printRow(AttributedString attributedString, boolean header) { AttributedStringBuilder builder = new AttributedStringBuilder(); if (header) { sqlLine.output( - builder - .append("+-", AttributedStyles.GREEN) + builder.append("+-", AttributedStyles.GREEN) .append(attributedString) .append("-+", AttributedStyles.GREEN) .toAttributedString()); } else { sqlLine.output( - builder - .append("| ", AttributedStyles.GREEN) + builder.append("| ", AttributedStyles.GREEN) .append(attributedString) .append(" |", AttributedStyles.GREEN) .toAttributedString()); @@ -125,14 +125,14 @@ private AttributedString getOutputString( final int[] sizes = row.sizes; if (row.isMeta) { - v = centerString(row.values[i], row.sizes[i]); + v = SqlLine.center(row.values[i], row.sizes[i]); if (rows.isPrimaryKey(i)) { builder.append(v, AttributedStyles.CYAN); } else { builder.append(v, AttributedStyle.BOLD); } } else { - v = SqlLine.PAD.apply(row.values[i], row.sizes[i]); + v = rpad(row.values[i], row.sizes[i]); if (rows.isPrimaryKey(i)) { builder.append(v, AttributedStyles.CYAN); } else { @@ -158,23 +158,6 @@ private AttributedString getOutputString( return builder.toAttributedString(); } - static String centerString(String str, int len) { - final int n = len - str.length(); - if (n <= 0) { - return str; - } - final StringBuilder buf = new StringBuilder(); - final int left = n / 2; - final int right = n - left; - for (int i = 0; i < left; i++) { - buf.append(' '); - } - buf.append(str); - for (int i = 0; i < right; i++) { - buf.append(' '); - } - return buf.toString(); - } } // End TableOutputFormat.java diff --git a/src/main/java/sqlline/VerticalOutputFormat.java b/src/main/java/sqlline/VerticalOutputFormat.java index 96a071ab..43b1ca1d 100644 --- a/src/main/java/sqlline/VerticalOutputFormat.java +++ b/src/main/java/sqlline/VerticalOutputFormat.java @@ -14,6 +14,8 @@ import org.jline.utils.AttributedStringBuilder; import org.jline.utils.AttributedStyle; +import static sqlline.SqlLine.rpad; + /** * OutputFormat for vertical column name: value format. */ @@ -49,8 +51,7 @@ public void printRow(Rows rows, Rows.Row header, Rows.Row row) { for (int i = 0; (i < head.length) && (i < vals.length); i++) { sqlLine.output( new AttributedStringBuilder() - .append(SqlLine.PAD.apply(head[i], headWidth), - AttributedStyle.BOLD) + .append(rpad(head[i], headWidth), AttributedStyle.BOLD) .append((vals[i] == null) ? "" : vals[i]) .toAttributedString()); } diff --git a/src/test/java/sqlline/SqlLineTest.java b/src/test/java/sqlline/SqlLineTest.java index 2334dad9..b47508f6 100644 --- a/src/test/java/sqlline/SqlLineTest.java +++ b/src/test/java/sqlline/SqlLineTest.java @@ -15,6 +15,8 @@ import org.junit.jupiter.api.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -91,15 +93,23 @@ public void testSplitCompound() { assertArrayEquals(new String[][] {{"ABC"}, {";DEF"}}, strings); } + @Test + public void testRpad() { + assertThat(SqlLine.rpad("x", 1), is("x")); + assertThat(SqlLine.rpad("x", 2), is("x ")); + assertThat(SqlLine.rpad("xyz", 2), is("xyz")); + assertThat(SqlLine.rpad(" x ", 5), is(" x ")); + assertThat(SqlLine.rpad(null, 2), is(" ")); + } + @Test public void testCenterString() { - assertEquals("abc", TableOutputFormat.centerString("abc", -1)); - assertEquals("abc", TableOutputFormat.centerString("abc", 1)); - assertEquals("abc ", TableOutputFormat.centerString("abc", 4)); - assertEquals(" abc ", TableOutputFormat.centerString("abc", 5)); - // centerString used to have cartesian performance - assertEquals( - 1234567, TableOutputFormat.centerString("abc", 1234567).length()); + assertThat(SqlLine.center("abc", -1), is("abc")); + assertThat(SqlLine.center("abc", 1), is("abc")); + assertThat(SqlLine.center("abc", 4), is("abc ")); + assertThat(SqlLine.center("abc", 5), is(" abc ")); + // center used to have cartesian performance + assertThat(SqlLine.center("abc", 1234567).length(), is(1234567)); } @Test