Search in sources :

Example 11 with Terminal

use of org.jline.terminal.Terminal in project flink by apache.

the class CliClientTest method verifySqlCompletion.

private void verifySqlCompletion(String statement, int position, List<String> expectedHints) throws IOException {
    final MockExecutor mockExecutor = new MockExecutor();
    String sessionId = mockExecutor.openSession("test-session");
    final SqlCompleter completer = new SqlCompleter(sessionId, mockExecutor);
    final SqlMultiLineParser parser = new SqlMultiLineParser(new SqlCommandParserImpl(mockExecutor, sessionId));
    try (Terminal terminal = TerminalUtils.createDumbTerminal()) {
        final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();
        final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
        final List<Candidate> candidates = new ArrayList<>();
        final List<String> results = new ArrayList<>();
        completer.complete(reader, parsedLine, candidates);
        candidates.forEach(item -> results.add(item.value()));
        assertThat(results.containsAll(expectedHints)).isTrue();
        assertThat(statement).isEqualTo(mockExecutor.receivedStatement);
        assertThat(position).isEqualTo(mockExecutor.receivedPosition);
    }
}
Also used : Candidate(org.jline.reader.Candidate) LineReader(org.jline.reader.LineReader) ParsedLine(org.jline.reader.ParsedLine) ArrayList(java.util.ArrayList) DumbTerminal(org.jline.terminal.impl.DumbTerminal) Terminal(org.jline.terminal.Terminal)

Example 12 with Terminal

use of org.jline.terminal.Terminal in project flink by apache.

the class CliView method ensureTerminalFullScreen.

private void ensureTerminalFullScreen() {
    final Terminal terminal = client.getTerminal();
    terminal.puts(Capability.enter_ca_mode);
    terminal.puts(Capability.keypad_xmit);
    terminal.puts(Capability.cursor_invisible);
}
Also used : Terminal(org.jline.terminal.Terminal)

Example 13 with Terminal

use of org.jline.terminal.Terminal in project flink by apache.

the class CliClient method executeFile.

/**
 * Execute content from Sql file and prints status information and/or errors on the terminal.
 *
 * @param content SQL file content
 */
private boolean executeFile(String content, OutputStream outputStream, ExecutionMode mode) {
    terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EXECUTE_FILE).toAnsi());
    // append line delimiter
    InputStream inputStream = new ByteArrayInputStream(SqlMultiLineParser.formatSqlFile(content).getBytes());
    Terminal dumbTerminal = TerminalUtils.createDumbTerminal(inputStream, outputStream);
    try {
        LineReader lineReader = createLineReader(dumbTerminal);
        return getAndExecuteStatements(lineReader, mode);
    } catch (Throwable e) {
        printExecutionException(e);
        return false;
    } finally {
        try {
            dumbTerminal.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) LineReader(org.jline.reader.LineReader) IOException(java.io.IOException) Terminal(org.jline.terminal.Terminal)

Example 14 with Terminal

use of org.jline.terminal.Terminal in project alluxio by Alluxio.

the class HelpCommand method printCommandInfo.

/**
 * Prints the info about a command to the given print writer.
 *
 * @param command command to print info
 * @param pw where to print the info
 */
public static void printCommandInfo(Command command, PrintWriter pw) {
    String description = String.format("%s: %s", command.getCommandName(), command.getDescription());
    int width = 80;
    try (Terminal terminal = TerminalBuilder.terminal()) {
        width = terminal.getWidth();
    } catch (Exception e) {
    // In case the terminal builder failed to decide terminal type, use default width
    }
    // Use default value if terminal width is assigned 0
    if (width == 0) {
        width = 80;
    }
    HELP_FORMATTER.printWrapped(pw, width, description);
    HELP_FORMATTER.printUsage(pw, width, command.getUsage());
    if (command.getOptions().getOptions().size() > 0) {
        HELP_FORMATTER.printOptions(pw, width, command.getOptions(), HELP_FORMATTER.getLeftPadding(), HELP_FORMATTER.getDescPadding());
    }
}
Also used : Terminal(org.jline.terminal.Terminal) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) InvalidArgumentException(alluxio.exception.status.InvalidArgumentException)

Example 15 with Terminal

use of org.jline.terminal.Terminal in project Payara by payara.

the class MultimodeCommand method executeCommand.

@Override
protected int executeCommand() throws CommandException, CommandValidationException {
    LineReader reader = null;
    // restore echo flag, saved in validate
    programOpts.setEcho(echo);
    try {
        if (file == null) {
            System.out.println(strings.get("multimodeIntro"));
            Completer completer = getAllCommandsCompleter();
            Terminal asadminTerminal = TerminalBuilder.builder().name(ASADMIN).system(true).encoding(encoding != null ? Charset.forName(encoding) : Charset.defaultCharset()).build();
            reader = newLineReaderBuilder().terminal(asadminTerminal).completer(completer).build();
            reader.unsetOpt(LineReader.Option.INSERT_TAB);
        } else {
            printPrompt = false;
            if (!file.canRead()) {
                throw new CommandException("File: " + file + " can not be read");
            }
            OutputStream out = new OutputStream() {

                @Override
                public void write(int b) throws IOException {
                    return;
                }

                @Override
                public void write(byte[] b) throws IOException {
                    return;
                }

                @Override
                public void write(byte[] b, int off, int len) throws IOException {
                    return;
                }
            };
            Terminal asadminTerminal = new ExternalTerminal(ASADMIN, "", new FileInputStream(file), out, encoding != null ? Charset.forName(encoding) : Charset.defaultCharset());
            reader = newLineReaderBuilder().terminal(asadminTerminal).build();
        }
        return executeCommands(reader);
    } catch (IOException e) {
        throw new CommandException(e);
    } finally {
        try {
            if (file != null && reader != null && reader.getTerminal() != null) {
                reader.getTerminal().close();
            }
        } catch (Exception e) {
        // ignore it
        }
    }
}
Also used : LineReader(org.jline.reader.LineReader) OutputStream(java.io.OutputStream) ExternalTerminal(org.jline.terminal.impl.ExternalTerminal) Completer(org.jline.reader.Completer) StringsCompleter(org.jline.reader.impl.completer.StringsCompleter) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) ExternalTerminal(org.jline.terminal.impl.ExternalTerminal) Terminal(org.jline.terminal.Terminal) FileInputStream(java.io.FileInputStream) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException)

Aggregations

Terminal (org.jline.terminal.Terminal)39 LineReader (org.jline.reader.LineReader)14 IOException (java.io.IOException)13 InputStream (java.io.InputStream)8 EndOfFileException (org.jline.reader.EndOfFileException)8 UserInterruptException (org.jline.reader.UserInterruptException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Attributes (org.jline.terminal.Attributes)7 PrintStream (java.io.PrintStream)6 Path (java.nio.file.Path)6 CommandSession (org.apache.felix.service.command.CommandSession)6 OutputStream (java.io.OutputStream)5 ArrayList (java.util.ArrayList)5 InputStreamReader (java.io.InputStreamReader)4 Reader (java.io.Reader)4 ParsedLine (org.jline.reader.ParsedLine)4 BufferedReader (java.io.BufferedReader)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3