in samza-sql-shell/src/main/java/org/apache/samza/sql/client/impl/CliCommandHandler.java [556:662]
private List<String> formatSchema4Display(SqlSchema schema) {
final String headerField = "Field";
final String headerType = "Type";
final char seperator = '|';
final char lineSep = '-';
int terminalWidth = terminal.getWidth();
// Two spaces * 2 plus one SEPERATOR
if (terminalWidth < 2 + 2 + 1 + headerField.length() + headerType.length()) {
return Collections.singletonList("Not enough room.");
}
// Find the best seperator position for least rows
int seperatorPos = headerField.length() + 2;
int minRowNeeded = Integer.MAX_VALUE;
int longestLineCharNum = 0;
int rowCount = schema.getFields().size();
for (int j = seperatorPos; j < terminalWidth - headerType.length() - 2; ++j) {
boolean fieldWrapped = false;
int rowNeeded = 0;
for (int i = 0; i < rowCount; ++i) {
SqlSchema.SqlField field = schema.getFields().get(i);
int fieldLen = field.getFieldName().length();
int typeLen = field.getFieldSchema().getFieldType().toString().length();
int fieldRowNeeded = CliUtil.ceilingDiv(fieldLen, j - 2);
int typeRowNeeded = CliUtil.ceilingDiv(typeLen, terminalWidth - 1 - j - 2);
rowNeeded += Math.max(fieldRowNeeded, typeRowNeeded);
fieldWrapped |= fieldRowNeeded > 1;
if (typeRowNeeded > 1) {
longestLineCharNum = terminalWidth;
} else {
longestLineCharNum = Math.max(longestLineCharNum, j + typeLen + 2 + 1);
}
}
if (rowNeeded < minRowNeeded) {
minRowNeeded = rowNeeded;
seperatorPos = j;
}
if (!fieldWrapped)
break;
}
List<String> lines = new ArrayList<>(minRowNeeded + 4);
// Header
StringBuilder line = new StringBuilder(terminalWidth);
line.append(CliConstants.SPACE);
line.append(headerField);
CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
line.append(seperator);
line.append(CliConstants.SPACE);
line.append(headerType);
lines.add(line.toString());
line = new StringBuilder(terminalWidth);
CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
lines.add(line.toString());
// Body
AttributedStyle oddLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.BLUE);
AttributedStyle evenLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.CYAN);
final int fieldColSize = seperatorPos - 2;
final int typeColSize = terminalWidth - seperatorPos - 1 - 2;
for (int i = 0; i < rowCount; ++i) {
SqlSchema.SqlField sqlField = schema.getFields().get(i);
String field = sqlField.getFieldName();
String type = getFieldDisplayValue(sqlField.getFieldSchema());
int fieldLen = field.length();
int typeLen = type.length();
int fieldStartIdx = 0, typeStartIdx = 0;
while (fieldStartIdx < fieldLen || typeStartIdx < typeLen) {
line = new StringBuilder(terminalWidth);
line.append(CliConstants.SPACE);
int numToWrite = Math.min(fieldColSize, fieldLen - fieldStartIdx);
if (numToWrite > 0) {
line.append(field, fieldStartIdx, fieldStartIdx + numToWrite);
fieldStartIdx += numToWrite;
}
CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
line.append(seperator);
line.append(CliConstants.SPACE);
numToWrite = Math.min(typeColSize, typeLen - typeStartIdx);
if (numToWrite > 0) {
line.append(type, typeStartIdx, typeStartIdx + numToWrite);
typeStartIdx += numToWrite;
}
if (i % 2 == 0) {
AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(evenLineStyle);
attrBuilder.append(line.toString());
lines.add(attrBuilder.toAnsi());
} else {
AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(oddLineStyle);
attrBuilder.append(line.toString());
lines.add(attrBuilder.toAnsi());
}
}
}
// Footer
line = new StringBuilder(terminalWidth);
CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
lines.add(line.toString());
return lines;
}