This is an automated email from the ASF dual-hosted git repository. jpoth pushed a commit to branch CAMEL-21723 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 42cf7101e8b1459c900c04fd36dcb3cdf846818a Author: John Poth <poth.j...@gmail.com> AuthorDate: Wed Feb 12 14:19:03 2025 +0100 CAMEL-21723: camel-jbang add edit command --- .../dsl/jbang/core/commands/CamelJBangMain.java | 1 + .../apache/camel/dsl/jbang/core/commands/Edit.java | 58 ++++++++++++++++++++++ .../camel/dsl/jbang/core/commands/Shell.java | 3 ++ 3 files changed, 62 insertions(+) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java index d168a0b66f7..d192bd894fa 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java @@ -82,6 +82,7 @@ public class CamelJBangMain implements Callable<Integer> { } commandLine = new CommandLine(main) + .addSubcommand("edit", new CommandLine(new Edit(main))) .addSubcommand("shell", new CommandLine(new Shell(main))) .addSubcommand("init", new CommandLine(new Init(main))) .addSubcommand("run", new CommandLine(new Run(main))) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Edit.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Edit.java new file mode 100644 index 00000000000..0eff6d70a59 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Edit.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; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Stack; +import java.util.function.Supplier; + +import org.jline.builtins.Commands; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; +import picocli.CommandLine; + +@CommandLine.Command(name = "edit", + description = "Edit file", + footer = "Press Ctrl-X to exit.") +public class Edit extends CamelCommand { + + @CommandLine.Parameters(description = "Name of file", arity = "1", + paramLabel = "<file>", parameterConsumer = FileConsumer.class) + private Path filePath; // Defined only for file path completion; the field never used + private String file; + + public Edit(CamelJBangMain main) { + super(main); + } + + @Override + public Integer doCall() throws Exception { + Supplier<Path> workDir = () -> Paths.get(System.getProperty("user.dir")); + try (Terminal terminal = TerminalBuilder.builder().build()) { + Commands.nano(terminal, System.out, System.err, workDir.get(), new String[] { file }, null); + } + return 0; + } + + static class FileConsumer extends ParameterConsumer<Edit> { + @Override + protected void doConsumeParameters(Stack<String> args, Edit cmd) { + cmd.file = args.pop(); + } + } +} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Shell.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Shell.java index f3c95a1563b..16c620e9ec4 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Shell.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Shell.java @@ -89,6 +89,7 @@ public class Shell extends CamelCommand { // start the shell and process input until the user quits with Ctrl-C or Ctrl-D String line; boolean run = true; + TerminalBuilder.setTerminalOverride(terminal); while (run) { try { systemRegistry.cleanUp(); @@ -106,6 +107,8 @@ public class Shell extends CamelCommand { systemRegistry.trace(e); } } + } finally { + TerminalBuilder.setTerminalOverride(null); } return 0; }