in dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetUtils.java [62:158]
public static String toTable(List<String> header, List<List<String>> table) {
int totalWidth = 0;
int[] widths = new int[header.size()];
int maxwidth = 70;
int maxcountbefore = 0;
for (int j = 0; j < widths.length; j++) {
widths[j] = Math.max(widths[j], header.get(j).length());
}
for (List<String> row : table) {
int countbefore = 0;
for (int j = 0; j < widths.length; j++) {
widths[j] = Math.max(widths[j], row.get(j).length());
totalWidth = (totalWidth + widths[j]) > maxwidth ? maxwidth : (totalWidth + widths[j]);
if (j < widths.length - 1) {
countbefore = countbefore + widths[j];
}
}
maxcountbefore = Math.max(countbefore, maxcountbefore);
}
widths[widths.length - 1] = Math.min(widths[widths.length - 1], maxwidth - maxcountbefore);
StringBuilder buf = new StringBuilder();
// line
buf.append('+');
for (int j = 0; j < widths.length; j++) {
for (int k = 0; k < widths[j] + 2; k++) {
buf.append('-');
}
buf.append('+');
}
buf.append("\r\n");
// header
buf.append('|');
for (int j = 0; j < widths.length; j++) {
String cell = header.get(j);
buf.append(' ');
buf.append(cell);
int pad = widths[j] - cell.length();
if (pad > 0) {
for (int k = 0; k < pad; k++) {
buf.append(' ');
}
}
buf.append(" |");
}
buf.append("\r\n");
// line
buf.append('+');
for (int j = 0; j < widths.length; j++) {
for (int k = 0; k < widths[j] + 2; k++) {
buf.append('-');
}
buf.append('+');
}
buf.append("\r\n");
// content
for (List<String> row : table) {
StringBuilder rowbuf = new StringBuilder();
rowbuf.append('|');
for (int j = 0; j < widths.length; j++) {
String cell = row.get(j);
rowbuf.append(' ');
int remaining = cell.length();
while (remaining > 0) {
if (rowbuf.length() >= totalWidth) {
buf.append(rowbuf.toString());
rowbuf = new StringBuilder();
// for(int m = 0;m < maxcountbefore && maxcountbefore < totalWidth ;
// m++){
// rowbuf.append(" ");
// }
}
rowbuf.append(cell, cell.length() - remaining, cell.length() - remaining + 1);
remaining--;
}
int pad = widths[j] - cell.length();
if (pad > 0) {
for (int k = 0; k < pad; k++) {
rowbuf.append(' ');
}
}
rowbuf.append(" |");
}
buf.append(rowbuf).append("\r\n");
}
// line
buf.append('+');
for (int j = 0; j < widths.length; j++) {
for (int k = 0; k < widths[j] + 2; k++) {
buf.append('-');
}
buf.append('+');
}
buf.append("\r\n");
return buf.toString();
}