org.jline.terminal.Attributes Java Examples

The following examples show how to use org.jline.terminal.Attributes. 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: 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 #2
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 #3
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 #4
Source File: SshShellUtilsTest.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@Test
void testFillAttributes() {
    Attributes attributes = new Attributes();
    Map<PtyMode, Integer> map = new HashMap<>();
    for (PtyMode value : PtyMode.values()) {
        map.put(value, 1);
    }
    SshShellUtils.fill(attributes, map);
    assertFalse(attributes.getControlChars().isEmpty());
}
 
Example #5
Source File: StatusPrinter.java    From presto with Apache License 2.0 4 votes vote down vote up
public void printInitialStatusUpdates(Terminal terminal)
{
    Attributes originalAttributes = terminal.enterRawMode();
    long lastPrint = System.nanoTime();
    try {
        WarningsPrinter warningsPrinter = new ConsoleWarningsPrinter(console);
        while (client.isRunning()) {
            try {
                // exit status loop if there is pending output
                if (client.currentData().getData() != null) {
                    return;
                }

                // check if time to update screen
                boolean update = nanosSince(lastPrint).getValue(SECONDS) >= 0.5;

                // check for keyboard input
                int key = readKey(terminal);
                if (key == CTRL_P) {
                    client.cancelLeafStage();
                }
                else if (key == CTRL_C) {
                    updateScreen(warningsPrinter);
                    update = false;
                    client.close();
                }
                else if (toUpperCase(key) == 'D') {
                    debug = !debug;
                    console.resetScreen();
                    update = true;
                }

                // update screen
                if (update) {
                    updateScreen(warningsPrinter);
                    lastPrint = System.nanoTime();
                }

                // fetch next results (server will wait for a while if no data)
                client.advance();
            }
            catch (RuntimeException e) {
                log.debug(e, "error printing status");
                if (debug) {
                    e.printStackTrace(out);
                }
            }
        }
    }
    finally {
        console.resetScreen();
        discardKeys(terminal);
        terminal.setAttributes(originalAttributes);
    }
}
 
Example #6
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 #7
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 #8
Source File: SshShellUtils.java    From ssh-shell-spring-boot with Apache License 2.0 4 votes vote down vote up
/**
  * Fill attributes with given modes
  *
  * @param attr     attributes
  * @param ptyModes pty modes
  */
 public static void fill(Attributes attr, Map<PtyMode, Integer> ptyModes) {
     for (Map.Entry<PtyMode, Integer> e : ptyModes.entrySet()) {
         switch (e.getKey()) {
             case VINTR:
                 attr.setControlChar(Attributes.ControlChar.VINTR, e.getValue());
                 break;
             case VQUIT:
                 attr.setControlChar(Attributes.ControlChar.VQUIT, e.getValue());
                 break;
             case VERASE:
                 attr.setControlChar(Attributes.ControlChar.VERASE, e.getValue());
                 break;
             case VKILL:
                 attr.setControlChar(Attributes.ControlChar.VKILL, e.getValue());
                 break;
             case VEOF:
                 attr.setControlChar(Attributes.ControlChar.VEOF, e.getValue());
                 break;
             case VEOL:
                 attr.setControlChar(Attributes.ControlChar.VEOL, e.getValue());
                 break;
             case VEOL2:
                 attr.setControlChar(Attributes.ControlChar.VEOL2, e.getValue());
                 break;
             case VSTART:
                 attr.setControlChar(Attributes.ControlChar.VSTART, e.getValue());
                 break;
             case VSTOP:
                 attr.setControlChar(Attributes.ControlChar.VSTOP, e.getValue());
                 break;
             case VSUSP:
                 attr.setControlChar(Attributes.ControlChar.VSUSP, e.getValue());
                 break;
             case VDSUSP:
                 attr.setControlChar(Attributes.ControlChar.VDSUSP, e.getValue());
                 break;
             case VREPRINT:
                 attr.setControlChar(Attributes.ControlChar.VREPRINT, e.getValue());
                 break;
             case VWERASE:
                 attr.setControlChar(Attributes.ControlChar.VWERASE, e.getValue());
                 break;
             case VLNEXT:
                 attr.setControlChar(Attributes.ControlChar.VLNEXT, e.getValue());
                 break;
/*
case VFLUSH:
		attr.setControlChar(Attributes.ControlChar.VMIN, e.getValue());
		break;
case VSWTCH:
		attr.setControlChar(Attributes.ControlChar.VTIME, e.getValue());
		break;
*/
             case VSTATUS:
                 attr.setControlChar(Attributes.ControlChar.VSTATUS, e.getValue());
                 break;
             case VDISCARD:
                 attr.setControlChar(Attributes.ControlChar.VDISCARD, e.getValue());
                 break;
             case ECHO:
                 attr.setLocalFlag(Attributes.LocalFlag.ECHO, e.getValue() != 0);
                 break;
             case ICANON:
                 attr.setLocalFlag(Attributes.LocalFlag.ICANON, e.getValue() != 0);
                 break;
             case ISIG:
                 attr.setLocalFlag(Attributes.LocalFlag.ISIG, e.getValue() != 0);
                 break;
             case ICRNL:
                 attr.setInputFlag(Attributes.InputFlag.ICRNL, e.getValue() != 0);
                 break;
             case INLCR:
                 attr.setInputFlag(Attributes.InputFlag.INLCR, e.getValue() != 0);
                 break;
             case IGNCR:
                 attr.setInputFlag(Attributes.InputFlag.IGNCR, e.getValue() != 0);
                 break;
             case OCRNL:
                 attr.setOutputFlag(Attributes.OutputFlag.OCRNL, e.getValue() != 0);
                 break;
             case ONLCR:
                 attr.setOutputFlag(Attributes.OutputFlag.ONLCR, e.getValue() != 0);
                 break;
             case ONLRET:
                 attr.setOutputFlag(Attributes.OutputFlag.ONLRET, e.getValue() != 0);
                 break;
             case OPOST:
                 attr.setOutputFlag(Attributes.OutputFlag.OPOST, e.getValue() != 0);
                 break;
             default:
                 // nothing to do
         }
     }
 }
 
