diff --git a/src/main/java/com/cube/shell/CubeShell.java b/src/main/java/com/cube/shell/CubeShell.java index 48c13ec..d3164ee 100644 --- a/src/main/java/com/cube/shell/CubeShell.java +++ b/src/main/java/com/cube/shell/CubeShell.java @@ -134,93 +134,96 @@ sb.append("\n"); } + if (!text.isEmpty()) { - if ((((align & FMT_TABLE) == FMT_TABLE) && (align & FMT_TABLE_END) != FMT_TABLE_END) &&((align & FMT_TABLE_SEPRATOR) == FMT_TABLE_SEPRATOR)) { - int startLen=len; - if (table) { - startLen= startLen -2; - } - - if (table) { - sb.append(TABLE_SEPRATOR_LEFT); - } - - sb.append(TABLE_BORDER_HORIZANTAL.repeat(startLen)); - if (table) { - sb.append(TABLE_SEPRATOR_RIGHT); - } - sb.append("\n"); - } - - if ((align & FMT_CENTER_ALIGN) == FMT_CENTER_ALIGN) { - int textLen= text.length(); - int centerLen= len; - if (table) { - centerLen = centerLen -2; - } - if (textLen < centerLen) { - int padLen= centerLen-textLen; - int leftPadLen= padLen/2; - int rightPadLen=centerLen- (leftPadLen +textLen); - if (table) { - sb.append(TABLE_BORDER_VERTICAL); - } - - sb.append(filler.repeat(leftPadLen)); - sb.append(text); - sb.append(filler.repeat(rightPadLen)); - if (table) { - sb.append(TABLE_BORDER_VERTICAL); - } - } else { - sb.append(text); - } - } else if ((align & FMT_LEFT_ALIGN) == FMT_LEFT_ALIGN) { - int textLen= text.length(); - int leftLen= len; - if (table) { - leftLen = leftLen -2; - } - if (textLen < leftLen) { - - int rightPadLen=leftLen-textLen; + if ((((align & FMT_TABLE) == FMT_TABLE) && ((align & FMT_TABLE_SEPRATOR) == FMT_TABLE_SEPRATOR))) { + int startLen = len; if (table) { - sb.append(TABLE_BORDER_VERTICAL); + startLen = startLen - 2; } - sb.append(text); - sb.append(filler.repeat(rightPadLen)); if (table) { - sb.append(TABLE_BORDER_VERTICAL); + sb.append(TABLE_SEPRATOR_LEFT); } - } else { - sb.append(text); - } - } else if ((align & FMT_RIGHT_ALIGN) == FMT_RIGHT_ALIGN) { - int textLen= text.length(); - int rightLen= len; - if (table) { - rightLen = rightLen -2; - } - if (textLen < rightLen) { - int leftPadLen=rightLen-textLen; + sb.append(TABLE_BORDER_HORIZANTAL.repeat(startLen)); if (table) { - sb.append(TABLE_BORDER_VERTICAL); + sb.append(TABLE_SEPRATOR_RIGHT); } - sb.append(filler.repeat(leftPadLen)); - sb.append(text); + sb.append("\n"); + } + if ((align & FMT_CENTER_ALIGN) == FMT_CENTER_ALIGN) { + int textLen = text.length(); + int centerLen = len; if (table) { - sb.append(TABLE_BORDER_VERTICAL); + centerLen = centerLen - 2; } - } else { - sb.append(text); + if (textLen < centerLen) { + int padLen = centerLen - textLen; + int leftPadLen = padLen / 2; + int rightPadLen = centerLen - (leftPadLen + textLen); + if (table) { + sb.append(TABLE_BORDER_VERTICAL); + } + + sb.append(filler.repeat(leftPadLen)); + sb.append(text); + sb.append(filler.repeat(rightPadLen)); + if (table) { + sb.append(TABLE_BORDER_VERTICAL); + } + } else { + sb.append(text); + } + } else if ((align & FMT_LEFT_ALIGN) == FMT_LEFT_ALIGN) { + int textLen = text.length(); + int leftLen = len; + if (table) { + leftLen = leftLen - 2; + } + if (textLen < leftLen) { + + int rightPadLen = leftLen - textLen; + if (table) { + sb.append(TABLE_BORDER_VERTICAL); + } + + sb.append(text); + sb.append(filler.repeat(rightPadLen)); + if (table) { + sb.append(TABLE_BORDER_VERTICAL); + } + } else { + sb.append(text); + } + } else if ((align & FMT_RIGHT_ALIGN) == FMT_RIGHT_ALIGN) { + int textLen = text.length(); + int rightLen = len; + if (table) { + rightLen = rightLen - 2; + } + if (textLen < rightLen) { + + int leftPadLen = rightLen - textLen; + if (table) { + sb.append(TABLE_BORDER_VERTICAL); + } + sb.append(filler.repeat(leftPadLen)); + sb.append(text); + + if (table) { + sb.append(TABLE_BORDER_VERTICAL); + } + } else { + sb.append(text); + } } } - if (((align & FMT_TABLE) == FMT_TABLE) && (align & FMT_TABLE_END) == FMT_TABLE_END) { - sb.append("\n"); + if (!text.isEmpty()) { + sb.append("\n"); + } int startLen=len; if (table) { startLen= startLen -2; @@ -506,13 +509,12 @@ } private void showClusterInfo() { - System.out.println("\n╔════════════════════════════════════════════════════════════╗"); - System.out.println("║ Cluster Nodes ║"); - System.out.println("╠════════════════════════════════════════════════════════════╣"); - + + System.out.println( fillFormat(TABLE_MAX_LENGTH, " Cluster Nodes ", " ", FMT_CENTER_ALIGN |FMT_TABLE, true)); + + if (clusterNodes.isEmpty()) { - System.out.println("║ No nodes connected ║"); - System.out.println("╚════════════════════════════════════════════════════════════╝\n"); + System.out.println( fillFormat(TABLE_MAX_LENGTH, " No nodes connected ", " ", FMT_CENTER_ALIGN |FMT_TABLE|FMT_TABLE_SEPRATOR, true)); return; } @@ -520,18 +522,24 @@ String current = (node.equals(currentNode)) ? "➜ " : " "; String status = node.isAlive() ? "✓" : "✗"; - System.out.printf("║ %s%s %-20s %s %-25s ║%n", + String text=String.format("%s%s %-20s %s %-25s", current, status, node.getNodeId(), node.getEndpoint(), "DC:" + node.getDatacenter()); + + System.out.println( fillFormat(TABLE_MAX_LENGTH, text, " ", FMT_CENTER_ALIGN |FMT_TABLE|FMT_TABLE_SEPRATOR, true)); + } - - System.out.println("╠════════════════════════════════════════════════════════════╣"); - System.out.printf("║ Total Nodes: %-3d Alive: %-3d Current: %-18s║%n", + + //System.out.println("╠════════════════════════════════════════════════════════════╣"); + String text=String.format("Total Nodes: %-3d Alive: %-3d Current: %-18s", clusterNodes.size(), clusterNodes.stream().filter(ClusterNode::isAlive).count(), currentNode != null ? currentNode.getNodeId() : "none"); - System.out.println("╚════════════════════════════════════════════════════════════╝\n"); + + + System.out.println( fillFormat(TABLE_MAX_LENGTH, text, " ", FMT_LEFT_ALIGN |FMT_TABLE|FMT_TABLE_SEPRATOR|FMT_TABLE_END, true)); + } private void handleUseNode(String args) { @@ -625,7 +633,15 @@ } private void showReplicationStats() { - System.out.println("\n╔════════════════════════════════════════════════════════════╗"); + System.out.println( fillFormat(TABLE_MAX_LENGTH, " Replication Statistics ", " ", FMT_CENTER_ALIGN |FMT_TABLE, true)); + System.out.println( fillFormat(TABLE_MAX_LENGTH, String.format("Cluster Nodes: %-33d", clusterNodes.size()), " ", FMT_LEFT_ALIGN , true)); + System.out.println( fillFormat(TABLE_MAX_LENGTH, String.format("Alive Nodes: %-33d", clusterNodes.stream().filter(ClusterNode::isAlive).count()), " ", FMT_LEFT_ALIGN , true)); + System.out.println( fillFormat(TABLE_MAX_LENGTH, String.format("Default Consistency: %-33s", defaultConsistencyLevel), " ", FMT_LEFT_ALIGN , true)); + System.out.println( fillFormat(TABLE_MAX_LENGTH, "Datacenter Distribution:", " ", FMT_LEFT_ALIGN |FMT_TABLE|FMT_TABLE_SEPRATOR, true)); + + + //Replication Statistics + /* System.out.println("\n╔════════════════════════════════════════════════════════════╗"); System.out.println("║ Replication Statistics ║"); System.out.println("╠════════════════════════════════════════════════════════════╣"); System.out.printf("║ Cluster Nodes: %-33d ║%n", clusterNodes.size()); @@ -634,15 +650,18 @@ System.out.printf("║ Default Consistency: %-33s ║%n", defaultConsistencyLevel); System.out.println("╠════════════════════════════════════════════════════════════╣"); System.out.println("║ Datacenter Distribution: ║"); - + */ Map dcCount = clusterNodes.stream() .collect(Collectors.groupingBy(ClusterNode::getDatacenter, Collectors.counting())); for (Map.Entry entry : dcCount.entrySet()) { - System.out.printf("║ %-20s %-33d ║%n", entry.getKey() + ":", entry.getValue()); + // System.out.printf("║ %-20s %-33d ║%n", entry.getKey() + ":", entry.getValue()); + System.out.println( fillFormat(TABLE_MAX_LENGTH, String.format(" %-20s %-33d ", entry.getKey() + ":", entry.getValue()), " ", FMT_LEFT_ALIGN |FMT_TABLE|FMT_TABLE_SEPRATOR, true)); + } - - System.out.println("╚════════════════════════════════════════════════════════════╝\n"); + + System.out.println( fillFormat(TABLE_MAX_LENGTH, "", " ", FMT_LEFT_ALIGN |FMT_TABLE|FMT_TABLE_END, true)); + } private void handlePut(String args) {