org.jline.terminal.Terminal.SignalHandler Java Examples

The following examples show how to use org.jline.terminal.Terminal.SignalHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShellUtils.java    From rug-cli with GNU General Public License v3.0 7 votes vote down vote up
public static LineReader lineReader(File historyPath, Optional<SignalHandler> handler,
        Completer... completers) {
    // Protect the history file as may contain sensitive information
    FileUtils.setPermissionsToOwnerOnly(historyPath);

    // Create JLine LineReader
    History history = new DefaultHistory();
    LineReader reader = LineReaderBuilder.builder().terminal(terminal(handler)).history(history)
            .parser(new DefaultParser()).variable(LineReader.HISTORY_FILE, historyPath)
            .completer(new AggregateCompleter(completers)).highlighter(new DefaultHighlighter())
            .build();
    history.attach(reader);

    setOptions(reader);

    return reader;
}
 
Example #2
Source File: Query.java    From presto with Apache License 2.0 6 votes vote down vote up
public boolean renderOutput(Terminal terminal, PrintStream out, PrintStream errorChannel, OutputFormat outputFormat, boolean usePager, boolean showProgress)
{
    Thread clientThread = Thread.currentThread();
    SignalHandler oldHandler = terminal.handle(Signal.INT, signal -> {
        if (ignoreUserInterrupt.get() || client.isClientAborted()) {
            return;
        }
        client.close();
        clientThread.interrupt();
    });
    try {
        return renderQueryOutput(terminal, out, errorChannel, outputFormat, usePager, showProgress);
    }
    finally {
        terminal.handle(Signal.INT, oldHandler);
        Thread.interrupted(); // clear interrupt status
    }
}
 
Example #3
Source File: QueryPreprocessor.java    From presto with Apache License 2.0 6 votes vote down vote up
public static String preprocessQuery(Terminal terminal, Optional<String> catalog, Optional<String> schema, String query, List<String> preprocessorCommand, Duration timeout)
        throws QueryPreprocessorException
{
    Thread clientThread = Thread.currentThread();
    SignalHandler oldHandler = terminal.handle(Signal.INT, signal -> clientThread.interrupt());
    try {
        if (REAL_TERMINAL) {
            System.out.print(PREPROCESSING_QUERY_MESSAGE);
            System.out.flush();
        }
        return preprocessQueryInternal(catalog, schema, query, preprocessorCommand, timeout);
    }
    finally {
        if (REAL_TERMINAL) {
            System.out.print("\r" + Strings.repeat(" ", PREPROCESSING_QUERY_MESSAGE.length()) + "\r");
            System.out.flush();
        }
        terminal.handle(Signal.INT, oldHandler);
        Thread.interrupted(); // clear interrupt status
    }
}
 
Example #4
Source File: CliView.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: CliView.java    From flink with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #6
Source File: CliView.java    From flink with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #7
Source File: CliView.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void restoreTerminal(Tuple2<Attributes, Map<Signal, SignalHandler>> prev) {
	final Terminal terminal = client.getTerminal();

	terminal.setAttributes(prev.f0);
	prev.f1.forEach(terminal::handle);
}
 
Example #8
Source File: CliView.java    From flink with Apache License 2.0 4 votes vote down vote up
private void restoreTerminal(Tuple2<Attributes, Map<Signal, SignalHandler>> prev) {
	final Terminal terminal = client.getTerminal();

	terminal.setAttributes(prev.f0);
	prev.f1.forEach(terminal::handle);
}
 
Example #9
Source File: ShellCommandRunner.java    From rug-cli with GNU General Public License v3.0 4 votes vote down vote up
private SignalHandler signalHandler() {
    return s -> {
        markInterrupted();
    };
}
 
Example #10
Source File: CliView.java    From flink with Apache License 2.0 4 votes vote down vote up
private void restoreTerminal(Tuple2<Attributes, Map<Signal, SignalHandler>> prev) {
	final Terminal terminal = client.getTerminal();

	terminal.setAttributes(prev.f0);
	prev.f1.forEach(terminal::handle);
}