Search in sources :

Example 1 with SignalHandler

use of org.jline.terminal.Terminal.SignalHandler in project felix by apache.

the class Shell method runShell.

private Object runShell(final CommandSession session, Terminal terminal, LineReader reader) throws InterruptedException {
    AtomicBoolean reading = new AtomicBoolean();
    session.setJobListener((job, previous, current) -> {
        if (previous == Status.Background || current == Status.Background || previous == Status.Suspended || current == Status.Suspended) {
            int width = terminal.getWidth();
            String status = current.name().toLowerCase();
            terminal.writer().write(getStatusLine(job, width, status));
            terminal.flush();
            if (reading.get() && !stopping.get()) {
                reader.callWidget(LineReader.REDRAW_LINE);
                reader.callWidget(LineReader.REDISPLAY);
            }
        }
    });
    SignalHandler intHandler = terminal.handle(Signal.INT, s -> {
        Job current = session.foregroundJob();
        if (current != null) {
            current.interrupt();
        }
    });
    SignalHandler suspHandler = terminal.handle(Signal.TSTP, s -> {
        Job current = session.foregroundJob();
        if (current != null) {
            current.suspend();
        }
    });
    Object result = null;
    try {
        while (!stopping.get()) {
            try {
                reading.set(true);
                try {
                    String prompt = Shell.getPrompt(session);
                    String rprompt = Shell.getRPrompt(session);
                    if (stopping.get()) {
                        break;
                    }
                    reader.readLine(prompt, rprompt, null, null);
                } finally {
                    reading.set(false);
                }
                ParsedLine parsedLine = reader.getParsedLine();
                if (parsedLine == null) {
                    throw new EndOfFileException();
                }
                try {
                    result = session.execute(((ParsedLineImpl) parsedLine).program());
                    // set $_ to last result
                    session.put(Shell.VAR_RESULT, result);
                    if (result != null && !Boolean.FALSE.equals(session.get(".Gogo.format"))) {
                        System.out.println(session.format(result, Converter.INSPECT));
                    }
                } catch (Exception e) {
                    AttributedStringBuilder sb = new AttributedStringBuilder();
                    sb.style(sb.style().foreground(AttributedStyle.RED));
                    sb.append(e.toString());
                    sb.style(sb.style().foregroundDefault());
                    terminal.writer().println(sb.toAnsi(terminal));
                    terminal.flush();
                    session.put(Shell.VAR_EXCEPTION, e);
                }
                waitJobCompletion(session);
            } catch (UserInterruptException e) {
            // continue;
            } catch (EndOfFileException e) {
                reader.getHistory().save();
                break;
            }
        }
    } finally {
        terminal.handle(Signal.INT, intHandler);
        terminal.handle(Signal.TSTP, suspHandler);
    }
    return result;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EndOfFileException(org.jline.reader.EndOfFileException) ParsedLine(org.jline.reader.ParsedLine) SignalHandler(org.jline.terminal.Terminal.SignalHandler) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Job(org.apache.felix.service.command.Job) UserInterruptException(org.jline.reader.UserInterruptException) EndOfFileException(org.jline.reader.EndOfFileException) IOException(java.io.IOException) UserInterruptException(org.jline.reader.UserInterruptException)

Example 2 with SignalHandler

use of org.jline.terminal.Terminal.SignalHandler 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

SignalHandler (org.jline.terminal.Terminal.SignalHandler)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Job (org.apache.felix.service.command.Job)1 EndOfFileException (org.jline.reader.EndOfFileException)1 ParsedLine (org.jline.reader.ParsedLine)1 UserInterruptException (org.jline.reader.UserInterruptException)1 Attributes (org.jline.terminal.Attributes)1 Terminal (org.jline.terminal.Terminal)1 Signal (org.jline.terminal.Terminal.Signal)1 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)1