• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 26212118157

21 May 2026 07:31AM UTC coverage: 70.979%. Remained the same
26212118157

push

github

web-flow
#836: fix exit autocompletion (#1950)

4453 of 6934 branches covered (64.22%)

Branch coverage included in aggregate %.

11481 of 15515 relevant lines covered (74.0%)

3.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.75
cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.io.IOException;
4
import java.nio.file.Path;
5
import java.nio.file.Paths;
6
import java.util.Iterator;
7

8
import org.fusesource.jansi.AnsiConsole;
9
import org.jline.reader.EndOfFileException;
10
import org.jline.reader.LineReader;
11
import org.jline.reader.LineReaderBuilder;
12
import org.jline.reader.MaskingCallback;
13
import org.jline.reader.Parser;
14
import org.jline.reader.UserInterruptException;
15
import org.jline.reader.impl.DefaultParser;
16
import org.jline.terminal.Terminal;
17
import org.jline.terminal.TerminalBuilder;
18
import org.jline.widget.AutosuggestionWidgets;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

22
import com.devonfw.tools.ide.cli.CliArgument;
23
import com.devonfw.tools.ide.cli.CliArguments;
24
import com.devonfw.tools.ide.cli.CliException;
25
import com.devonfw.tools.ide.completion.IdeCompleter;
26
import com.devonfw.tools.ide.context.AbstractIdeContext;
27
import com.devonfw.tools.ide.context.IdeContext;
28
import com.devonfw.tools.ide.property.BooleanProperty;
29
import com.devonfw.tools.ide.property.KeywordProperty;
30
import com.devonfw.tools.ide.property.Property;
31

32
/**
33
 * {@link Commandlet} for internal interactive shell with build-in auto-completion and help.
34
 */
