davsclaus commented on code in PR #23152:
URL: https://github.com/apache/camel/pull/23152#discussion_r3227705306


##########
components/camel-diagram/src/main/java/org/apache/camel/diagram/RouteDiagramAsciiRenderer.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.diagram;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.Bounds;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.LayoutNode;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.LayoutRoute;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.TreeNode;
+
+import static org.apache.camel.diagram.RouteDiagramLayoutEngine.PADDING;
+import static org.apache.camel.diagram.RouteDiagramLayoutEngine.SCOPE_BOX_PAD;
+
+/**
+ * Renders route diagrams as plain ASCII art or Unicode box-drawing text.
+ */
+public class RouteDiagramAsciiRenderer {
+
+    private static final int MAX_WRAP_LINES = 3;
+    private static final int Y_SCALE = 20;
+    private static final int MIN_BOX_WIDTH = 16;
+    private static final int X_DIVISOR = 15;
+
+    // Unicode box-drawing characters
+    private static final char UNI_H = '─';     // ─
+    private static final char UNI_V = '│';     // │
+    private static final char UNI_TL = '┌';    // ┌
+    private static final char UNI_TR = '┐';    // ┐
+    private static final char UNI_BL = '└';    // └
+    private static final char UNI_BR = '┘';    // ┘
+    private static final char UNI_T_DOWN = '┬'; // ┬
+    private static final char UNI_T_UP = '┴';  // ┴
+    private static final char UNI_T_RIGHT = '├'; // ├
+    private static final char UNI_T_LEFT = '┤'; // ┤
+    private static final char UNI_CROSS = '┼'; // ┼
+    private static final char UNI_ARROW = '▼'; // ▼
+    private static final char UNI_DASH_H = '╌'; // ╌
+    private static final char UNI_DASH_V = '╎'; // ╎
+

Review Comment:
   nit: `UNI_T_RIGHT` and `UNI_T_LEFT` are declared but never used in the 
current implementation. Consider removing them to keep the code clean, or add a 
brief comment if they're reserved for future junction handling.



##########
components/camel-diagram/src/main/java/org/apache/camel/diagram/RouteDiagramAsciiRenderer.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.diagram;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.Bounds;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.LayoutNode;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.LayoutRoute;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.TreeNode;
+
+import static org.apache.camel.diagram.RouteDiagramLayoutEngine.PADDING;
+import static org.apache.camel.diagram.RouteDiagramLayoutEngine.SCOPE_BOX_PAD;
+
+/**
+ * Renders route diagrams as plain ASCII art or Unicode box-drawing text.
+ */
+public class RouteDiagramAsciiRenderer {
+
+    private static final int MAX_WRAP_LINES = 3;
+    private static final int Y_SCALE = 20;
+    private static final int MIN_BOX_WIDTH = 16;
+    private static final int X_DIVISOR = 15;
+
+    // Unicode box-drawing characters
+    private static final char UNI_H = '─';     // ─
+    private static final char UNI_V = '│';     // │
+    private static final char UNI_TL = '┌';    // ┌
+    private static final char UNI_TR = '┐';    // ┐
+    private static final char UNI_BL = '└';    // └
+    private static final char UNI_BR = '┘';    // ┘
+    private static final char UNI_T_DOWN = '┬'; // ┬
+    private static final char UNI_T_UP = '┴';  // ┴
+    private static final char UNI_T_RIGHT = '├'; // ├
+    private static final char UNI_T_LEFT = '┤'; // ┤
+    private static final char UNI_CROSS = '┼'; // ┼
+    private static final char UNI_ARROW = '▼'; // ▼
+    private static final char UNI_DASH_H = '╌'; // ╌
+    private static final char UNI_DASH_V = '╎'; // ╎
+
+    private final int nodeWidth;
+    private final int boxWidth;
+    private final boolean unicode;
+
+    public RouteDiagramAsciiRenderer(int nodeWidth) {
+        this(nodeWidth, false);
+    }
+
+    public RouteDiagramAsciiRenderer(int nodeWidth, boolean unicode) {
+        this.nodeWidth = nodeWidth;
+        this.boxWidth = Math.max(MIN_BOX_WIDTH, nodeWidth / X_DIVISOR);
+        this.unicode = unicode;
+    }
+
+    public int getBoxWidth() {
+        return boxWidth;
+    }
+
+    public String renderDiagram(List<LayoutRoute> layoutRoutes, int 
totalHeight) {
+        int maxPixelX = layoutRoutes.stream()
+                .mapToInt(lr -> lr.maxX).max().orElse(nodeWidth) + PADDING;
+        int gridWidth = toCol(maxPixelX) + boxWidth + 4;
+        int gridHeight = totalHeight / Y_SCALE + 20;
+
+        char[][] grid = new char[gridHeight][gridWidth];
+        for (char[] row : grid) {
+            Arrays.fill(row, ' ');
+        }
+
+        for (LayoutRoute lr : layoutRoutes) {
+            drawRoute(grid, lr);
+        }
+
+        return gridToString(grid);
+    }
+
+    private void drawRoute(char[][] grid, LayoutRoute lr) {
+        int labelRow = toRow(lr.labelY);
+        String label = lr.routeId;
+        if (lr.source != null && !lr.source.isEmpty()) {
+            label += " (" + lr.source + ")";
+        }
+        drawText(grid, labelRow, toCol(PADDING), label);
+
+        for (LayoutNode ln : lr.nodes) {
+            if (ln.treeNode != null && 
RouteDiagramLayoutEngine.hasScope(ln.treeNode)) {
+                drawScopeBox(grid, ln);
+            }
+        }
+
+        for (LayoutNode ln : lr.nodes) {
+            if (ln.parentNode != null) {
+                if (ln.connectFromMerge) {
+                    drawMergeArrow(grid, ln);
+                } else {
+                    drawArrow(grid, ln.parentNode, ln);
+                }
+            }
+        }
+
+        for (LayoutNode ln : lr.nodes) {
+            drawNode(grid, ln);
+        }
+    }
+
+    private void drawNode(char[][] grid, LayoutNode node) {
+        int col = toCol(node.x);
+        int row = toRow(node.y);
+        int innerWidth = boxWidth - 4;
+        List<String> lines = rewrapText(node, innerWidth);
+        int height = 2 + lines.size();
+
+        if (row + height >= grid.length) {
+            return;
+        }
+
+        char h = unicode ? UNI_H : '-';
+        char v = unicode ? UNI_V : '|';
+
+        setChar(grid, row, col, unicode ? UNI_TL : '+');
+        for (int c = col + 1; c < col + boxWidth - 1; c++) {
+            setChar(grid, row, c, h);
+        }
+        setChar(grid, row, col + boxWidth - 1, unicode ? UNI_TR : '+');
+
+        int bottom = row + height - 1;
+        setChar(grid, bottom, col, unicode ? UNI_BL : '+');
+        for (int c = col + 1; c < col + boxWidth - 1; c++) {
+            setChar(grid, bottom, c, h);
+        }
+        setChar(grid, bottom, col + boxWidth - 1, unicode ? UNI_BR : '+');
+
+        for (int i = 0; i < lines.size(); i++) {
+            int r = row + 1 + i;
+            setChar(grid, r, col, v);
+            setChar(grid, r, col + boxWidth - 1, v);
+            for (int c = col + 1; c < col + boxWidth - 1; c++) {
+                setChar(grid, r, c, ' ');
+            }
+            String text = lines.get(i);
+            if (text.length() > innerWidth) {
+                text = text.substring(0, Math.max(1, innerWidth - 3)) + "...";
+            }
+            int textCol = col + 2 + Math.max(0, (innerWidth - text.length()) / 
2);
+            drawText(grid, r, textCol, text);
+        }
+    }
+
+    private void drawArrow(char[][] grid, LayoutNode from, LayoutNode to) {
+        int fromCx = centerCol(from);
+        int fromBottom = toRow(from.y) + boxHeight(from);
+        int toCx = centerCol(to);
+        int toTop = getTopRow(to);
+
+        drawArrowPath(grid, fromCx, fromBottom, toCx, toTop);
+    }
+
+    private void drawMergeArrow(char[][] grid, LayoutNode to) {
+        int fromCx = toCol(to.mergeCx);
+        int fromRow = toRow(to.mergeY);
+        int toCx = centerCol(to);
+        int toTop = getTopRow(to);
+
+        drawArrowPath(grid, fromCx, fromRow, toCx, toTop);
+    }
+
+    private void drawArrowPath(char[][] grid, int fromCx, int fromRow, int 
toCx, int toRow) {
+        if (fromRow >= toRow) {
+            return;
+        }
+
+        char v = unicode ? UNI_V : '|';
+        char h = unicode ? UNI_H : '-';
+        char arrow = unicode ? UNI_ARROW : 'v';
+
+        if (fromCx == toCx) {
+            for (int r = fromRow; r < toRow - 1; r++) {
+                plotLine(grid, r, fromCx, v);
+            }
+            setChar(grid, toRow - 1, toCx, arrow);
+        } else {
+            int midRow = fromRow + (toRow - fromRow) / 2;
+
+            for (int r = fromRow; r < midRow; r++) {
+                plotLine(grid, r, fromCx, v);
+            }
+
+            int minC = Math.min(fromCx, toCx);
+            int maxC = Math.max(fromCx, toCx);
+            for (int c = minC; c <= maxC; c++) {
+                plotLine(grid, midRow, c, h);
+            }
+
+            if (unicode) {
+                setChar(grid, midRow, fromCx, UNI_T_UP);
+                setChar(grid, midRow, toCx, UNI_T_DOWN);
+            } else {
+                setChar(grid, midRow, fromCx, '+');
+                setChar(grid, midRow, toCx, '+');
+            }
+
+            for (int r = midRow + 1; r < toRow - 1; r++) {
+                plotLine(grid, r, toCx, v);
+            }
+            setChar(grid, toRow - 1, toCx, arrow);
+        }
+    }
+
+    private void drawScopeBox(char[][] grid, LayoutNode scopeNode) {
+        TreeNode tn = scopeNode.treeNode;
+        Bounds bounds = new Bounds(
+                scopeNode.x, scopeNode.y,
+                scopeNode.x + nodeWidth, scopeNode.y + scopeNode.height);
+        for (TreeNode child : tn.children) {
+            RouteDiagramLayoutEngine.expandBoundsForBox(child, bounds, 
nodeWidth);
+        }
+
+        int col1 = toCol(bounds.minX - SCOPE_BOX_PAD);
+        int row1 = toRow(bounds.minY - SCOPE_BOX_PAD);
+        int col2 = toCol(bounds.maxX + SCOPE_BOX_PAD);
+        int row2 = toRow(bounds.maxY + SCOPE_BOX_PAD);
+
+        if (unicode) {
+            for (int c = col1; c <= col2; c++) {
+                setChar(grid, row1, c, UNI_DASH_H);
+                setChar(grid, row2, c, UNI_DASH_H);
+            }
+            for (int r = row1 + 1; r < row2; r++) {
+                setChar(grid, r, col1, UNI_DASH_V);
+                setChar(grid, r, col2, UNI_DASH_V);
+            }
+        } else {
+            drawDashedHLine(grid, row1, col1, col2);
+            drawDashedHLine(grid, row2, col1, col2);
+            for (int r = row1 + 1; r < row2; r++) {
+                setChar(grid, r, col1, ':');
+                setChar(grid, r, col2, ':');
+            }
+        }
+    }
+
+    private void drawDashedHLine(char[][] grid, int row, int col1, int col2) {
+        for (int c = col1; c <= col2; c++) {
+            if (c == col1 || c == col2) {
+                setChar(grid, row, c, '+');
+            } else if ((c - col1) % 2 == 0) {
+                setChar(grid, row, c, '-');
+            }
+        }
+    }
+
+    private int getTopRow(LayoutNode node) {
+        if (node.treeNode != null && 
RouteDiagramLayoutEngine.hasScope(node.treeNode)) {
+            return toRow(node.y - SCOPE_BOX_PAD);
+        }

Review Comment:
   nit: Adding explicit parentheses would improve readability here, since `&&` 
and `||` precedence is easy to misread:
   
   ```suggestion
           if ((isVertical(current) && isHorizontal(ch)) || 
(isHorizontal(current) && isVertical(ch))) {
   ```



##########
components/camel-diagram/src/main/docs/diagram.adoc:
##########
@@ -10,14 +10,15 @@
 *Since Camel {since}*
 
 The Diagram module provides route diagram rendering capabilities for Apache 
Camel routes.
-It can generate visual route diagrams as PNG images representations from route 
structure data.
+It can generate visual route diagrams as PNG images or plain ASCII art text 
representations from route structure data.
 
 == Features
 
 * Render route diagrams as PNG images with colored nodes and scope boxes
+* Render route diagrams as plain ASCII art text for terminal output
 * Support for all Camel EIPs: choice, doTry/doCatch, filter, split, loop, 
multicast, and more
-* Scope boxes visually group branching and scoping EIPs
-* Multiple color themes: dark, light, transparent, or custom
+* Scope boxes visually group branching and scoping EIPs (PNG only)

Review Comment:
   This line says `(PNG only)` but scope boxes are now also rendered in ASCII 
and Unicode themes. Consider removing the `(PNG only)` annotation.
   
   ```suggestion
   * Scope boxes visually group branching and scoping EIPs
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to