Search in sources :

Example 1 with BasicConsole

use of io.quarkus.dev.console.BasicConsole in project quarkus by quarkusio.

the class ConsoleHelper method installConsole.

public static synchronized void installConsole(TestConfig config, ConsoleConfig consoleConfig, ConsoleRuntimeConfig consoleRuntimeConfig, io.quarkus.runtime.logging.ConsoleConfig logConfig, boolean test) {
    if (QuarkusConsole.installed) {
        return;
    }
    boolean colorEnabled = ColorSupport.isColorEnabled(consoleRuntimeConfig, logConfig);
    QuarkusConsole.installed = true;
    // if there is no color we need a basic console
    // note that we never enable input for tests
    // surefire communicates of stdin, so this can mess with it
    boolean inputSupport = !test && !config.disableConsoleInput.orElse(consoleConfig.disableInput);
    if (!inputSupport) {
        // note that in this case we don't hold onto anything from this class loader
        // which is important for the test suite
        QuarkusConsole.INSTANCE = new BasicConsole(colorEnabled, false, QuarkusConsole.ORIGINAL_OUT, System.console());
        return;
    }
    try {
        new TerminalConnection(new Consumer<Connection>() {

            @Override
            public void accept(Connection connection) {
                if (connection.supportsAnsi() && !config.basicConsole.orElse(consoleConfig.basic)) {
                    QuarkusConsole.INSTANCE = new AeshConsole(connection);
                } else {
                    LinkedBlockingDeque<Integer> queue = new LinkedBlockingDeque<>();
                    if (inputSupport) {
                        connection.openNonBlocking();
                    }
                    connection.setStdinHandler(new Consumer<int[]>() {

                        @Override
                        public void accept(int[] ints) {
                            QuarkusConsole.StateChangeInputStream redirectIn = QuarkusConsole.REDIRECT_IN;
                            for (int i : ints) {
                                if (redirectIn != null && !redirectIn.acceptInput(i)) {
                                    queue.add(i);
                                }
                            }
                        }
                    });
                    connection.setSignalHandler(event -> {
                        switch(event) {
                            case INT:
                                // todo: why does async exit not work here
                                // Quarkus.asyncExit();
                                // end(conn);
                                new Thread(new Runnable() {

                                    @Override
                                    public void run() {
                                        System.exit(0);
                                    }
                                }).start();
                                break;
                        }
                    });
                    connection.setCloseHandler(new Consumer<Void>() {

                        @Override
                        public void accept(Void unused) {
                            queue.add(-1);
                        }
                    });
                    QuarkusConsole.INSTANCE = new BasicConsole(colorEnabled, inputSupport, connection::write, new Supplier<Integer>() {

                        @Override
                        public Integer get() {
                            try {
                                return queue.takeFirst();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    });
                }
            }
        });
    } catch (IOException e) {
        QuarkusConsole.INSTANCE = new BasicConsole(colorEnabled, false, QuarkusConsole.ORIGINAL_OUT, System.console());
    }
    QuarkusConsole.installRedirects();
}
Also used : Consumer(java.util.function.Consumer) Connection(org.aesh.terminal.Connection) BasicConsole(io.quarkus.dev.console.BasicConsole) QuarkusConsole(io.quarkus.dev.console.QuarkusConsole) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) IOException(java.io.IOException) TestConfig(io.quarkus.deployment.dev.testing.TestConfig) TerminalConnection(org.aesh.readline.tty.terminal.TerminalConnection) Supplier(java.util.function.Supplier) ColorSupport(io.quarkus.runtime.util.ColorSupport) ConsoleRuntimeConfig(io.quarkus.runtime.console.ConsoleRuntimeConfig) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) QuarkusConsole(io.quarkus.dev.console.QuarkusConsole) Connection(org.aesh.terminal.Connection) TerminalConnection(org.aesh.readline.tty.terminal.TerminalConnection) IOException(java.io.IOException) BasicConsole(io.quarkus.dev.console.BasicConsole) TerminalConnection(org.aesh.readline.tty.terminal.TerminalConnection) Consumer(java.util.function.Consumer)

Aggregations

TestConfig (io.quarkus.deployment.dev.testing.TestConfig)1 BasicConsole (io.quarkus.dev.console.BasicConsole)1 QuarkusConsole (io.quarkus.dev.console.QuarkusConsole)1 ConsoleRuntimeConfig (io.quarkus.runtime.console.ConsoleRuntimeConfig)1 ColorSupport (io.quarkus.runtime.util.ColorSupport)1 IOException (java.io.IOException)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 TerminalConnection (org.aesh.readline.tty.terminal.TerminalConnection)1 Connection (org.aesh.terminal.Connection)1