From 97d48dde0b670c87eac8aee59252bc054497a498 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 14 Oct 2020 18:09:12 +0800 Subject: [PATCH] add console.table --- .../java/cn/hutool/core/lang/Console.java | 35 +++++++++----- .../cn/hutool/core/lang/ConsoleTable.java | 12 ++++- .../cn/hutool/core/lang/ConsoleTableTest.java | 47 +++++++++---------- 3 files changed, 57 insertions(+), 37 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Console.java b/hutool-core/src/main/java/cn/hutool/core/lang/Console.java index d436b27c4..0b9279f4a 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Console.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Console.java @@ -48,14 +48,14 @@ public class Console { * 同 System.out.println()方法,打印控制台日志
* 如果传入打印对象为{@link Throwable}对象,那么同时打印堆栈 * - * @param obj1 第一个要打印的对象 + * @param obj1 第一个要打印的对象 * @param otherObjs 其它要打印的对象 * @since 5.4.3 */ public static void log(Object obj1, Object... otherObjs) { - if(ArrayUtil.isEmpty(otherObjs)){ + if (ArrayUtil.isEmpty(otherObjs)) { log(obj1); - } else{ + } else { log(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1)); } } @@ -97,11 +97,22 @@ public class Console { * @param values 值 * @since 5.4.3 */ - private static void logInternal(String template, Object... values){ + private static void logInternal(String template, Object... values) { log(null, template, values); } // --------------------------------------------------------------------------------- print + + /** + * 打印表格到控制台 + * + * @param consoleTable 控制台表格 + * @since 5.4.5 + */ + public static void table(ConsoleTable consoleTable) { + print(consoleTable.toString()); + } + /** * 同 System.out.print()方法,打印控制台日志 * @@ -116,14 +127,14 @@ public class Console { * 同 System.out.println()方法,打印控制台日志
* 如果传入打印对象为{@link Throwable}对象,那么同时打印堆栈 * - * @param obj1 第一个要打印的对象 + * @param obj1 第一个要打印的对象 * @param otherObjs 其它要打印的对象 * @since 5.4.3 */ public static void print(Object obj1, Object... otherObjs) { - if(ArrayUtil.isEmpty(otherObjs)){ + if (ArrayUtil.isEmpty(otherObjs)) { print(obj1); - } else{ + } else { print(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1)); } } @@ -174,7 +185,7 @@ public class Console { * @param values 值 * @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)); } @@ -205,14 +216,14 @@ public class Console { * 同 System.out.println()方法,打印控制台日志
* 如果传入打印对象为{@link Throwable}对象,那么同时打印堆栈 * - * @param obj1 第一个要打印的对象 + * @param obj1 第一个要打印的对象 * @param otherObjs 其它要打印的对象 * @since 5.4.3 */ public static void error(Object obj1, Object... otherObjs) { - if(ArrayUtil.isEmpty(otherObjs)){ + if (ArrayUtil.isEmpty(otherObjs)) { error(obj1); - } else{ + } else { error(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1)); } } @@ -312,7 +323,7 @@ public class Console { * @param count 变量数量 * @return 模板 */ - private static String buildTemplateSplitBySpace(int count){ + private static String buildTemplateSplitBySpace(int count) { return StrUtil.repeatAndJoin(TEMPLATE_VAR, count, StrUtil.SPACE); } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ConsoleTable.java b/hutool-core/src/main/java/cn/hutool/core/lang/ConsoleTable.java index d219655eb..23066a045 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ConsoleTable.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ConsoleTable.java @@ -35,6 +35,16 @@ public class ConsoleTable { */ private List 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 表头列表或者表体列表 */ private void fillRow(StringBuilder sb, List> list) { diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/ConsoleTableTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/ConsoleTableTest.java index b92ddfd72..aba608f27 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/ConsoleTableTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/ConsoleTableTest.java @@ -1,35 +1,34 @@ package cn.hutool.core.lang; -import org.junit.Ignore; import org.junit.Test; public class ConsoleTableTest { - @Test - @Ignore - public void printTest() { - ConsoleTable t = new ConsoleTable(); - t.addHeader("姓名", "年龄"); - t.addBody("张三", "15"); - t.addBody("李四", "29"); - t.addBody("王二麻子", "37"); - t.print(); + @Test +// @Ignore + public void printTest() { + ConsoleTable t = new ConsoleTable(); + t.addHeader("姓名", "年龄"); + t.addBody("张三", "15"); + t.addBody("李四", "29"); + t.addBody("王二麻子", "37"); + t.print(); - Console.log(); + Console.log(); - t = new ConsoleTable(); - t.addHeader("体温", "占比"); - t.addHeader("℃", "%"); - t.addBody("36.8", "10"); - t.addBody("37", "5"); - t.print(); + t = new ConsoleTable(); + t.addHeader("体温", "占比"); + t.addHeader("℃", "%"); + t.addBody("36.8", "10"); + t.addBody("37", "5"); + t.print(); - Console.log(); + Console.log(); - t = new ConsoleTable(); - t.addHeader("标题1", "标题2"); - t.addBody("12345", "混合321654asdfcSDF"); - t.addBody("sd e3ee ff22", "ff值"); - t.print(); - } + t = new ConsoleTable(); + t.addHeader("标题1", "标题2"); + t.addBody("12345", "混合321654asdfcSDF"); + t.addBody("sd e3ee ff22", "ff值"); + t.print(); + } }