Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5768] improvement(CLI): improve formatted output #5770

Merged
merged 9 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Metalake;

Expand Down Expand Up @@ -81,6 +82,12 @@ public void output(Catalog catalog) {

static final class TableFormatImpl {
private int[] maxElementLengths;
// This expression is primarily used to match characters that have a display width of
// 2, such as characters from Korean, Chinese
private static final Pattern FULL_WIDTH_PATTERN =
Pattern.compile(
"[\u1100-\u115F\u2E80-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE6F\uFF00-\uFF60\uFFE0-\uFFE6]");
private int[][] elementOutputWidths;
private final String horizontalDelimiter = "-";
private final String verticalDelimiter = "|";
private final String crossDelimiter = "+";
Expand All @@ -96,10 +103,9 @@ public void print(List<String> headers, List<List<String>> rows) {
throw new IllegalArgumentException("Number of columns is not equal.");
}
maxElementLengths = new int[headers.size()];
elementOutputWidths = new int[rows.size()][headers.size()];
updateMaxLengthsFromList(headers);
updateMaxLengthsFromNestedList(rows);

// print headers
printLine();
System.out.println();
for (int i = 0; i < headers.size(); ++i) {
Expand All @@ -115,9 +121,22 @@ public void print(List<String> headers, List<List<String>> rows) {
for (int i = 0; i < rows.size(); ++i) {
List<String> columns = rows.get(i);
for (int j = 0; j < columns.size(); ++j) {
System.out.printf(
verticalDelimiter + indent + "%-" + maxElementLengths[j] + "s" + indent,
columns.get(j));
String column = columns.get(j);
// Handle cases where the width and number of characters are inconsistent
if (elementOutputWidths[i][j] != column.length()) {
if (elementOutputWidths[i][j] > maxElementLengths[j]) {
System.out.printf(
verticalDelimiter + indent + "%-" + column.length() + "s" + indent, column);
} else {
int paddingLength =
maxElementLengths[j] - (elementOutputWidths[i][j] - column.length());
System.out.printf(
verticalDelimiter + indent + "%-" + paddingLength + "s" + indent, column);
}
} else {
System.out.printf(
verticalDelimiter + indent + "%-" + maxElementLengths[j] + "s" + indent, column);
}
}
System.out.println(verticalDelimiter);
}
Expand All @@ -130,20 +149,42 @@ private void updateMaxLengthsFromList(List<String> elements) {
String s;
for (int i = 0; i < elements.size(); ++i) {
s = elements.get(i);
if (s.length() > maxElementLengths[i]) maxElementLengths[i] = s.length();
if (getOutputWidth(s) > maxElementLengths[i]) maxElementLengths[i] = getOutputWidth(s);
}
}

private void updateMaxLengthsFromNestedList(List<List<String>> elements) {
int rowIdx = 0;
for (List<String> row : elements) {
String s;
for (int i = 0; i < row.size(); ++i) {
s = row.get(i);
if (s.length() > maxElementLengths[i]) maxElementLengths[i] = s.length();
int consoleWidth = getOutputWidth(s);
elementOutputWidths[rowIdx][i] = consoleWidth;
if (consoleWidth > maxElementLengths[i]) maxElementLengths[i] = consoleWidth;
}
rowIdx++;
}
}

private int getOutputWidth(String s) {
int width = 0;
for (int i = 0; i < s.length(); i++) {
width += getCharWidth(s.charAt(i));
}

return width;
}

private static int getCharWidth(char ch) {
String s = String.valueOf(ch);
if (FULL_WIDTH_PATTERN.matcher(s).find()) {
return 2;
}

return 1;
}

private void printLine() {
System.out.print(crossDelimiter);
for (int i = 0; i < maxElementLengths.length; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ public void startUp() {
gravitinoUrl
};
Main.main(create_catalog_args);

String[] create_catalog_with_comment_args = {
"catalog",
"create",
commandArg(GravitinoOptions.METALAKE),
"my_metalake",
commandArg(GravitinoOptions.NAME),
"postgres2",
commandArg(GravitinoOptions.PROVIDER),
"postgres",
commandArg(GravitinoOptions.PROPERTIES),
"jdbc-url=jdbc:postgresql://postgresql-host/mydb,jdbc-user=user,jdbc-password=password,jdbc-database=db,jdbc-driver=org.postgresql.Driver",
commandArg(GravitinoOptions.URL),
gravitinoUrl,
commandArg(GravitinoOptions.COMMENT),
"catalog, 用于测试"
};
Main.main(create_catalog_with_comment_args);
}

@Test
Expand Down Expand Up @@ -160,6 +178,38 @@ public void testCatalogDetailsCommand() {
output);
}

@Test
public void testCatalogDetailsCommandFullCornerCharacter() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outputStream));

String[] args = {
"catalog",
"details",
commandArg(GravitinoOptions.METALAKE),
"my_metalake",
commandArg(GravitinoOptions.NAME),
"postgres2",
commandArg(GravitinoOptions.OUTPUT),
"table",
commandArg(GravitinoOptions.URL),
gravitinoUrl
};
Main.main(args);
// Restore the original System.out
System.setOut(originalOut);
// Get the output and verify it
String output = new String(outputStream.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(
"+-----------+------------+-----------------+-------------------+\n"
+ "| catalog | type | provider | comment |\n"
+ "+-----------+------------+-----------------+-------------------+\n"
+ "| postgres2 | RELATIONAL | jdbc-postgresql | catalog, 用于测试 |\n"
+ "+-----------+------------+-----------------+-------------------+",
output);
}

private String commandArg(String arg) {
return String.format("--%s", arg);
}
Expand Down
Loading