This commit is contained in:
Looly 2022-04-10 21:07:56 +08:00
parent f471677761
commit eff60f0c86
2 changed files with 73 additions and 17 deletions

View File

@ -22,6 +22,18 @@ public class ConsoleTable {
private static final char SPACE = '\u3000';
private static final char LF = CharUtil.LF;
private boolean isSBCMode = true;
/**
* 创建ConsoleTable对象
*
* @return ConsoleTable
* @since 5.4.5
*/
public static ConsoleTable create() {
return new ConsoleTable();
}
/**
* 表格头信息
*/
@ -36,13 +48,16 @@ public class ConsoleTable {
private List<Integer> columnCharNumber;
/**
* 创建ConsoleTable对象
* 设置是否使用全角模式<br>
* 当包含中文字符时输出的表格可能无法对齐因此当设置为全角模式时全部字符转为全角
*
* @return ConsoleTable
* @since 5.4.5
* @param isSBCMode 是否全角模式
* @return this
* @since 5.8.0
*/
public static ConsoleTable create() {
return new ConsoleTable();
public ConsoleTable setSBCMode(boolean isSBCMode) {
this.isSBCMode = isSBCMode;
return this;
}
/**
@ -83,8 +98,10 @@ public class ConsoleTable {
private void fillColumns(List<String> l, String[] columns) {
for (int i = 0; i < columns.length; i++) {
String column = columns[i];
String column2 = Convert.toSBC(column);
l.add(column2);
if (isSBCMode) {
column = Convert.toSBC(column);
}
l.add(column);
int width = column.length();
if (width > columnCharNumber.get(i)) {
columnCharNumber.set(i, width);
@ -131,17 +148,19 @@ public class ConsoleTable {
private void fillRow(StringBuilder sb, List<String> row) {
final int size = row.size();
String value;
for (int i = 0;i < size; i++) {
for (int i = 0; i < size; i++) {
value = row.get(i);
sb.append(SPACE);
sb.append(value);
final int length = value.length();
final int sbcCount = sbcCount(value);
if(sbcCount % 2 == 1){
sb.append(CharUtil.SPACE);
}
sb.append(SPACE);
int length = value.length();
int maxLength = columnCharNumber.get(i);
if (maxLength > length) {
for (int j = 0; j < (maxLength - length); j++) {
sb.append(SPACE);
}
for (int j = 0; j < (maxLength - length + (sbcCount / 2)); j++) {
sb.append(SPACE);
}
sb.append(COLUMN_LINE);
}
@ -167,4 +186,21 @@ public class ConsoleTable {
public void print() {
Console.print(toString());
}
/**
* 半角字符数量
*
* @param value 字符串
* @return 填充空格数量
*/
private int sbcCount(String value) {
int count = 0;
for (int i = 0; i < value.length(); i++) {
if (value.charAt(i) < '\177') {
count++;
}
}
return count;
}
}

View File

@ -7,8 +7,8 @@ public class ConsoleTableTest {
@Test
@Ignore
public void printTest() {
ConsoleTable t = new ConsoleTable();
public void printSBCTest() {
ConsoleTable t = ConsoleTable.create();
t.addHeader("姓名", "年龄");
t.addBody("张三", "15");
t.addBody("李四", "29");
@ -17,7 +17,7 @@ public class ConsoleTableTest {
Console.log();
t = new ConsoleTable();
t = ConsoleTable.create();
t.addHeader("体温", "占比");
t.addHeader("", "%");
t.addBody("36.8", "10");
@ -26,10 +26,30 @@ public class ConsoleTableTest {
Console.log();
t = new ConsoleTable();
t = ConsoleTable.create();
t.addHeader("标题1", "标题2");
t.addBody("12345", "混合321654asdfcSDF");
t.addBody("sd e3ee ff22", "ff值");
t.print();
}
@Test
@Ignore
public void printDBCTest() {
ConsoleTable t = ConsoleTable.create().setSBCMode(false);
t.addHeader("姓名", "年龄");
t.addBody("张三", "15");
t.addBody("李四", "29");
t.addBody("王二麻子", "37");
t.print();
Console.log();
t = ConsoleTable.create().setSBCMode(false);
t.addHeader("体温", "占比");
t.addHeader("", "%");
t.addBody("36.8", "10");
t.addBody("37", "5");
t.print();
}
}