Skip to content

Commit

Permalink
[SQLLINE-296] Use Jline3's AttributedString instead of ColorBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
julianhyde committed May 22, 2019
1 parent 6cf6002 commit 53d8df0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 56 deletions.
32 changes: 16 additions & 16 deletions src/main/java/sqlline/Commands.java
Expand Up @@ -32,6 +32,8 @@
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

import static sqlline.SqlLine.rpad;

/**
* Collection of available commands.
*/
Expand Down Expand Up @@ -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));

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
40 changes: 32 additions & 8 deletions src/main/java/sqlline/SqlLine.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Integer, String> 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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -1635,13 +1659,13 @@ void runBatch(List<String> 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());
}
Expand All @@ -1663,7 +1687,7 @@ public int runCommands(List<String> 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());

Expand Down Expand Up @@ -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());
}
Expand Down
29 changes: 6 additions & 23 deletions src/main/java/sqlline/TableOutputFormat.java
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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());
Expand All @@ -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 {
Expand All @@ -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
5 changes: 3 additions & 2 deletions src/main/java/sqlline/VerticalOutputFormat.java
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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());
}
Expand Down
24 changes: 17 additions & 7 deletions src/test/java/sqlline/SqlLineTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 53d8df0

Please sign in to comment.