add console.table

This commit is contained in:
Looly 2020-10-14 18:09:12 +08:00
parent 1b4424f7fa
commit 97d48dde0b
3 changed files with 57 additions and 37 deletions

View File

@ -48,14 +48,14 @@ public class Console {
* System.out.println()方法打印控制台日志<br> * System.out.println()方法打印控制台日志<br>
* 如果传入打印对象为{@link Throwable}对象那么同时打印堆栈 * 如果传入打印对象为{@link Throwable}对象那么同时打印堆栈
* *
* @param obj1 第一个要打印的对象 * @param obj1 第一个要打印的对象
* @param otherObjs 其它要打印的对象 * @param otherObjs 其它要打印的对象
* @since 5.4.3 * @since 5.4.3
*/ */
public static void log(Object obj1, Object... otherObjs) { public static void log(Object obj1, Object... otherObjs) {
if(ArrayUtil.isEmpty(otherObjs)){ if (ArrayUtil.isEmpty(otherObjs)) {
log(obj1); log(obj1);
} else{ } else {
log(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1)); log(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1));
} }
} }
@ -97,11 +97,22 @@ public class Console {
* @param values * @param values
* @since 5.4.3 * @since 5.4.3
*/ */
private static void logInternal(String template, Object... values){ private static void logInternal(String template, Object... values) {
log(null, template, values); log(null, template, values);
} }
// --------------------------------------------------------------------------------- print // --------------------------------------------------------------------------------- print
/**
* 打印表格到控制台
*
* @param consoleTable 控制台表格
* @since 5.4.5
*/
public static void table(ConsoleTable consoleTable) {
print(consoleTable.toString());
}
/** /**
* System.out.print()方法打印控制台日志 * System.out.print()方法打印控制台日志
* *
@ -116,14 +127,14 @@ public class Console {
* System.out.println()方法打印控制台日志<br> * System.out.println()方法打印控制台日志<br>
* 如果传入打印对象为{@link Throwable}对象那么同时打印堆栈 * 如果传入打印对象为{@link Throwable}对象那么同时打印堆栈
* *
* @param obj1 第一个要打印的对象 * @param obj1 第一个要打印的对象
* @param otherObjs 其它要打印的对象 * @param otherObjs 其它要打印的对象
* @since 5.4.3 * @since 5.4.3
*/ */
public static void print(Object obj1, Object... otherObjs) { public static void print(Object obj1, Object... otherObjs) {
if(ArrayUtil.isEmpty(otherObjs)){ if (ArrayUtil.isEmpty(otherObjs)) {
print(obj1); print(obj1);
} else{ } else {
print(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1)); print(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1));
} }
} }
@ -174,7 +185,7 @@ public class Console {
* @param values * @param values
* @since 5.4.3 * @since 5.4.3
*/ */
private static void printInternal(String template, Object... values){ private static void printInternal(String template, Object... values) {
out.print(StrUtil.format(template, values)); out.print(StrUtil.format(template, values));
} }
@ -205,14 +216,14 @@ public class Console {
* System.out.println()方法打印控制台日志<br> * System.out.println()方法打印控制台日志<br>
* 如果传入打印对象为{@link Throwable}对象那么同时打印堆栈 * 如果传入打印对象为{@link Throwable}对象那么同时打印堆栈
* *
* @param obj1 第一个要打印的对象 * @param obj1 第一个要打印的对象
* @param otherObjs 其它要打印的对象 * @param otherObjs 其它要打印的对象
* @since 5.4.3 * @since 5.4.3
*/ */
public static void error(Object obj1, Object... otherObjs) { public static void error(Object obj1, Object... otherObjs) {
if(ArrayUtil.isEmpty(otherObjs)){ if (ArrayUtil.isEmpty(otherObjs)) {
error(obj1); error(obj1);
} else{ } else {
error(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1)); error(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1));
} }
} }
@ -312,7 +323,7 @@ public class Console {
* @param count 变量数量 * @param count 变量数量
* @return 模板 * @return 模板
*/ */
private static String buildTemplateSplitBySpace(int count){ private static String buildTemplateSplitBySpace(int count) {
return StrUtil.repeatAndJoin(TEMPLATE_VAR, count, StrUtil.SPACE); return StrUtil.repeatAndJoin(TEMPLATE_VAR, count, StrUtil.SPACE);
} }

View File

@ -35,6 +35,16 @@ public class ConsoleTable {
*/ */
private List<Integer> columnCharNumber; private List<Integer> columnCharNumber;
/**
* 创建ConsoleTable对象
*
* @return ConsoleTable
* @since 5.4.5
*/
public static ConsoleTable create(){
return new ConsoleTable();
}
/** /**
* 添加头信息 * 添加头信息
* *
@ -101,7 +111,7 @@ public class ConsoleTable {
/** /**
* 填充表头或者表体信息 * 填充表头或者表体信息
* *
* @param sb * @param sb 内容
* @param list 表头列表或者表体列表 * @param list 表头列表或者表体列表
*/ */
private void fillRow(StringBuilder sb, List<List<String>> list) { private void fillRow(StringBuilder sb, List<List<String>> list) {

View File

@ -1,35 +1,34 @@
package cn.hutool.core.lang; package cn.hutool.core.lang;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public class ConsoleTableTest { public class ConsoleTableTest {
@Test @Test
@Ignore // @Ignore
public void printTest() { public void printTest() {
ConsoleTable t = new ConsoleTable(); ConsoleTable t = new ConsoleTable();
t.addHeader("姓名", "年龄"); t.addHeader("姓名", "年龄");
t.addBody("张三", "15"); t.addBody("张三", "15");
t.addBody("李四", "29"); t.addBody("李四", "29");
t.addBody("王二麻子", "37"); t.addBody("王二麻子", "37");
t.print(); t.print();
Console.log(); Console.log();
t = new ConsoleTable(); t = new ConsoleTable();
t.addHeader("体温", "占比"); t.addHeader("体温", "占比");
t.addHeader("", "%"); t.addHeader("", "%");
t.addBody("36.8", "10"); t.addBody("36.8", "10");
t.addBody("37", "5"); t.addBody("37", "5");
t.print(); t.print();
Console.log(); Console.log();
t = new ConsoleTable(); t = new ConsoleTable();
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();
} }
} }