35
public final class ShellCommandlet extends Commandlet {
36

37
  private static final Logger LOG = LoggerFactory.getLogger(ShellCommandlet.class);
4✔
38

39
  private static final int AUTOCOMPLETER_MAX_RESULTS = 50;
40

41
  private static final int RC_EXIT = 987654321;
42

43
  private static final String EXIT_COMMAND = "exit";
44

45
  /**
46
   * The constructor.
47
   *
48
   * @param context the {@link IdeContext}.
49
   */
50
  public ShellCommandlet(IdeContext context) {
51

52
    super(context);
3✔
53
    addKeyword(getName());
4✔
54
  }
1✔
55

56
  @Override
57
  public String getName() {
58

59
    return "shell";
2✔
60
  }
61

62
  @Override
63
  public boolean isIdeHomeRequired() {
64

65
    return false;
2✔
66
  }
67

68
  @Override
69
  protected void doRun() {
70

71
    try {
72
      Parser parser = new DefaultParser();
×
73
      try (Terminal terminal = TerminalBuilder.builder().build()) {
×
74
        IdeCompleter completer = new IdeCompleter((AbstractIdeContext) this.context);
×
75

76
        LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(completer).parser(parser)
×
77
            .variable(LineReader.LIST_MAX, AUTOCOMPLETER_MAX_RESULTS).build();
×
78

79
        // Create autosuggestion widgets
80
        AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
×
81
        // Enable autosuggestions
82
        autosuggestionWidgets.enable();
×
83

84
        // TODO: implement TailTipWidgets, see: https://github.com/devonfw/IDEasy/issues/169
85

86
        String rightPrompt = null;
×
87
        String line;
88

89
        AnsiConsole.systemInstall();
×
90
        while (true) {
91
          try {
92
            String prompt = context.getCwd() + "$ ide ";
×
93
            line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
×
94
            line = line.trim();
×
95
            if (EXIT_COMMAND.equals(line)) {
×
96
              return;
×
97
            }
98
            reader.getHistory().add(line);
×
99
            int rc = runCommand(line);
×
100
            if (rc == RC_EXIT) {
×
101
              return;
×
102
            }
103
          } catch (UserInterruptException e) {
×
104
            // Ignore CTRL+C
105
            return;
×
106
          } catch (EndOfFileException e) {
×
107
            // CTRL+D
108
            return;
×
109
          } finally {
110
            AnsiConsole.systemUninstall();
×
111
          }
×
112
        }
113

114
      } catch (IOException e) {
×
115
        throw new RuntimeException(e);
×
116
      }
117
    } catch (CliException e) {
×
118
      throw e;
×
119
    } catch (Exception e) {
×
120
      throw new RuntimeException("Unexpected error during interactive auto-completion", e);
×
121
    }
122
  }
123

124
  /**
125
   * Converts String of arguments to array and runs the command
126
   *
127
   * @param args String of arguments
128
   * @return status code
129
   */
130
  private int runCommand(String args) {
131

132
    if (EXIT_COMMAND.equals(args) || "quit".equals(args)) {
×
133
      return RC_EXIT;
×
134
    }
135
    String[] arguments = args.split(" ", 0);
×
136
    CliArguments cliArgs = new CliArguments(arguments);
×
137
    cliArgs.next();
×
138

139
    if ("cd".equals(arguments[0])) {
×
140
      return changeDirectory(cliArgs);
×
141
    }
142

143
    return ((AbstractIdeContext) this.context).run(cliArgs);
×
144
  }
145

146
  private int changeDirectory(CliArguments cliArgs) {
147
    if (!cliArgs.hasNext()) {
×
148
      Path homeDir = this.context.getUserHome();
×
149
      context.setCwd(homeDir, context.getWorkspaceName(), context.getIdeHome());
×
150
      return 0;
×
151
    }
152

153
    String targetDir = String.valueOf(cliArgs.next());
×
154
    Path path = Paths.get(targetDir);
×
155

156
    // If the given path is relative, resolve it relative to the current directory
157
    if (!path.isAbsolute()) {
×
158
      path = context.getCwd().resolve(targetDir).normalize();
×
159
    }
160

161
    if (context.getFileAccess().isExpectedFolder(path)) {
×
162
      context.setCwd(path, context.getWorkspaceName(), context.getIdeHome());
×
163
      return 0;
×
164
    } else {
165
      return 1;
×
166
    }
167
  }
168

169
  /**
170
   * @param argument the current {@link CliArgument} (position) to match.
171
   * @param commandlet the potential {@link Commandlet} to match.
172
   * @return {@code true} if the given {@link Commandlet} matches to the given {@link CliArgument}(s) and those have been applied (set in the {@link Commandlet}
173
   *     and {@link Commandlet#validate() validated}), {@code false} otherwise (the {@link Commandlet} did not match and we have to try a different candidate).
174
   */
175
  private boolean apply(CliArgument argument, Commandlet commandlet) {
176

177
    LOG.trace("Trying to match arguments to commandlet {}", commandlet.getName());
×
178
    CliArgument currentArgument = argument;
×
179
    Iterator<Property<?>> valueIterator = commandlet.getValues().iterator();
×
180
    Property<?> currentProperty = null;
×
181
    boolean endOpts = false;
×
182
    while (!currentArgument.isEnd()) {
×
183
      if (currentArgument.isEndOptions()) {
×
184
        endOpts = true;
×
185
      } else {
186
        String arg = currentArgument.get();
×
187
        LOG.trace("Trying to match argument '{}'", currentArgument);
×
188
        if ((currentProperty != null) && (currentProperty.isExpectValue())) {
×
189
          currentProperty.setValueAsString(arg, this.context);
×
190
          if (!currentProperty.isMultiValued()) {
×
191
            currentProperty = null;
×
192
          }
193
        } else {
194
          Property<?> property = null;
×
195
          if (!endOpts) {
×
196
            property = commandlet.getOption(currentArgument.getKey());
×
197
          }
198
          if (property == null) {
×
199
            if (!valueIterator.hasNext()) {
×
200
              LOG.trace("No option or next value found");
×
201
              return false;
×
202
            }
203
            currentProperty = valueIterator.next();
×
204
            LOG.trace("Next value candidate is {}", currentProperty);
×
205
            if (currentProperty instanceof KeywordProperty keyword) {
×
206
              if (keyword.matches(arg)) {
×
207
                keyword.setValue(Boolean.TRUE);
×
208
                LOG.trace("Keyword matched");
×
209
              } else {
210
                LOG.trace("Missing keyword");
×
211
                return false;
×
212
              }
213
            } else {
214
              boolean success = currentProperty.assignValueAsString(arg, this.context, commandlet);
×
215
              if (!success && currentProperty.isRequired()) {
×
216
                return false;
×
217
              }
218
            }
219
            if ((currentProperty != null) && !currentProperty.isMultiValued()) {
×
220
              currentProperty = null;
×
221
            }
222
          } else {
223
            LOG.trace("Found option by name");
×
224
            String value = currentArgument.getValue();
×
225
            if (value != null) {
×
226
              property.setValueAsString(value, this.context);
×
227
            } else if (property instanceof BooleanProperty) {
×
228
              ((BooleanProperty) property).setValue(Boolean.TRUE);
×
229
            } else {
230
              currentProperty = property;
×
231
              if (property.isEndOptions()) {
×
232
                endOpts = true;
×
233
              }
234
              throw new UnsupportedOperationException("not implemented");
×
235
            }
236
          }
237
        }
238
      }
239
      currentArgument = currentArgument.getNext(!endOpts);
×
240
    }
241
    return commandlet.validate().isValid();
×
242
  }
243
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc