浏览代码

add console.table

Looly 5 年之前
父节点
当前提交
97d48dde0b

+ 23 - 12
hutool-core/src/main/java/cn/hutool/core/lang/Console.java

@@ -48,14 +48,14 @@ public class Console {
 	 * 同 System.out.println()方法,打印控制台日志<br>
 	 * 如果传入打印对象为{@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()方法,打印控制台日志<br>
 	 * 如果传入打印对象为{@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()方法,打印控制台日志<br>
 	 * 如果传入打印对象为{@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);
 	}
 

+ 11 - 1
hutool-core/src/main/java/cn/hutool/core/lang/ConsoleTable.java

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

+ 23 - 24
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();
+	}
 }