Search in sources :

Example 1 with Shell

use of org.aesh.command.shell.Shell in project infinispan by infinispan.

the class Help method execute.

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    Shell shell = commandInvocation.getShell();
    if (manPages == null || manPages.size() == 0) {
        shell.writeln("Call `help <command>` where command is one of:");
        List<TerminalString> commandNames = ((ContextAwareCommandInvocation) commandInvocation).getContext().getRegistry().getAllCommandNames().stream().map(n -> new TerminalString(n)).collect(Collectors.toList());
        // then we print out the completions
        shell.write(Parser.formatDisplayListTerminalString(commandNames, shell.size().getHeight(), shell.size().getWidth()));
        // then on the next line we write the line again
        shell.writeln("");
        return CommandResult.SUCCESS;
    }
    if (manPages.size() <= 0) {
        shell.write("No manual entry for " + manPages.get(0) + Config.getLineSeparator());
        return CommandResult.SUCCESS;
    }
    if (manProvider == null) {
        shell.write("No manual provider defined");
        return CommandResult.SUCCESS;
    }
    InputStream inputStream = manProvider.getManualDocument(manPages.get(0));
    if (inputStream != null) {
        setCommandInvocation(commandInvocation);
        try {
            fileParser.setInput(inputStream);
            afterAttach();
        } catch (IOException ex) {
            throw new CommandException(ex);
        }
    }
    return CommandResult.SUCCESS;
}
Also used : CommandDefinition(org.aesh.command.CommandDefinition) Parser(org.aesh.readline.util.Parser) ManProvider(org.aesh.command.settings.ManProvider) MetaInfServices(org.kohsuke.MetaInfServices) TerminalPage(org.aesh.command.man.TerminalPage) Command(org.aesh.command.Command) FileParser(org.aesh.command.man.FileParser) ArrayList(java.util.ArrayList) ManFileParser(org.aesh.command.man.parser.ManFileParser) CommandResult(org.aesh.command.CommandResult) ANSI(org.aesh.terminal.utils.ANSI) Shell(org.aesh.command.shell.Shell) Config(org.aesh.terminal.utils.Config) ContextAwareCommandInvocation(org.infinispan.cli.impl.ContextAwareCommandInvocation) CliManProvider(org.infinispan.cli.impl.CliManProvider) IOException(java.io.IOException) CommandInvocation(org.aesh.command.invocation.CommandInvocation) Collectors(java.util.stream.Collectors) CommandException(org.aesh.command.CommandException) Arguments(org.aesh.command.option.Arguments) List(java.util.List) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) AeshFileDisplayer(org.aesh.command.man.AeshFileDisplayer) HelpCompleter(org.infinispan.cli.completers.HelpCompleter) InputStream(java.io.InputStream) Shell(org.aesh.command.shell.Shell) InputStream(java.io.InputStream) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) IOException(java.io.IOException) CommandException(org.aesh.command.CommandException)

Example 2 with Shell

use of org.aesh.command.shell.Shell in project infinispan by infinispan.

the class RestCliCommand method exec.

@Override
protected final CommandResult exec(ContextAwareCommandInvocation invocation) throws CommandException {
    Shell shell = invocation.getShell();
    Context context = invocation.getContext();
    try {
        String response = context.getConnection().execute((c, r) -> {
            try {
                return exec(invocation, c, r);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, getResponseMode());
        if (response != null && !response.isEmpty()) {
            shell.writeln(response);
        }
        invocation.getContext().refreshPrompt();
        return CommandResult.SUCCESS;
    } catch (Exception e) {
        TerminalString error = new TerminalString(Util.getRootCause(e).getLocalizedMessage(), new TerminalColor(Color.RED, Color.DEFAULT, Color.Intensity.BRIGHT));
        shell.writeln(error.toString());
        invocation.getContext().refreshPrompt();
        return CommandResult.FAILURE;
    }
}
Also used : Context(org.infinispan.cli.Context) Shell(org.aesh.command.shell.Shell) TerminalColor(org.aesh.readline.terminal.formatting.TerminalColor) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) CommandException(org.aesh.command.CommandException)

Aggregations

CommandException (org.aesh.command.CommandException)2 Shell (org.aesh.command.shell.Shell)2 TerminalString (org.aesh.readline.terminal.formatting.TerminalString)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Command (org.aesh.command.Command)1 CommandDefinition (org.aesh.command.CommandDefinition)1 CommandResult (org.aesh.command.CommandResult)1 CommandInvocation (org.aesh.command.invocation.CommandInvocation)1 AeshFileDisplayer (org.aesh.command.man.AeshFileDisplayer)1 FileParser (org.aesh.command.man.FileParser)1 TerminalPage (org.aesh.command.man.TerminalPage)1 ManFileParser (org.aesh.command.man.parser.ManFileParser)1 Arguments (org.aesh.command.option.Arguments)1 ManProvider (org.aesh.command.settings.ManProvider)1 TerminalColor (org.aesh.readline.terminal.formatting.TerminalColor)1 Parser (org.aesh.readline.util.Parser)1