Example #9
Source File: QueryResultLogView.java    From samza with Apache License 2.0 4 votes vote down vote up
private TerminalStatus setupTerminal() {
  TerminalStatus prevStatus = new TerminalStatus();

  // Signal handlers
  prevStatus.handlerInt = terminal.handle(Terminal.Signal.INT, this::handleSignal);
  prevStatus.handlerQuit = terminal.handle(Terminal.Signal.QUIT, this::handleSignal);
  prevStatus.handlerTstp = terminal.handle(Terminal.Signal.TSTP, this::handleSignal);
  prevStatus.handlerCont = terminal.handle(Terminal.Signal.CONT, this::handleSignal);
  prevStatus.handlerWinch = terminal.handle(Terminal.Signal.WINCH, this::handleSignal);

  // Attributes
  prevStatus.attributes = terminal.getAttributes();
  Attributes newAttributes = new Attributes(prevStatus.attributes);
  // (003, ETX, Ctrl-C, or also 0177, DEL, rubout) Interrupt char‐
  // acter (INTR).  Send a SIGINT signal.  Recognized when ISIG is
  // set, and then not passed as input.
  newAttributes.setControlChar(Attributes.ControlChar.VINTR, 0);
  // (034, FS, Ctrl-\) Quit character (QUIT).  Send SIGQUIT signal.
  // Recognized when ISIG is set, and then not passed as input.
  // newAttributes.setControlChar(Attributes.ControlChar.VQUIT, 0);
  newAttributes.setControlChar(Attributes.ControlChar.VMIN, 1);
  newAttributes.setControlChar(Attributes.ControlChar.VTIME, 0);
  // Enables signals and SIGTTOU signal to the process group of a background
  // process which tries to write to our terminal
  newAttributes.setLocalFlags(
          EnumSet.of(Attributes.LocalFlag.ISIG, Attributes.LocalFlag.TOSTOP), true);
  // No canonical mode, no echo, and no implementation-defined input processing
  newAttributes.setLocalFlags(EnumSet.of(
          Attributes.LocalFlag.ICANON, Attributes.LocalFlag.ECHO,
          Attributes.LocalFlag.IEXTEN), false);
  // Input flags
  newAttributes.setInputFlags(EnumSet.of(
          Attributes.InputFlag.ICRNL, Attributes.InputFlag.INLCR, Attributes.InputFlag.IXON), false);
  terminal.setAttributes(newAttributes);

  // Capabilities
  // tput smcup; use alternate screen
  terminal.puts(InfoCmp.Capability.enter_ca_mode);
  terminal.puts(InfoCmp.Capability.cursor_invisible);
  terminal.puts(InfoCmp.Capability.cursor_home);

  terminal.flush();

  return prevStatus;
}
 
Example #10
Source File: RuntimeClient.java    From tesb-studio-se with Apache License 2.0 4 votes vote down vote up
private static int getFlag(Attributes attributes, InputFlag flag) {
    return attributes.getInputFlag(flag) ? 1 : 0;
}
 
Example #11
Source File: RuntimeClient.java    From tesb-studio-se with Apache License 2.0 4 votes vote down vote up
private static int getFlag(Attributes attributes, OutputFlag flag) {
    return attributes.getOutputFlag(flag) ? 1 : 0;
}
 
Example #12
Source File: RuntimeClient.java    From tesb-studio-se with Apache License 2.0 4 votes vote down vote up
private static int getFlag(Attributes attributes, LocalFlag flag) {
    return attributes.getLocalFlag(flag) ? 1 : 0;
}
 
Example #13
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);
}