Enhance ConsoleTable initialization; allow custom max column length and adjust border calculation for better formatting in Result statistics
This commit is contained in:
@@ -21,13 +21,27 @@ public class ConsoleTable {
|
||||
* @throws NullPointerException if the array is null
|
||||
*/
|
||||
public ConsoleTable(String... header) {
|
||||
var max = 0;
|
||||
this(-1, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new table with the header passed as input.
|
||||
* The table will have as many columns as the length of the header array.
|
||||
* Each column will have the same size and will be the max length of all the
|
||||
* headers string or the maxLen passed as input.
|
||||
*
|
||||
* @param maxLen the max length of the columns
|
||||
* @param header an array of strings
|
||||
* @throws NullPointerException if the array is null
|
||||
*/
|
||||
public ConsoleTable(int maxLen, String... header) {
|
||||
var max = Math.max(0, maxLen);
|
||||
for (var name : header)
|
||||
max = Math.max(max, name.length());
|
||||
|
||||
this.columns = header.length;
|
||||
this.maxLen = max + 2;
|
||||
this.border = ("+" + "═".repeat(maxLen)).repeat(header.length) + "+\n";
|
||||
this.border = ("+" + "═".repeat(this.maxLen)).repeat(header.length) + "+\n";
|
||||
this.builder.append(border);
|
||||
this.addRow(header);
|
||||
}
|
||||
@@ -45,7 +59,7 @@ public class ConsoleTable {
|
||||
|
||||
for (var val : values) {
|
||||
var diff = maxLen - val.length();
|
||||
var first = (int) Math.ceil(diff / 2.0);
|
||||
var first = Math.max((int) Math.ceil(diff / 2.0), 0);
|
||||
builder.append('║');
|
||||
builder.append(" ".repeat(first));
|
||||
builder.append(val);
|
||||
|
||||
@@ -116,8 +116,14 @@ public class Result implements Iterable<Entry<String, NodeStats>> {
|
||||
var fFormat = "%" + (size + 4) + ".3f";
|
||||
var builder = new StringBuilder();
|
||||
|
||||
var table = new ConsoleTable("Node", "Departures", "Avg Queue", "Avg Wait", "Avg Response", "Throughput",
|
||||
"Utilization %", "Unavailable %", "Last Event");
|
||||
var maxNameLen = 0;
|
||||
for (var i = 0; i < nodes.length; i++) {
|
||||
var node = nodes[i];
|
||||
maxNameLen = Math.max(node.length(), maxNameLen);
|
||||
}
|
||||
|
||||
var table = new ConsoleTable(maxNameLen, "Node", "Departures", "Avg Queue", "Avg Wait", "Avg Response",
|
||||
"Throughput", "Utilization %", "Unavailable %", "Last Event");
|
||||
|
||||
for (var i = 0; i < nodes.length; i++) {
|
||||
var node = nodes[i];
|
||||
|
||||
Reference in New Issue
Block a user