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 3 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,10 @@ public void output(Catalog catalog) {

static final class TableFormatImpl {
private int[] maxElementLengths;
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 +101,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 +119,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 +147,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 @@ -59,7 +59,9 @@ public void startUp() {
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
gravitinoUrl,
commandArg(GravitinoOptions.COMMENT),
"catalog,用于测试"
};
Main.main(create_catalog_args);
}
Expand Down Expand Up @@ -152,11 +154,11 @@ public void testCatalogDetailsCommand() {
// Get the output and verify it
String output = new String(outputStream.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(
"+----------+------------+-----------------+---------+\n"
+ "| catalog | type | provider | comment |\n"
+ "+----------+------------+-----------------+---------+\n"
+ "| postgres | RELATIONAL | jdbc-postgresql | null |\n"
+ "+----------+------------+-----------------+---------+",
"+----------+------------+-----------------+-------------------+\n"
+ "| catalog | type | provider | comment |\n"
+ "+----------+------------+-----------------+-------------------+\n"
+ "| postgres | RELATIONAL | jdbc-postgresql | catalog,用于测试 |\n"
+ "+----------+------------+-----------------+-------------------+",
output);
}

Expand Down
Loading