mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
f471677761
commit
eff60f0c86
@ -22,6 +22,18 @@ public class ConsoleTable {
|
|||||||
private static final char SPACE = '\u3000';
|
private static final char SPACE = '\u3000';
|
||||||
private static final char LF = CharUtil.LF;
|
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;
|
private List<Integer> columnCharNumber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建ConsoleTable对象
|
* 设置是否使用全角模式<br>
|
||||||
|
* 当包含中文字符时,输出的表格可能无法对齐,因此当设置为全角模式时,全部字符转为全角。
|
||||||
*
|
*
|
||||||
* @return ConsoleTable
|
* @param isSBCMode 是否全角模式
|
||||||
* @since 5.4.5
|
* @return this
|
||||||
|
* @since 5.8.0
|
||||||
*/
|
*/
|
||||||
public static ConsoleTable create() {
|
public ConsoleTable setSBCMode(boolean isSBCMode) {
|
||||||
return new ConsoleTable();
|
this.isSBCMode = isSBCMode;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,8 +98,10 @@ public class ConsoleTable {
|
|||||||
private void fillColumns(List<String> l, String[] columns) {
|
private void fillColumns(List<String> l, String[] columns) {
|
||||||
for (int i = 0; i < columns.length; i++) {
|
for (int i = 0; i < columns.length; i++) {
|
||||||
String column = columns[i];
|
String column = columns[i];
|
||||||
String column2 = Convert.toSBC(column);
|
if (isSBCMode) {
|
||||||
l.add(column2);
|
column = Convert.toSBC(column);
|
||||||
|
}
|
||||||
|
l.add(column);
|
||||||
int width = column.length();
|
int width = column.length();
|
||||||
if (width > columnCharNumber.get(i)) {
|
if (width > columnCharNumber.get(i)) {
|
||||||
columnCharNumber.set(i, width);
|
columnCharNumber.set(i, width);
|
||||||
@ -131,17 +148,19 @@ public class ConsoleTable {
|
|||||||
private void fillRow(StringBuilder sb, List<String> row) {
|
private void fillRow(StringBuilder sb, List<String> row) {
|
||||||
final int size = row.size();
|
final int size = row.size();
|
||||||
String value;
|
String value;
|
||||||
for (int i = 0;i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
value = row.get(i);
|
value = row.get(i);
|
||||||
sb.append(SPACE);
|
sb.append(SPACE);
|
||||||
sb.append(value);
|
sb.append(value);
|
||||||
|
final int length = value.length();
|
||||||
|
final int sbcCount = sbcCount(value);
|
||||||
|
if(sbcCount % 2 == 1){
|
||||||
|
sb.append(CharUtil.SPACE);
|
||||||
|
}
|
||||||
sb.append(SPACE);
|
sb.append(SPACE);
|
||||||
int length = value.length();
|
|
||||||
int maxLength = columnCharNumber.get(i);
|
int maxLength = columnCharNumber.get(i);
|
||||||
if (maxLength > length) {
|
for (int j = 0; j < (maxLength - length + (sbcCount / 2)); j++) {
|
||||||
for (int j = 0; j < (maxLength - length); j++) {
|
sb.append(SPACE);
|
||||||
sb.append(SPACE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
sb.append(COLUMN_LINE);
|
sb.append(COLUMN_LINE);
|
||||||
}
|
}
|
||||||
@ -167,4 +186,21 @@ public class ConsoleTable {
|
|||||||
public void print() {
|
public void print() {
|
||||||
Console.print(toString());
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ public class ConsoleTableTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void printTest() {
|
public void printSBCTest() {
|
||||||
ConsoleTable t = new ConsoleTable();
|
ConsoleTable t = ConsoleTable.create();
|
||||||
t.addHeader("姓名", "年龄");
|
t.addHeader("姓名", "年龄");
|
||||||
t.addBody("张三", "15");
|
t.addBody("张三", "15");
|
||||||
t.addBody("李四", "29");
|
t.addBody("李四", "29");
|
||||||
@ -17,7 +17,7 @@ public class ConsoleTableTest {
|
|||||||
|
|
||||||
Console.log();
|
Console.log();
|
||||||
|
|
||||||
t = new ConsoleTable();
|
t = ConsoleTable.create();
|
||||||
t.addHeader("体温", "占比");
|
t.addHeader("体温", "占比");
|
||||||
t.addHeader("℃", "%");
|
t.addHeader("℃", "%");
|
||||||
t.addBody("36.8", "10");
|
t.addBody("36.8", "10");
|
||||||
@ -26,10 +26,30 @@ public class ConsoleTableTest {
|
|||||||
|
|
||||||
Console.log();
|
Console.log();
|
||||||
|
|
||||||
t = new ConsoleTable();
|
t = ConsoleTable.create();
|
||||||
t.addHeader("标题1", "标题2");
|
t.addHeader("标题1", "标题2");
|
||||||
t.addBody("12345", "混合321654asdfcSDF");
|
t.addBody("12345", "混合321654asdfcSDF");
|
||||||
t.addBody("sd e3ee ff22", "ff值");
|
t.addBody("sd e3ee ff22", "ff值");
|
||||||
t.print();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user