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

devonfw / IDEasy / 11527033206

26 Oct 2024 12:03AM UTC coverage: 66.759% (-0.05%) from 66.811%
11527033206

push

github

web-flow
#533: Add autocompletion of exit in ide shell (#707)

2402 of 3946 branches covered (60.87%)

Branch coverage included in aggregate %.

6266 of 9038 relevant lines covered (69.33%)

3.05 hits per line

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

3.7
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.util.Iterator;
5

6
import org.fusesource.jansi.AnsiConsole;
7
import org.jline.reader.Completer;
8
import org.jline.reader.EndOfFileException;
9
import org.jline.reader.LineReader;
10
import org.jline.reader.LineReaderBuilder;
11
import org.jline.reader.MaskingCallback;
12
import org.jline.reader.Parser;
13
import org.jline.reader.UserInterruptException;
14
import org.jline.reader.impl.DefaultParser;
15
import org.jline.reader.impl.completer.AggregateCompleter;
16
import org.jline.reader.impl.completer.StringsCompleter;
17
import org.jline.terminal.Terminal;
18
import org.jline.terminal.TerminalBuilder;
19
import org.jline.widget.AutosuggestionWidgets;
20

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

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

35
  private static final int AUTOCOMPLETER_MAX_RESULTS = 50;
36

37
  private static final int RC_EXIT = 987654321;
38

39
  /**
40
   * The constructor.
41
   *
42
   * @param context the {@link IdeContext}.
43
   */
44
  public ShellCommandlet(IdeContext context) {
45

46
    super(context);
3✔
47
    addKeyword(getName());
4✔
48
  }
1✔
49

50
  @Override
51
  public String getName() {
52

53
    return "shell";
2✔
54
  }
55

56
  @Override
57
  public boolean isIdeHomeRequired() {
58

59
    return false;
2✔
60
  }
61

62
  @Override
63
  public void run() {
64

65
    try {
66
      // TODO: add BuiltIns here, see: https://github.com/devonfw/IDEasy/issues/168
67

68
      Parser parser = new DefaultParser();
×
69
      try (Terminal terminal = TerminalBuilder.builder().build()) {
×
70

71
        // initialize our own completer here and add exit as an autocompletion option
72
        Completer completer = new AggregateCompleter(
×
73
            new StringsCompleter("exit"), new IdeCompleter((AbstractIdeContext) this.context));
74

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

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

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

85
        String prompt = "ide> ";
×
86
        String rightPrompt = null;
×
87
        String line;
88

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

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

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

129
    if ("exit".equals(args) || "quit".equals(args)) {
×
130
      return RC_EXIT;
×
131
    }
132
    String[] arguments = args.split(" ", 0);
×
133
    CliArguments cliArgs = new CliArguments(arguments);
×
134
    cliArgs.next();
×
135
    return ((AbstractIdeContext) this.context).run(cliArgs);
×
136
  }
137

138
  /**
139
   * @param argument the current {@link CliArgument} (position) to match.
140
   * @param commandlet the potential {@link Commandlet} to match.
141
   * @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}
142
   *     and {@link Commandlet#validate() validated}), {@code false} otherwise (the {@link Commandlet} did not match and we have to try a different candidate).
143
   */
144
  private boolean apply(CliArgument argument, Commandlet commandlet) {
145

146
    this.context.trace("Trying to match arguments to commandlet {}", commandlet.getName());
×
147
    CliArgument currentArgument = argument;
×
148
    Iterator<Property<?>> valueIterator = commandlet.getValues().iterator();
×
149
    Property<?> currentProperty = null;
×
150
    boolean endOpts = false;
×
151
    while (!currentArgument.isEnd()) {
×
152
      if (currentArgument.isEndOptions()) {
×
153
        endOpts = true;
×
154
      } else {
155
        String arg = currentArgument.get();
×
156
        this.context.trace("Trying to match argument '{}'", currentArgument);
×
157
        if ((currentProperty != null) && (currentProperty.isExpectValue())) {
×
158
          currentProperty.setValueAsString(arg, this.context);
×
159
          if (!currentProperty.isMultiValued()) {
×
160
            currentProperty = null;
×
161
          }
162
        } else {
163
          Property<?> property = null;
×
164
          if (!endOpts) {
×
165
            property = commandlet.getOption(currentArgument.getKey());
×
166
          }
167
          if (property == null) {
×
168
            if (!valueIterator.hasNext()) {
×
169
              this.context.trace("No option or next value found");
×
170
              return false;
×
171
            }
172
            currentProperty = valueIterator.next();
×
173
            this.context.trace("Next value candidate is {}", currentProperty);
×
174
            if (currentProperty instanceof KeywordProperty keyword) {
×
175
              if (keyword.matches(arg)) {
×
176
                keyword.setValue(Boolean.TRUE);
×
177
                this.context.trace("Keyword matched");
×
178
              } else {
179
                this.context.trace("Missing keyword");
×
180
                return false;
×
181
              }
182
            } else {
183
              boolean success = currentProperty.assignValueAsString(arg, this.context, commandlet);
×
184
              if (!success && currentProperty.isRequired()) {
×
185
                return false;
×
186
              }
187
            }
188
            if ((currentProperty != null) && !currentProperty.isMultiValued()) {
×
189
              currentProperty = null;
×
190
            }
191
          } else {
192
            this.context.trace("Found option by name");
×
193
            String value = currentArgument.getValue();
×
194
            if (value != null) {
×
195
              property.setValueAsString(value, this.context);
×
196
            } else if (property instanceof BooleanProperty) {
×
197
              ((BooleanProperty) property).setValue(Boolean.TRUE);
×
198
            } else {
199
              currentProperty = property;
×
200
              if (property.isEndOptions()) {
×
201
                endOpts = true;
×
202
              }
203
              throw new UnsupportedOperationException("not implemented");
×
204
            }
205
          }
206
        }
207
      }
208
      currentArgument = currentArgument.getNext(!endOpts);
×
209
    }
210
    return commandlet.validate().isValid();
×
211
  }
212
}
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

© 2025 Coveralls, Inc