-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TableFormatter.java
127 lines (112 loc) · 3.61 KB
/
TableFormatter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package me.nallar.tickthreading.util;
import java.util.ArrayList;
import java.util.List;
import me.nallar.tickthreading.util.stringfillers.StringFiller;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
public class TableFormatter {
public final StringFiller stringFiller;
public final StringBuilder sb = new StringBuilder();
private final List<String> currentHeadings = new ArrayList<String>();
private final List<String> currentData = new ArrayList<String>();
public String splitter = " | ";
public String headingSplitter = " | ";
public String headingColour = "";
public String rowColour = "";
public String tableSeparator = "";
public TableFormatter(ICommandSender commandSender) {
boolean chat = commandSender instanceof Entity;
stringFiller = chat ? StringFiller.CHAT : StringFiller.FIXED_WIDTH;
if (chat) {
splitter = " " + ChatFormat.YELLOW + '|' + ChatFormat.RESET + ' ';
headingSplitter = splitter;
headingColour = String.valueOf(ChatFormat.DARK_GREEN);
}
}
public TableFormatter(StringFiller stringFiller) {
this.stringFiller = stringFiller;
}
public TableFormatter heading(String text) {
currentHeadings.add(text);
return this;
}
public TableFormatter row(String data) {
currentData.add(data);
return this;
}
public TableFormatter row(Object data) {
return row(String.valueOf(data));
}
public TableFormatter row(int data) {
return row(String.valueOf(data));
}
public TableFormatter row(double data) {
currentData.add(formatDoubleWithPrecision(data, 3));
return this;
}
public void finishTable() {
int rowIndex = 0;
int rowCount = currentHeadings.size();
double[] rowLengths = new double[rowCount];
getMaxLengths(rowLengths, rowIndex, rowCount, currentHeadings);
getMaxLengths(rowLengths, rowIndex, rowCount, currentData);
String cSplit = "";
for (String heading : currentHeadings) {
sb.append(cSplit).append(headingColour).append(stringFiller.fill(heading, rowLengths[rowIndex % rowCount]));
cSplit = headingSplitter;
rowIndex++;
}
sb.append('\n');
cSplit = "";
rowIndex = 0;
for (String data : currentData) {
sb.append(cSplit).append(rowColour).append(stringFiller.fill(data, rowLengths[rowIndex % rowCount]));
cSplit = splitter;
rowIndex++;
if (rowIndex % rowCount == 0 && rowIndex != currentData.size()) {
sb.append('\n');
cSplit = "";
}
}
sb.append(tableSeparator);
currentHeadings.clear();
currentData.clear();
}
private int getMaxLengths(double[] rowLengths, int rowIndex, int rowCount, Iterable<String> stringIterable) {
for (String data : stringIterable) {
double length = stringFiller.getLength(data);
if (rowLengths[rowIndex % rowCount] < length) {
rowLengths[rowIndex % rowCount] = length;
}
rowIndex++;
}
return rowIndex;
}
private static final int POW10[] = {1, 10, 100, 1000, 10000, 100000, 1000000};
/*
* http://stackoverflow.com/a/10554128/250076
*/
public static String formatDoubleWithPrecision(double val, int precision) {
if (Double.isInfinite(val) || Double.isNaN(val)) {
return Double.toString(val);
}
StringBuilder sb = new StringBuilder();
if (val < 0) {
sb.append('-');
val = -val;
}
int exp = POW10[precision];
long lval = (long) (val * exp + 0.5);
sb.append(lval / exp).append('.');
long fval = lval % exp;
for (int p = precision - 1; p > 0 && fval < POW10[p]; p--) {
sb.append('0');
}
sb.append(fval);
return sb.toString();
}
@Override
public String toString() {
return sb.toString();
}
}