Search in sources :

Example 6 with CommandLine

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));
    }
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Parser(org.apache.karaf.shell.api.console.Parser) CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) ParsedLineImpl(org.apache.felix.gogo.jline.ParsedLineImpl) ParsedLine(org.jline.reader.ParsedLine)

Example 7 with CommandLine

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();
        }
    };
}
Also used : ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) CommandLine(org.apache.karaf.shell.api.console.CommandLine) ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) Command(org.apache.karaf.shell.api.console.Command) ArrayList(java.util.ArrayList) Completer(org.apache.karaf.shell.api.console.Completer) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.apache.karaf.shell.api.console.Session)

Example 8 with CommandLine

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()));
    }
}
Also used : Candidate(org.apache.karaf.shell.api.console.Candidate) ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) CommandLine(org.apache.karaf.shell.api.console.CommandLine) ArrayList(java.util.ArrayList)

Example 9 with CommandLine

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)));
}
Also used : CommandLine(org.apache.karaf.shell.api.console.CommandLine) ApplicationStatus(org.codice.ddf.admin.application.service.ApplicationStatus) ApplicationState(org.codice.ddf.admin.application.service.ApplicationStatus.ApplicationState) ArrayList(java.util.ArrayList) Application(org.codice.ddf.admin.application.service.Application) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CommandLine (org.apache.karaf.shell.api.console.CommandLine)9 Command (org.apache.karaf.shell.api.console.Command)5 ArrayList (java.util.ArrayList)4 Parser (org.apache.karaf.shell.api.console.Parser)4 DefaultParser (org.apache.karaf.shell.support.parsing.DefaultParser)3 HashSet (java.util.HashSet)2 List (java.util.List)2 ParsedLineImpl (org.apache.felix.gogo.jline.ParsedLineImpl)2 Program (org.apache.felix.gogo.runtime.Parser.Program)2 Statement (org.apache.felix.gogo.runtime.Parser.Statement)2 Completer (org.apache.karaf.shell.api.console.Completer)2 Session (org.apache.karaf.shell.api.console.Session)2 ArgumentCommandLine (org.apache.karaf.shell.support.completers.ArgumentCommandLine)2 StringsCompleter (org.apache.karaf.shell.support.completers.StringsCompleter)2 GogoParser (org.apache.karaf.shell.support.parsing.GogoParser)2 ParsedLine (org.jline.reader.ParsedLine)2 Test (org.junit.Test)2 Arrays (java.util.Arrays)1 ListIterator (java.util.ListIterator)1 Set (java.util.Set)1