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
commit 21c0188c9bc9e6d2111ef5693e802b59ff945897 Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jan 4 13:29:07 2023 +0100 CAMEL-18523: camel-jbang - Add watch option --- .../jbang/core/commands/process/ListProcess.java | 5 +- .../commands/process/WatchableProcessCommand.java | 58 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java index 85a9475e3e8..3dbc775570b 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java @@ -32,7 +32,7 @@ import picocli.CommandLine; import picocli.CommandLine.Command; @Command(name = "ps", description = "List running Camel integrations") -public class ListProcess extends ProcessBaseCommand { +public class ListProcess extends WatchableProcessCommand { @CommandLine.Option(names = { "--sort" }, description = "Sort by pid, name or age", defaultValue = "pid") @@ -46,8 +46,7 @@ public class ListProcess extends ProcessBaseCommand { super(main); } - @Override - public Integer call() throws Exception { + protected Integer doCall() throws Exception { List<Row> rows = new ArrayList<>(); List<Long> pids = findPids("*"); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/WatchableProcessCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/WatchableProcessCommand.java new file mode 100644 index 00000000000..f01131efea4 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/WatchableProcessCommand.java @@ -0,0 +1,58 @@ +/* + * 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.dsl.jbang.core.commands.process; + +import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain; +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.AnsiConsole; +import picocli.CommandLine; + +/** + * Base class for commands that can run in watch mode. + */ +public abstract class WatchableProcessCommand extends ProcessBaseCommand { + + @CommandLine.Option(names = { "--watch" }, + description = "Execute periodically and showing output fullscreen") + boolean watch; + + public WatchableProcessCommand(CamelJBangMain main) { + super(main); + } + + @Override + public Integer call() throws Exception { + int exit; + if (watch) { + do { + // clear screen first + AnsiConsole.out().print(Ansi.ansi().eraseScreen()); + AnsiConsole.out().print(Ansi.ansi().cursor(0, 0)); + // output command + exit = doCall(); + // use 2-sec delay in watch mode + Thread.sleep(2000); + } while (exit == 0); + } else { + exit = doCall(); + } + return exit; + } + + protected abstract Integer doCall() throws Exception; + +}
