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

devonfw / IDEasy / 11783304870

11 Nov 2024 05:26PM UTC coverage: 67.136% (+0.2%) from 66.902%
11783304870

push

github

web-flow
#632: added .editorconfig  to settings-workspaces (#731)

2430 of 3960 branches covered (61.36%)

Branch coverage included in aggregate %.

6336 of 9097 relevant lines covered (69.65%)

3.07 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
      Parser parser = new DefaultParser();
×
67
      try (Terminal terminal = TerminalBuilder.builder().build()) {
×
68
        // initialize our own completer here and add exit as an autocompletion option
69
        Completer completer = new AggregateCompleter(
×
70
            new StringsCompleter("exit"), new IdeCompleter((AbstractIdeContext) this.context));
71

72
        LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(completer).parser(parser)
×
73
            .variable(LineReader.LIST_MAX, AUTOCOMPLETER_MAX_RESULTS).build();
×
74

75
        // Create autosuggestion widgets
76
        AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
×
77
        // Enable autosuggestions
78
        autosuggestionWidgets.enable();
×
79

80
        // TODO: implement TailTipWidgets, see: https://github.com/devonfw/IDEasy/issues/169
81

82
        String prompt = "ide> ";
×
83
        String rightPrompt = null;
×
84
        String line;
85

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

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

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

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

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

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