最近在學習使用java來編寫cli應用,可是在信息展現上碰到了難題。緣由是沒有很好工具來展現一個由字符串組成的表格。在git上搜到阿里巴巴有一個叫作 text-ui 的開源項目能夠用,可是這個工具在製做表格的時候若是表格內容是中英文混合的,表格就會亂掉。因而就本身寫了一個工具類來打印一個字符串組成的表格java
package com.hebaibai.ascmd.text;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TextForm {
/**
* 左邊距
*/
protected int paddingL = 1;
/**
* 右邊距
*/
protected int paddingR = 1;
/**
* 標題
*/
protected List<String> title = new ArrayList<>();
/**
* 數據
*/
protected List<List<String>> datas = new ArrayList<>();
/**
* 最大列數
*/
protected int maxCol = 0;
/**
* 每一個單元格最大字符數
*/
protected int colMaxLength = 8;
/**
* 表格組成符號
*/
protected char separator = '.';
private TextForm() {
}
public static TextFormBulider bulider() {
return new TextFormBulider(new TextForm());
}
/**
* 格式化輸出表格
*/
public void printFormat() {
List<List<String>> formData = new ArrayList<>();
formData.add(title);
formData.addAll(datas);
Map<Integer, Integer> colMaxLengthMap = colMaxLength(formData);
for (int i = 0; i < formData.size(); i++) {
List<String> row = formData.get(i);
for (int j = 0; j < row.size(); j++) {
Formatter formatter = new Formatter();
String str = row.get(j);
if (str.length() > colMaxLength) {
str = str.substring(0, colMaxLength);
}
int chineseNum = getChineseNum(str);
Integer maxLength = colMaxLengthMap.get(j);
String val = formatter.format("%-" + (maxLength - chineseNum) + "s", str).toString();
row.set(j, val);
}
}
Map<Integer, Integer> colMinBlankLengthMap = colMinBlankLength(formData);
for (int i = 0; i < formData.size(); i++) {
List<String> row = formData.get(i);
for (int j = 0; j < row.size(); j++) {
String val = row.get(j);
Integer minBlankLength = colMinBlankLengthMap.get(j);
val = val.substring(0, val.length() - minBlankLength);
row.set(j, val);
}
}
String line = "";
List<String> rows = new ArrayList<>();
for (List<String> strings : formData) {
String pL = StringUtils.repeat(" ", paddingL);
String pR = StringUtils.repeat(" ", paddingR);
String row = separator + pL + String.join(pL + separator + pR, strings) + pR + separator;
if (line.length() < row.length()) {
line = StringUtils.repeat(separator, row.length());
}
rows.add(row);
}
System.out.println(line);
for (String row : rows) {
System.out.println(row);
System.out.println(line);
}
}
/**
* 找到每一列最大的長度
*
* @param formData
* @return
*/
private Map<Integer, Integer> colMaxLength(List<List<String>> formData) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < formData.size(); i++) {
int col = 0;
List<String> strings = formData.get(i);
while (strings.size() > col) {
String val = strings.get(col);
if (val.length() > colMaxLength) {
val = val.substring(0, colMaxLength);
strings.set(col, val);
}
int length = val.getBytes().length;
Integer integer = map.get(col);
if (integer == null) {
map.put(col, length);
} else {
if (integer < length) {
map.put(col, length);
}
}
col++;
}
}
return map;
}
/**
* 找到每一列從右開始最小的空格長度
*
* @param formData
* @return
*/
private Map<Integer, Integer> colMinBlankLength(List<List<String>> formData) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < formData.size(); i++) {
int col = 0;
List<String> strings = formData.get(i);
while (strings.size() > col) {
String val = strings.get(col);
int length = 0;
for (int i1 = val.length() - 1; i1 >= 0; i1--) {
if (val.charAt(i1) == ' ') {
length++;
} else {
break;
}
}
Integer integer = map.get(col);
if (integer == null) {
map.put(col, length);
} else {
if (integer > length) {
map.put(col, length);
}
}
col++;
}
}
return map;
}
/**
* 獲取中文數量
*
* @param val
* @return
*/
private int getChineseNum(String val) {
if (val == null) {
val = "null";
}
String regex = "[\u4e00-\u9fa5|。|,]";
ArrayList<String> list = new ArrayList<String>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(val);
while (matcher.find()) {
list.add(matcher.group());
}
int size = list.size();
return size;
}
}
複製代碼
package com.hebaibai.ascmd.text;
import java.util.ArrayList;
import java.util.List;
public class TextFormBulider {
private TextForm textForm;
protected TextFormBulider(TextForm textForm) {
this.textForm = textForm;
}
public TextFormBulider title(String... titles) {
if (textForm.maxCol < titles.length) {
textForm.maxCol = titles.length;
}
for (String title : titles) {
if (title == null) {
title = "null";
}
textForm.title.add(title);
}
return this;
}
public TextFormBulider paddingL(int paddingL) {
textForm.paddingL = paddingL;
return this;
}
public TextFormBulider paddingR(int paddingR) {
textForm.paddingR = paddingR;
return this;
}
public TextFormBulider separator(char separator) {
textForm.separator = separator;
return this;
}
public TextFormBulider colMaxLength(int colMaxLength) {
textForm.colMaxLength = colMaxLength;
return this;
}
public TextFormBulider addRow(String... cols) {
if (textForm.maxCol < cols.length) {
textForm.maxCol = cols.length;
}
List<String> list = new ArrayList<>(cols.length);
for (String col : cols) {
if (col == null) {
col = "null";
}
list.add(col);
}
textForm.datas.add(list);
return this;
}
public TextForm finish() {
int titleSize = textForm.title.size();
if (titleSize < textForm.maxCol) {
for (int i = 0; i < textForm.maxCol - titleSize; i++) {
textForm.title.add(null);
}
}
for (List<String> data : textForm.datas) {
int dataSize = data.size();
if (dataSize < textForm.maxCol) {
for (int i = 0; i < textForm.maxCol - dataSize; i++) {
data.add(null);
}
}
}
return textForm;
}
}
複製代碼
package com.hebaibai.ascmd.text;
import org.junit.Test;
import static org.junit.Assert.*;
public class TextFormTest {
@Test
public void printFormat() {
TextForm.bulider()
.title("name", "age", "sex")//設置標題
.addRow("王麻345子", "21", "男2")//添加行
.addRow("wzeefgrerhei", "21", "男")//添加行
.colMaxLength(5)//設置單元格最大數據長度
.separator('*')//設置表格由什麼符號構成
.paddingR(2)//右邊距
.paddingL(2)//左邊距
.finish()//完成
.printFormat();//打印
}
}
複製代碼
原文連接 :www.hebaibai.com/?p=371git