use of org.apache.karaf.shell.api.console.CommandLine in project karaf by apache.
the class KarafParser method doParse.
private ParsedLine doParse(CharSequence line, int cursor) throws SyntaxError {
org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(line);
Program program = parser.program();
List<Statement> statements = parser.statements();
// Find corresponding statement
Statement statement = null;
for (int i = statements.size() - 1; i >= 0; i--) {
Statement s = statements.get(i);
if (s.start() <= cursor) {
boolean isOk = true;
// check if there are only spaces after the previous statement
if (s.start() + s.length() < cursor) {
for (int j = s.start() + s.length(); isOk && j < cursor; j++) {
isOk = Character.isWhitespace(line.charAt(j));
}
}
statement = s;
break;
}
}
if (statement != null && statement.tokens() != null && !statement.tokens().isEmpty()) {
String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
String[] parts = cmdName.split(":");
Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
Parser cmdParser = cmd != null ? cmd.getParser() : null;
if (cmdParser != null) {
final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
return new ParsedLine() {
@Override
public String word() {
return cmdLine.getCursorArgument();
}
@Override
public int wordCursor() {
return cmdLine.getArgumentPosition();
}
@Override
public int wordIndex() {
return cmdLine.getCursorArgumentIndex();
}
@Override
public List<String> words() {
return Arrays.asList(cmdLine.getArguments());
}
@Override
public String line() {
return cmdLine.getBuffer();
}
@Override
public int cursor() {
return cmdLine.getBufferPosition();
}
};
}
return new ParsedLineImpl(program, statement, cursor, statement.tokens());
} else {
// TODO:
return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
}
}
use of org.apache.karaf.shell.api.console.CommandLine in project karaf by apache.
the class HelpCommand method getCompleter.
@Override
public Completer getCompleter(final boolean scoped) {
return new Completer() {
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
String[] args = commandLine.getArguments();
int argIndex = commandLine.getCursorArgumentIndex();
StringsCompleter completer = new StringsCompleter(Collections.singletonList(getName()));
if (argIndex == 0) {
return completer.complete(session, new ArgumentCommandLine(args[argIndex], commandLine.getArgumentPosition()), candidates);
} else if (!verifyCompleter(session, completer, args[0])) {
return -1;
}
// TODO: use CommandNamesCompleter and better completion wrt parsing etc...
completer = new StringsCompleter();
for (Command command : session.getRegistry().getCommands()) {
if (!Session.SCOPE_GLOBAL.equals(command.getScope())) {
completer.getStrings().add(command.getScope() + ":" + command.getName());
}
completer.getStrings().add(command.getName());
}
completer.getStrings().add("--help");
if (argIndex == 1) {
int res;
if (argIndex < args.length) {
res = completer.complete(session, new ArgumentCommandLine(args[argIndex], commandLine.getArgumentPosition()), candidates);
} else {
res = completer.complete(session, new ArgumentCommandLine("", 0), candidates);
}
return res + (commandLine.getBufferPosition() - commandLine.getArgumentPosition());
} else if (!verifyCompleter(session, completer, args[1])) {
return -1;
}
return -1;
}
protected boolean verifyCompleter(Session session, Completer completer, String argument) {
List<String> candidates = new ArrayList<>();
return completer.complete(session, new ArgumentCommandLine(argument, argument.length()), candidates) != -1 && !candidates.isEmpty();
}
};
}
use of org.apache.karaf.shell.api.console.CommandLine in project karaf by apache.
the class CommandsCompleter method complete.
@Override
public void complete(LineReader reader, ParsedLine line, List<org.jline.reader.Candidate> candidates) {
CommandLine commandLine = new CommandLineImpl(line);
List<Candidate> cands = new ArrayList<>();
completeCandidates(session, commandLine, cands);
for (Candidate cand : cands) {
candidates.add(new org.jline.reader.Candidate(cand.value(), cand.displ(), cand.group(), cand.descr(), cand.suffix(), cand.key(), cand.complete()));
}
}
use of org.apache.karaf.shell.api.console.CommandLine in project ddf by codice.
the class ActiveApplicationsCompleterTest method testActiveApplicationsCompleter.
/**
* Tests the {@link ActiveApplicationsCompleter#complete(String, int, List)} method,
* as well as the associated constructor
*/
@Test
public void testActiveApplicationsCompleter() {
ApplicationService testAppService = mock(ApplicationServiceImpl.class);
Application testApp = mock(ApplicationImpl.class);
Set<Application> appSet = new HashSet<>();
appSet.add(testApp);
ApplicationStatus testStatus = mock(ApplicationStatusImpl.class);
ApplicationState testState = ApplicationState.ACTIVE;
when(testAppService.getApplications()).thenReturn(appSet);
when(testAppService.getApplicationStatus(testApp)).thenReturn(testStatus);
when(testStatus.getState()).thenReturn(testState);
when(testApp.getName()).thenReturn("TestApp");
ActiveApplicationsCompleter activeApplicationsCompleter = new ActiveApplicationsCompleter();
activeApplicationsCompleter.setApplicationService(testAppService);
CommandLine commandLine = mock(CommandLine.class);
assertThat("If the return value is -1, the expected match was not found.", activeApplicationsCompleter.complete(null, commandLine, new ArrayList<>()), is(not(-1)));
}
Aggregations