Search in sources :

Example 31 with Terminal

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

the class CliClientITCase method runSqlStatements.

/**
 * Returns printed results for each ran SQL statements.
 *
 * @param statements the SQL statements to run
 * @return the printed results on SQL Client
 */
private List<Result> runSqlStatements(List<String> statements) throws IOException {
    final String sqlContent = String.join("", statements);
    DefaultContext defaultContext = new DefaultContext(Collections.emptyList(), new Configuration(MINI_CLUSTER_RESOURCE.getClientConfiguration()).set(ExecutionConfigOptions.TABLE_EXEC_LEGACY_CAST_BEHAVIOUR, ExecutionConfigOptions.LegacyCastBehaviour.DISABLED), Collections.singletonList(new DefaultCLI()));
    final Executor executor = new LocalExecutor(defaultContext);
    InputStream inputStream = new ByteArrayInputStream(sqlContent.getBytes());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
    String sessionId = executor.openSession("test-session");
    try (Terminal terminal = new DumbTerminal(inputStream, outputStream);
        CliClient client = new CliClient(() -> terminal, sessionId, executor, historyPath, HideSqlStatement.INSTANCE)) {
        client.executeInInteractiveMode();
        String output = new String(outputStream.toByteArray());
        return normalizeOutput(output);
    }
}
Also used : LocalExecutor(org.apache.flink.table.client.gateway.local.LocalExecutor) Executor(org.apache.flink.table.client.gateway.Executor) Configuration(org.apache.flink.configuration.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultCLI(org.apache.flink.client.cli.DefaultCLI) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultContext(org.apache.flink.table.client.gateway.context.DefaultContext) Terminal(org.jline.terminal.Terminal) DumbTerminal(org.jline.terminal.impl.DumbTerminal) DumbTerminal(org.jline.terminal.impl.DumbTerminal) LocalExecutor(org.apache.flink.table.client.gateway.local.LocalExecutor)

Example 32 with Terminal

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

the class CliClientTest method testHistoryFile.

@Test
public void testHistoryFile() throws Exception {
    final MockExecutor mockExecutor = new MockExecutor();
    String sessionId = mockExecutor.openSession("test-session");
    InputStream inputStream = new ByteArrayInputStream("help;\nuse catalog cat;\n".getBytes());
    Path historyFilePath = historyTempFile();
    try (Terminal terminal = new DumbTerminal(inputStream, new TerminalUtils.MockOutputStream());
        CliClient client = new CliClient(() -> terminal, sessionId, mockExecutor, historyFilePath, null)) {
        client.executeInInteractiveMode();
        List<String> content = Files.readAllLines(historyFilePath);
        assertThat(content.size()).isEqualTo(2);
        assertThat(content.get(0)).contains("help");
        assertThat(content.get(1)).contains("use catalog cat");
    }
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DumbTerminal(org.jline.terminal.impl.DumbTerminal) Terminal(org.jline.terminal.Terminal) DumbTerminal(org.jline.terminal.impl.DumbTerminal) Test(org.junit.Test)

Example 33 with Terminal

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

the class CliView method unsetTerminalFullScreen.

private void unsetTerminalFullScreen() {
    final Terminal terminal = client.getTerminal();
    terminal.puts(Capability.exit_ca_mode);
    terminal.puts(Capability.keypad_local);
    terminal.puts(Capability.cursor_visible);
}
Also used : Terminal(org.jline.terminal.Terminal)

Example 34 with Terminal

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

the class CliView method restoreTerminal.

private void restoreTerminal(Tuple2<Attributes, Map<Signal, SignalHandler>> prev) {
    final Terminal terminal = client.getTerminal();
    terminal.setAttributes(prev.f0);
    prev.f1.forEach(terminal::handle);
}
Also used : Terminal(org.jline.terminal.Terminal)

Example 35 with Terminal

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

the class CliView method prepareTerminal.

private Tuple2<Attributes, Map<Signal, SignalHandler>> prepareTerminal() {
    final Terminal terminal = client.getTerminal();
    final Attributes prevAttributes = terminal.getAttributes();
    // adopted from org.jline.builtins.Nano
    // see also
    // https://en.wikibooks.org/wiki/Serial_Programming/termios#Basic_Configuration_of_a_Serial_Interface
    // no line processing
    // canonical mode off, echo off, echo newline off, extended input processing off
    Attributes newAttr = new Attributes(prevAttributes);
    newAttr.setLocalFlags(EnumSet.of(LocalFlag.ICANON, LocalFlag.ECHO, LocalFlag.IEXTEN), false);
    // turn off input processing
    newAttr.setInputFlags(EnumSet.of(Attributes.InputFlag.IXON, Attributes.InputFlag.ICRNL, Attributes.InputFlag.INLCR), false);
    // one input byte is enough to return from read, inter-character timer off
    newAttr.setControlChar(Attributes.ControlChar.VMIN, 1);
    newAttr.setControlChar(Attributes.ControlChar.VTIME, 0);
    newAttr.setControlChar(Attributes.ControlChar.VINTR, 0);
    terminal.setAttributes(newAttr);
    final Map<Signal, SignalHandler> prevSignals = new HashMap<>();
    prevSignals.put(Signal.WINCH, terminal.handle(Signal.WINCH, this::handleSignal));
    prevSignals.put(Signal.INT, terminal.handle(Signal.INT, this::handleSignal));
    prevSignals.put(Signal.QUIT, terminal.handle(Signal.QUIT, this::handleSignal));
    return Tuple2.of(prevAttributes, prevSignals);
}
Also used : Signal(org.jline.terminal.Terminal.Signal) HashMap(java.util.HashMap) Attributes(org.jline.terminal.Attributes) SignalHandler(org.jline.terminal.Terminal.SignalHandler) Terminal(org.jline.terminal.Terminal)

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