TextTable { public static String makeTable(String[] columnNames, int sortIndex, Object[][] data, boolean rowNumbering) { StringBuilder builder = new StringBuilder(); int[] colLengths = calcColLengths(columnNames, data); int allColsLen = 0; for (int i : colLengths) { allColsLen += i; } int totalLegth = columnNames.length * 1 + (columnNames.length + 1) + allColsLen; int paddingLen = 0; if (rowNumbering && data.length > 0) { int len = data.length; while (len > 0) { paddingLen++; len /= 10; } } if (rowNumbering) { builder.append(StringUtils.repeat(" ", paddingLen + 2)); } builder.append(StringUtils.repeat("_", totalLegth)); builder.append("\n"); if (rowNumbering) { builder.append(StringUtils.repeat(" ", paddingLen + 2)); } for (int i = 0; i < columnNames.length; i++) { builder.append(String.format("| %-" + colLengths[i] + "s", columnNames[i])); } builder.append("|"); builder.append("\n"); if (rowNumbering) { builder.append(StringUtils.repeat(" ", paddingLen + 2)); } builder.append(StringUtils.repeat("=", totalLegth)); for (int x = 0; x < data.length; x++) { Object[] row = data[x]; builder.append("\n"); if (rowNumbering) { builder.append(String.format("%" + paddingLen + "d. ", x + 1)); } for (int i = 0; i < row.length; i++) { builder.append(String.format("| %-" + colLengths[i] + "s", row[i])); } builder.append("|"); } return builder.toString(); }  static String makeTable(String[] columnNames, int sortIndex, Object[][] data, boolean rowNumbering); static int realDataLen(Object data);  }
@Test public void testMakeTable() throws IOException, KaramelException { Settings.CB_CLASSPATH_MODE = true; String ymlString = Resources.toString(Resources.getResource("se/kth/karamel/client/model/test-definitions/hopsworks.yml"), Charsets.UTF_8); JsonCluster definition = ClusterDefinitionService.yamlToJsonObject(ymlString); ClusterRuntime dummyRuntime = MockingUtil.dummyRuntime(definition); List<MachineRuntime> machines = dummyRuntime.getGroups().get(1).getMachines(); String[] columnNames = {"Machine", "Public IP", "Private IP", "SSH Port", "SSH User", "Life Status", "Task Status"}; String[][] data = new String[machines.size()][columnNames.length]; for (int i = 0; i < machines.size(); i++) { MachineRuntime machine = machines.get(i); data[i][0] = machine.getName(); data[i][1] = "<a kref='shellconnect " + machine.getPublicIp() + "'>" + machine.getPublicIp() + "</a>"; data[i][2] = machine.getPrivateIp(); data[i][3] = machine.getSshPort() + ""; data[i][4] = machine.getSshUser(); data[i][5] = machine.getLifeStatus() + ""; data[i][6] = machine.getTasksStatus() + ""; } String table = TextTable.makeTable(columnNames, 0, data, true); System.out.println(table); table = TextTable.makeTable(columnNames, 0, data, false); System.out.println(table); }