You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/02/17 10:09:32 UTC

[camel] branch main updated: camel-jbang - Only show prefix if 2+ running

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new f760094ea99 camel-jbang - Only show prefix if 2+ running
f760094ea99 is described below

commit f760094ea999050ff56d89b70453e387d378b91a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Feb 17 11:09:16 2023 +0100

    camel-jbang - Only show prefix if 2+ running
---
 .../jbang/core/commands/action/CamelLogAction.java   | 20 +++++++++++++-------
 .../jbang/core/commands/action/CamelTraceAction.java | 20 ++++++++++++--------
 2 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
index d3c22828f97..1da4b7eb886 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
@@ -66,9 +66,9 @@ public class CamelLogAction extends ActionBaseCommand {
                         description = "Keep following and outputting new log lines (use ctrl + c to exit).")
     boolean follow = true;
 
-    @CommandLine.Option(names = { "--prefix" }, defaultValue = "true",
-                        description = "Print prefix with running Camel integration name.")
-    boolean prefix = true;
+    @CommandLine.Option(names = { "--prefix" }, defaultValue = "auto",
+                        description = "Print prefix with running Camel integration name. auto=only prefix when running multiple integrations. true=always prefix. false=prefix off.")
+    String prefix = "auto";
 
     @CommandLine.Option(names = { "--tail" }, defaultValue = "-1",
                         description = "The number of lines from the end of the logs to show. Use -1 to read from the beginning. Use 0 to read only new lines. Defaults to showing all logs from beginning.")
@@ -89,6 +89,7 @@ public class CamelLogAction extends ActionBaseCommand {
     String findAnsi;
 
     private int nameMaxWidth;
+    private boolean prefixShown;
 
     private final Map<String, Ansi.Color> colors = new HashMap<>();
 
@@ -319,14 +320,19 @@ public class CamelLogAction extends ActionBaseCommand {
         lines.forEach(l -> {
             String name = StringHelper.before(l, "| ");
             String line = StringHelper.after(l, "| ");
-            printLine(name, line);
+            printLine(name, rows.size(), line);
         });
     }
 
-    protected void printLine(String name, String line) {
-        if (!prefix) {
-            name = null;
+    protected void printLine(String name, int pids, String line) {
+        if (!prefixShown) {
+            // compute whether to show prefix or not
+            if ("false".equals(prefix) || "auto".equals(prefix) && pids <= 1) {
+                name = null;
+            }
         }
+        prefixShown = name != null;
+
         if (!timestamp) {
             // after timestamp is after 2 sine-space
             int pos = line.indexOf(' ');
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
index ad0221c4a8a..8763e497765 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
@@ -74,10 +74,9 @@ public class CamelTraceAction extends ActionBaseCommand {
                         description = "Keep following and outputting new traces (use ctrl + c to exit).")
     boolean follow = true;
 
-    @CommandLine.Option(names = { "--prefix" }, defaultValue = "true",
-                        description = "Print prefix with running Camel integration name.")
-    boolean prefix = true;
-    // TODO: true|false|auto (same for log)
+    @CommandLine.Option(names = { "--prefix" }, defaultValue = "auto",
+                        description = "Print prefix with running Camel integration name. auto=only prefix when running multiple integrations. true=always prefix. false=prefix off.")
+    String prefix = "auto";
 
     @CommandLine.Option(names = { "--source" },
                         description = "Prefer to display source filename/code instead of IDs")
@@ -133,6 +132,7 @@ public class CamelTraceAction extends ActionBaseCommand {
     String findAnsi;
 
     private int nameMaxWidth;
+    private boolean prefixShown;
 
     private final Map<String, Ansi.Color> nameColors = new HashMap<>();
     private final Map<String, Ansi.Color> exchangeIdColors = new HashMap<>();
@@ -472,7 +472,7 @@ public class CamelTraceAction extends ActionBaseCommand {
 
         int doneTraces = 0;
         for (Row r : rows) {
-            printTrace(r.name, r, limit);
+            printTrace(r.name, pids.size(), r, limit);
             if (r.done) {
                 doneTraces++;
             }
@@ -507,10 +507,14 @@ public class CamelTraceAction extends ActionBaseCommand {
         return row.compareTo(limit) >= 0;
     }
 
-    protected void printTrace(String name, Row row, Date limit) {
-        if (!prefix) {
-            name = null;
+    protected void printTrace(String name, int pids, Row row, Date limit) {
+        if (!prefixShown) {
+            // compute whether to show prefix or not
+            if ("false".equals(prefix) || "auto".equals(prefix) && pids <= 1) {
+                name = null;
+            }
         }
+        prefixShown = name != null;
 
         if (row.first) {
             row.parent.depth++;