diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index c3fd5c3c01..2a82f6b630 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -6,6 +6,7 @@ This file documents all notable changes to https://github.com/devonfw/IDEasy[IDE Release with new features and bugfixes: +* https://github.com/devonfw/IDEasy/issues/821[#821]: Made the `ide` prefix in `ide shell` part of the editable input so it can be removed for non-IDEasy commands * https://github.com/devonfw/IDEasy/issues/2286[#2286]: Fix SystemPath.findBinary to search extraPathEntries * https://github.com/devonfw/IDEasy/issues/1165[#1165]: Fix automatic project import for Eclipse * https://github.com/devonfw/IDEasy/issues/2040[#2040]: Fixed buggy workspace selection in the GUI diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellCommandlet.java index 7172588ba1..01e9305419 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellCommandlet.java @@ -42,6 +42,8 @@ public final class ShellCommandlet extends Commandlet { private static final String EXIT_COMMAND = "exit"; + private static final String IDE_PREFIX = "ide "; + /** * The constructor. * @@ -90,9 +92,9 @@ protected void doRun() { while (true) { try { String cwdPath = String.valueOf(context.getCwd()); - String prompt = cwdPath + (cwdPath.length() <= 80 ? "" : System.lineSeparator()) + "$ ide "; - line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null); - line = line.trim(); + String prompt = cwdPath + (cwdPath.length() <= 80 ? "" : System.lineSeparator()) + "$ "; + line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, IDE_PREFIX); + line = normalizeLine(line); if (EXIT_COMMAND.equals(line)) { return; } @@ -122,6 +124,25 @@ protected void doRun() { } } + /** + * Strips the pre-filled {@link #IDE_PREFIX} from the given raw input line, so that a command entered after leaving the prefix untouched (e.g. + * {@code ide status}) behaves the same as if only {@code status} was entered. This allows the user to remove the prefix via backspace to enter a + * non-IDEasy command (e.g. {@code cd}) without the misleading {@code ide} prefix. + * + * @param rawLine the raw line as read from the {@link LineReader}. + * @return the normalized line ready to be passed to {@link #runCommand(String)}. + */ + static String normalizeLine(String rawLine) { + + String line = rawLine.trim(); + if (line.equals(IDE_PREFIX.trim())) { + return ""; + } else if (line.startsWith(IDE_PREFIX)) { + return line.substring(IDE_PREFIX.length()).trim(); + } + return line; + } + /** * Converts String of arguments to array and runs the command * diff --git a/cli/src/test/java/com/devonfw/tools/ide/commandlet/ShellCommandletTest.java b/cli/src/test/java/com/devonfw/tools/ide/commandlet/ShellCommandletTest.java new file mode 100644 index 0000000000..23eb896c8b --- /dev/null +++ b/cli/src/test/java/com/devonfw/tools/ide/commandlet/ShellCommandletTest.java @@ -0,0 +1,47 @@ +package com.devonfw.tools.ide.commandlet; + +import org.junit.jupiter.api.Test; + +import com.devonfw.tools.ide.context.AbstractIdeContextTest; + +/** + * Test of {@link ShellCommandlet}. + */ +class ShellCommandletTest extends AbstractIdeContextTest { + + /** + * Test of {@link ShellCommandlet#normalizeLine(String)} when the pre-filled {@code ide } prefix is left untouched, so the command behaves the same as + * before this prefix was made part of the editable input line. + */ + @Test + void testNormalizeLineKeepsPrefixedCommand() { + + // act & assert + assertThat(ShellCommandlet.normalizeLine("ide status")).isEqualTo("status"); + assertThat(ShellCommandlet.normalizeLine("ide install java")).isEqualTo("install java"); + } + + /** + * Test of {@link ShellCommandlet#normalizeLine(String)} when the user removed the pre-filled {@code ide } prefix via backspace to enter a non-IDEasy + * command, e.g. {@code cd}, see #821 for reference. + */ + @Test + void testNormalizeLineWithRemovedPrefix() { + + // act & assert + assertThat(ShellCommandlet.normalizeLine("cd ..")).isEqualTo("cd .."); + assertThat(ShellCommandlet.normalizeLine("exit")).isEqualTo("exit"); + } + + /** + * Test of {@link ShellCommandlet#normalizeLine(String)} when the user leaves an empty or unmodified prompt. + */ + @Test + void testNormalizeLineWithEmptyInput() { + + // act & assert + assertThat(ShellCommandlet.normalizeLine("ide")).isEmpty(); + assertThat(ShellCommandlet.normalizeLine("ide ")).isEmpty(); + assertThat(ShellCommandlet.normalizeLine("")).isEmpty(); + } +}