Class DeferredTerminal

java.lang.Object
org.jline.terminal.impl.DeferredTerminal
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Sized, Terminal

public class DeferredTerminal extends Object implements Terminal
A Terminal proxy that defers construction to a background thread.

Before the real terminal is ready, this proxy returns heuristic defaults ? not the result of real terminal detection ? so callers can produce output without blocking:

Methods that require the real terminal (signal handling, raw mode, input, cursor queries) block until construction completes.

If the process exits before the terminal is ready, close() drains buffered output to System.out.

Obtain via TerminalBuilder.buildDeferred().

  • Constructor Details

    • DeferredTerminal

      public DeferredTerminal()
  • Method Details

    • onComplete

      public void onComplete(Consumer<Terminal> callback)
      Registers a callback that runs once the real terminal is ready. If the terminal is already ready when this method is called, the callback is invoked immediately on the calling thread. Useful for wiring up AnsiConsole, LineReader, etc.

      Thread safety: the callback may be invoked from the build thread or the calling thread. Any mutable state captured by the callback must be safely published independently of this field.

    • complete

      public boolean complete(Terminal terminal)
    • completeExceptionally

      public void completeExceptionally(Throwable t)
    • getTerminal

      public Terminal getTerminal()
      Blocks until the real terminal is ready, then returns it.
    • isReady

      public boolean isReady()
    • writer

      public PrintWriter writer()
      Description copied from interface: Terminal
      Retrieve the Writer for this terminal. This is the standard way to write to this terminal.

      The returned writer is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Specified by:
      writer in interface Terminal
      Returns:
      The writer
    • getType

      public String getType()
      Description copied from interface: Terminal
      Returns the type of this terminal.

      The terminal type is a string identifier that describes the terminal's capabilities and behavior. Common terminal types include "xterm", "vt100", "ansi", and "dumb". This type is often used to look up terminal capabilities in the terminfo database.

      Special terminal types include:

      Specified by:
      getType in interface Terminal
      Returns:
      the terminal type identifier
      See Also:
    • getName

      public String getName()
      Description copied from interface: Terminal
      Returns the name of this terminal.

      The terminal name is typically a descriptive identifier that can be used for logging or debugging purposes. It may reflect the terminal type, connection method, or other distinguishing characteristics.

      Specified by:
      getName in interface Terminal
      Returns:
      the terminal name
    • encoding

      public Charset encoding()
      Description copied from interface: Terminal
      Returns the Charset that should be used to encode characters for Terminal.input() and Terminal.output().

      This method returns a general encoding that can be used for both input and output. For stream-specific encodings, use Terminal.inputEncoding() and Terminal.outputEncoding().

      Specified by:
      encoding in interface Terminal
      Returns:
      The terminal encoding
      See Also:
    • getSize

      public Size getSize()
      Description copied from interface: Terminal
      Retrieve the size of the visible window
      Specified by:
      getSize in interface Terminal
      Returns:
      the visible terminal size
      See Also:
    • getColumns

      public int getColumns()
      Description copied from interface: Terminal
      Get the terminal's column count.
      Specified by:
      getColumns in interface Sized
      Specified by:
      getColumns in interface Terminal
      Returns:
      the number of columns in the terminal
      See Also:
    • getRows

      public int getRows()
      Description copied from interface: Terminal
      Get the terminal's visible row count.

      Convenience method for getSize().getRows().

      Specified by:
      getRows in interface Sized
      Specified by:
      getRows in interface Terminal
      Returns:
      the number of rows in the terminal
      See Also:
    • getBooleanCapability

      public boolean getBooleanCapability(InfoCmp.Capability capability)
      Description copied from interface: Terminal
      Returns whether the terminal supports the specified boolean capability.

      Boolean capabilities indicate whether the terminal supports specific features, such as color support, automatic margins, or status line support.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Check if terminal supports colors
      if (terminal.getBooleanCapability(Capability.colors)) {
          // Use color output
      } else {
          // Use monochrome output
      }
      
      Specified by:
      getBooleanCapability in interface Terminal
      Parameters:
      capability - the boolean capability to check
      Returns:
      true if the terminal supports the capability, false otherwise
    • getNumericCapability

      public Integer getNumericCapability(InfoCmp.Capability capability)
      Description copied from interface: Terminal
      Returns the value of the specified numeric capability for this terminal.

      Numeric capabilities represent terminal properties with numeric values, such as the maximum number of colors supported, the number of function keys, or timing parameters for certain operations.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Get the number of colors supported by the terminal
      Integer colors = terminal.getNumericCapability(Capability.max_colors);
      if (colors != null && colors >= 256) {
          // Terminal supports 256 colors
      }
      
      Specified by:
      getNumericCapability in interface Terminal
      Parameters:
      capability - the numeric capability to retrieve
      Returns:
      the value of the capability, or null if the capability is not supported
    • getStringCapability

      public String getStringCapability(InfoCmp.Capability capability)
      Description copied from interface: Terminal
      Returns the string value of the specified capability for this terminal.

      String capabilities represent terminal control sequences that can be used to perform various operations, such as moving the cursor, changing colors, clearing the screen, or ringing the bell. These sequences can be parameterized using the Terminal.puts(Capability, Object...) method.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Get the control sequence for clearing the screen
      String clearScreen = terminal.getStringCapability(Capability.clear_screen);
      if (clearScreen != null) {
          // Use the sequence directly
          terminal.writer().print(clearScreen);
          terminal.flush();
      }
      
      Specified by:
      getStringCapability in interface Terminal
      Parameters:
      capability - the string capability to retrieve
      Returns:
      the string value of the capability, or null if the capability is not supported
      See Also:
    • getPalette

      public ColorPalette getPalette()
      Description copied from interface: Terminal
      Returns the color palette for this terminal.

      The color palette provides access to the terminal's color capabilities, allowing for customization and mapping of colors to terminal-specific values. This is particularly useful for terminals that support different color modes (8-color, 256-color, or true color).

      The palette allows mapping between color values and their RGB representations, and provides methods for color conversion and manipulation.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      ColorPalette palette = terminal.getPalette();
      
      // Get RGB values for a specific color
      int[] rgb = palette.toRgb(AttributedStyle.RED);
      
      Specified by:
      getPalette in interface Terminal
      Returns:
      the terminal's color palette
      See Also:
    • flush

      public void flush()
      Description copied from interface: Terminal
      Flushes any buffered output to the terminal.

      Terminal implementations may buffer output for efficiency. This method ensures that any buffered data is written to the terminal immediately. It's important to call this method when immediate display of output is required, such as when prompting for user input or updating status information.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      terminal.writer().print("Enter your name: ");
      terminal.flush(); // Ensure the prompt is displayed before reading input
      String name = terminal.reader().readLine();
      
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in interface Terminal
    • handle

      Description copied from interface: Terminal
      Registers a handler for the given Terminal.Signal.

      This method allows the application to specify custom behavior when a particular signal is raised. The handler's Terminal.SignalHandler.handle(Signal) method will be called whenever the specified signal is raised.

      Note that the JVM does not easily allow catching the Terminal.Signal.QUIT signal (Ctrl+\), which typically causes a thread dump to be displayed. This signal handling is mainly effective when connecting through an SSH socket to a virtual terminal.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Handle window resize events
      terminal.handle(Signal.WINCH, signal -> {
          Size size = terminal.getSize();
          terminal.writer().println("\nTerminal resized to " +
                                   size.getColumns() + "x" + size.getRows());
          terminal.flush();
      });
      
      // Ignore interrupt signal
      terminal.handle(Signal.INT, SignalHandler.SIG_IGN);
      
      Specified by:
      handle in interface Terminal
      Parameters:
      signal - the signal to register a handler for
      handler - the handler to be called when the signal is raised
      Returns:
      the previous signal handler that was registered for this signal
      See Also:
    • raise

      public void raise(Terminal.Signal signal)
      Description copied from interface: Terminal
      Raises the specified signal, triggering any registered handlers.

      This method manually triggers a signal, causing any registered handler for that signal to be called. This is typically not a method that application code would call directly, but is used internally by terminal implementations.

      When accessing a terminal through an SSH or Telnet connection, signals may be conveyed by the protocol and need to be raised when they reach the terminal code. Terminal implementations automatically raise signals when the input stream receives characters mapped to special control characters:

      In some cases, application code might want to programmatically raise signals to trigger specific behaviors, such as simulating a window resize event by raising Terminal.Signal.WINCH.

      Specified by:
      raise in interface Terminal
      Parameters:
      signal - the signal to raise
      See Also:
    • reader

      public NonBlockingReader reader()
      Description copied from interface: Terminal
      Retrieve the Reader for this terminal. This is the standard way to read input from this terminal. The reader is non blocking.

      The returned reader is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Specified by:
      reader in interface Terminal
      Returns:
      The non blocking reader
    • inputEncoding

      public Charset inputEncoding()
      Description copied from interface: Terminal
      Returns the Charset that should be used to decode characters from the terminal input (Terminal.input()).

      This method returns the encoding specifically for terminal input. If no specific input encoding was configured, it falls back to the general encoding from Terminal.encoding().

      Specified by:
      inputEncoding in interface Terminal
      Returns:
      The terminal input encoding
      See Also:
    • outputEncoding

      public Charset outputEncoding()
      Description copied from interface: Terminal
      Returns the Charset that should be used to encode characters for the terminal output (Terminal.output()).

      This method returns the encoding specifically for terminal output. The encoding used depends on the system stream associated with this terminal: if the terminal is bound to standard error, it uses the stderr encoding; otherwise, it uses the stdout encoding. If no specific output encoding was configured, it falls back to the general encoding from Terminal.encoding().

      Specified by:
      outputEncoding in interface Terminal
      Returns:
      The terminal output encoding
      See Also:
    • input

      public InputStream input()
      Description copied from interface: Terminal
      Retrieve the input stream for this terminal. In some rare cases, there may be a need to access the terminal input stream directly. In the usual cases, use the Terminal.reader() instead.

      The returned stream is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Specified by:
      input in interface Terminal
      Returns:
      The input stream
      See Also:
    • output

      public OutputStream output()
      Description copied from interface: Terminal
      Retrieve the output stream for this terminal. In some rare cases, there may be a need to access the terminal output stream directly. In the usual cases, use the Terminal.writer() instead.

      The returned stream is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Specified by:
      output in interface Terminal
      Returns:
      The output stream
      See Also:
    • canPauseResume

      public boolean canPauseResume()
      Description copied from interface: Terminal
      Whether this terminal supports Terminal.pause() and Terminal.resume() calls.
      Specified by:
      canPauseResume in interface Terminal
      Returns:
      whether this terminal supports Terminal.pause() and Terminal.resume() calls.
      See Also:
    • pause

      public void pause()
      Description copied from interface: Terminal
      Temporarily stops reading the input stream.

      This method pauses the terminal's input processing, which can be useful when transferring control to a subprocess or when the terminal needs to be in a specific state for certain operations. While paused, the terminal will not process input or handle signals that would normally be triggered by special characters in the input stream.

      This method returns immediately without waiting for the terminal to actually pause. To wait until the terminal has fully paused, use Terminal.pause(boolean) with a value of true.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Pause terminal input processing before running a subprocess
      terminal.pause();
      
      // Run subprocess that takes control of the terminal
      Process process = new ProcessBuilder("vim").inheritIO().start();
      process.waitFor();
      
      // Resume terminal input processing
      terminal.resume();
      
      Specified by:
      pause in interface Terminal
      See Also:
    • pause

      public void pause(boolean wait) throws InterruptedException
      Description copied from interface: Terminal
      Stop reading the input stream and optionally wait for the underlying threads to finish.
      Specified by:
      pause in interface Terminal
      Parameters:
      wait - true to wait until the terminal is actually paused
      Throws:
      InterruptedException - if the call has been interrupted
    • resume

      public void resume()
      Description copied from interface: Terminal
      Resumes reading the input stream after it has been paused.

      This method restarts the terminal's input processing after it has been temporarily stopped using Terminal.pause() or Terminal.pause(boolean). Once resumed, the terminal will continue to process input and handle signals triggered by special characters in the input stream.

      Calling this method when the terminal is not paused has no effect.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Pause terminal input processing
      terminal.pause();
      
      // Perform operations while terminal input is paused...
      
      // Resume terminal input processing
      terminal.resume();
      
      Specified by:
      resume in interface Terminal
      See Also:
    • paused

      public boolean paused()
      Description copied from interface: Terminal
      Check whether the terminal is currently reading the input stream or not. In order to process signal as quickly as possible, the terminal need to read the input stream and buffer it internally so that it can detect specific characters in the input stream (Ctrl+C, Ctrl+D, etc...) and raise the appropriate signals. However, there are some cases where this processing should be disabled, for example when handing the terminal control to a subprocess.
      Specified by:
      paused in interface Terminal
      Returns:
      whether the terminal is currently reading the input stream or not
      See Also:
    • enterRawMode

      public Attributes enterRawMode()
      Description copied from interface: Terminal
      Puts the terminal into raw mode.

      In raw mode, input is available character by character, terminal-generated signals are disabled, and special character processing is disabled. This mode is typically used for full-screen interactive applications like text editors.

      This method modifies the terminal attributes to configure raw mode and returns the original attributes, which can be used to restore the terminal to its previous state.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      Attributes originalAttributes = terminal.enterRawMode();
      
      // Use terminal in raw mode...
      
      // Restore original attributes when done
      terminal.setAttributes(originalAttributes);
      
      Specified by:
      enterRawMode in interface Terminal
      Returns:
      the original terminal attributes before entering raw mode
      See Also:
    • echo

      public boolean echo()
      Description copied from interface: Terminal
      Returns whether the terminal is currently echoing input characters.

      When echo is enabled, characters typed by the user are automatically displayed on the screen. When echo is disabled, input characters are not displayed, which is useful for password input or other sensitive information.

      Specified by:
      echo in interface Terminal
      Returns:
      true if echo is enabled, false otherwise
      See Also:
    • echo

      public boolean echo(boolean echo)
      Description copied from interface: Terminal
      Enables or disables echoing of input characters.

      When echo is enabled, characters typed by the user are automatically displayed on the screen. When echo is disabled, input characters are not displayed, which is useful for password input or other sensitive information.

      Example usage for password input:

      Terminal terminal = TerminalBuilder.terminal();
      boolean oldEcho = terminal.echo(false); // Disable echo
      String password = readPassword(terminal);
      terminal.echo(oldEcho); // Restore previous echo state
      
      Specified by:
      echo in interface Terminal
      Parameters:
      echo - true to enable echo, false to disable it
      Returns:
      the previous echo state
    • getAttributes

      public Attributes getAttributes()
      Description copied from interface: Terminal
      Returns the current terminal attributes.

      Terminal attributes control various aspects of terminal behavior, including:

      • Input processing - How input characters are processed (e.g., character mapping, parity checking)
      • Output processing - How output characters are processed (e.g., newline translation)
      • Control settings - Hardware settings like baud rate and character size
      • Local settings - Terminal behavior settings like echo, canonical mode, and signal generation
      • Control characters - Special characters like EOF, interrupt, and erase

      The returned Attributes object is a copy of the terminal's current attributes and can be safely modified without affecting the terminal until it is applied using Terminal.setAttributes(Attributes). This allows for making multiple changes to the attributes before applying them all at once.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Get current attributes
      Attributes attrs = terminal.getAttributes();
      
      // Modify attributes
      attrs.setLocalFlag(LocalFlag.ECHO, false);      // Disable echo
      attrs.setInputFlag(InputFlag.ICRNL, false);     // Disable CR to NL mapping
      attrs.setControlChar(ControlChar.VMIN, 1);      // Set minimum input to 1 character
      attrs.setControlChar(ControlChar.VTIME, 0);     // Set timeout to 0 deciseconds
      
      // Apply modified attributes
      terminal.setAttributes(attrs);
      
      Specified by:
      getAttributes in interface Terminal
      Returns:
      a copy of the terminal's current attributes
      See Also:
    • setAttributes

      public void setAttributes(Attributes attr)
      Description copied from interface: Terminal
      Sets the terminal attributes to the specified values.

      This method applies the specified attributes to the terminal, changing its behavior according to the settings in the Attributes object. The terminal makes a copy of the provided attributes, so further modifications to the attr object will not affect the terminal until this method is called again.

      Terminal attributes control various aspects of terminal behavior, including input and output processing, control settings, local settings, and special control characters. Changing these attributes allows for fine-grained control over how the terminal processes input and output.

      Common attribute modifications include:

      • Disabling echo for password input
      • Enabling/disabling canonical mode for line-by-line or character-by-character input
      • Disabling signal generation for custom handling of Ctrl+C and other control sequences
      • Changing control characters like the interrupt character or end-of-file character

      For convenience, the Terminal.enterRawMode() method provides a pre-configured set of attributes suitable for full-screen interactive applications.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Save original attributes for later restoration
      Attributes originalAttrs = terminal.getAttributes();
      
      try {
          // Create and configure new attributes
          Attributes attrs = new Attributes(originalAttrs);
          attrs.setLocalFlag(LocalFlag.ECHO, false);      // Disable echo for password input
          attrs.setLocalFlag(LocalFlag.ICANON, false);    // Disable canonical mode
      
          // Apply the new attributes
          terminal.setAttributes(attrs);
      
          // Use terminal with modified attributes...
      } finally {
          // Restore original attributes
          terminal.setAttributes(originalAttrs);
      }
      
      Specified by:
      setAttributes in interface Terminal
      Parameters:
      attr - the attributes to apply to the terminal
      See Also:
    • setSize

      public void setSize(Sized size)
      Description copied from interface: Terminal
      Requests that the terminal adopt the specified window dimensions. Implementations may apply constraints or ignore the request if resizing is unsupported; callers should use Terminal.getSize() to observe the effective size after calling this method.
      Specified by:
      setSize in interface Terminal
      Parameters:
      size - the desired terminal size (columns and rows)
      See Also:
    • getBufferSize

      public Size getBufferSize()
      Description copied from interface: Terminal
      Retrieve the size of the window buffer. Some terminals can be configured to have a buffer size larger than the visible window size and provide scroll bars. In such cases, this method should attempt to return the size of the whole buffer. The getBufferSize() method can be used to avoid wrapping when using the terminal in a line editing mode, while the Terminal.getSize() method should be used when using full screen mode.
      Specified by:
      getBufferSize in interface Terminal
      Returns:
      the terminal buffer size
      See Also:
    • puts

      public boolean puts(InfoCmp.Capability capability, Object... params)
      Description copied from interface: Terminal
      Outputs a terminal control string for the specified capability.

      This method formats and outputs a control sequence for the specified terminal capability, with the given parameters. It's used to perform terminal operations such as cursor movement, screen clearing, color changes, and other terminal-specific functions.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Clear the screen
      terminal.puts(Capability.clear_screen);
      
      // Move cursor to position (10, 20)
      terminal.puts(Capability.cursor_address, 20, 10);
      
      // Set foreground color to red
      terminal.puts(Capability.set_a_foreground, 1);
      
      Specified by:
      puts in interface Terminal
      Parameters:
      capability - the terminal capability to use
      params - the parameters for the capability
      Returns:
      true if the capability is supported and was output, false otherwise
      See Also:
    • getCursorPosition

      public Cursor getCursorPosition(IntConsumer discarded)
      Description copied from interface: Terminal
      Query the terminal to report the cursor position. As the response is read from the input stream, some characters may be read before the cursor position is actually read. Those characters can be given back using org.jline.keymap.BindingReader#runMacro(String)
      Specified by:
      getCursorPosition in interface Terminal
      Parameters:
      discarded - a consumer receiving discarded characters
      Returns:
      null if cursor position reporting is not supported or a valid cursor position
    • hasMouseSupport

      public boolean hasMouseSupport()
      Description copied from interface: Terminal
      Returns whether the terminal has support for mouse tracking.

      Mouse support allows the terminal to report mouse events such as clicks, movement, and wheel scrolling. Not all terminals support mouse tracking, so this method should be called before attempting to enable mouse tracking with Terminal.trackMouse(MouseTracking).

      Common terminal emulators that support mouse tracking include xterm, iTerm2, and modern versions of GNOME Terminal and Konsole. Terminal multiplexers like tmux and screen may also support mouse tracking depending on their configuration and the capabilities of the underlying terminal.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      if (terminal.hasMouseSupport()) {
          // Enable mouse tracking
          terminal.trackMouse(MouseTracking.Normal);
      
          // Process mouse events
          // ...
      } else {
          System.out.println("Mouse tracking not supported by this terminal");
      }
      
      Specified by:
      hasMouseSupport in interface Terminal
      Returns:
      true if the terminal supports mouse tracking, false otherwise
      See Also:
    • trackMouse

      public boolean trackMouse(Terminal.MouseTracking tracking)
      Description copied from interface: Terminal
      Enables or disables mouse tracking with the specified mode.

      This method configures the terminal to report mouse events according to the specified tracking mode. When mouse tracking is enabled, the terminal will send special escape sequences to the input stream whenever mouse events occur. These sequences begin with the InfoCmp.Capability.key_mouse sequence, followed by data that describes the specific mouse event.

      The tracking mode determines which mouse events are reported:

      To process mouse events, applications should:

      1. Enable mouse tracking by calling this method with the desired mode
      2. Monitor the input stream for the InfoCmp.Capability.key_mouse sequence
      3. When this sequence is detected, call Terminal.readMouseEvent() to decode the event
      4. Process the returned MouseEvent as needed

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      if (terminal.hasMouseSupport()) {
          // Enable tracking of all mouse events
          boolean supported = terminal.trackMouse(MouseTracking.Any);
      
          if (supported) {
              System.out.println("Mouse tracking enabled");
              // Set up input processing to detect and handle mouse events
          }
      }
      
      Specified by:
      trackMouse in interface Terminal
      Parameters:
      tracking - the mouse tracking mode to enable, or Terminal.MouseTracking.Off to disable tracking
      Returns:
      true if the requested mouse tracking mode is supported, false otherwise
      See Also:
    • getCurrentMouseTracking

      public Terminal.MouseTracking getCurrentMouseTracking()
      Description copied from interface: Terminal
      Returns the current mouse tracking mode.
      Specified by:
      getCurrentMouseTracking in interface Terminal
      See Also:
    • readMouseEvent

      public MouseEvent readMouseEvent()
      Description copied from interface: Terminal
      Read a MouseEvent from the terminal input stream. Such an event must have been detected by scanning the terminal's InfoCmp.Capability.key_mouse in the stream immediately before reading the event.

      This method should be called after detecting the terminal's InfoCmp.Capability.key_mouse sequence in the input stream, which indicates that a mouse event has occurred. The method reads the necessary data from the input stream and decodes it into a MouseEvent object containing information about the event type, button, modifiers, and coordinates.

      Before calling this method, mouse tracking must be enabled using Terminal.trackMouse(MouseTracking) with an appropriate tracking mode.

      The typical pattern for handling mouse events is:

      1. Enable mouse tracking with Terminal.trackMouse(MouseTracking)
      2. Read input from the terminal
      3. When the InfoCmp.Capability.key_mouse sequence is detected, call this method
      4. Process the returned MouseEvent

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      if (terminal.hasMouseSupport()) {
          terminal.trackMouse(MouseTracking.Normal);
      
          // Read input and look for mouse events
          String keyMouse = terminal.getStringCapability(Capability.key_mouse);
          // When keyMouse sequence is detected in the input:
          MouseEvent event = terminal.readMouseEvent();
          System.out.println("Mouse event: " + event.getType() +
                            " at " + event.getX() + "," + event.getY());
      }
      
      Specified by:
      readMouseEvent in interface Terminal
      Returns:
      the decoded mouse event containing event type, button, modifiers, and coordinates
      See Also:
    • readMouseEvent

      public MouseEvent readMouseEvent(IntSupplier reader)
      Description copied from interface: Terminal
      Reads and decodes a mouse event using the provided input supplier.

      This method is similar to Terminal.readMouseEvent(), but allows reading mouse event data from a custom input source rather than the terminal's default input stream. This can be useful in situations where input is being processed through a different channel or when implementing custom input handling.

      The input supplier should provide the raw bytes of the mouse event data as integers. The method will read the necessary data from the supplier and decode it into a MouseEvent object containing information about the event type, button, modifiers, and coordinates.

      This method is primarily intended for advanced use cases where the standard Terminal.readMouseEvent() method is not sufficient.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      // Create a custom input supplier
      IntSupplier customReader = new IntSupplier() {
          private byte[] data = ...; // Mouse event data
          private int index = 0;
      
          public int getAsInt() {
              return (index < data.length) ? data[index++] & 0xFF : -1;
          }
      };
      
      // Read mouse event using the custom supplier
      MouseEvent event = terminal.readMouseEvent(customReader);
      
      Specified by:
      readMouseEvent in interface Terminal
      Parameters:
      reader - the input supplier that provides the raw bytes of the mouse event data
      Returns:
      the decoded mouse event containing event type, button, modifiers, and coordinates
      See Also:
    • readMouseEvent

      public MouseEvent readMouseEvent(String prefix)
      Description copied from interface: Terminal
      Reads and decodes a mouse event with a specified prefix that has already been consumed.

      This method is similar to Terminal.readMouseEvent(), but it allows specifying a prefix that has already been consumed. This is useful when the mouse event prefix (e.g., "\033[<" or "\033[M") has been consumed by the key binding detection, and we need to continue parsing from the current position.

      This method is primarily intended for advanced use cases where the standard Terminal.readMouseEvent() method is not sufficient, particularly when dealing with key binding systems that may consume part of the mouse event sequence.

      Specified by:
      readMouseEvent in interface Terminal
      Parameters:
      prefix - the prefix that has already been consumed, or null if none
      Returns:
      the decoded mouse event containing event type, button, modifiers, and coordinates
      See Also:
    • readMouseEvent

      public MouseEvent readMouseEvent(IntSupplier reader, String prefix)
      Description copied from interface: Terminal
      Reads and decodes a mouse event using the provided input supplier with a specified prefix that has already been consumed.

      This method combines the functionality of Terminal.readMouseEvent(IntSupplier) and Terminal.readMouseEvent(String), allowing both a custom input supplier and a prefix to be specified. This is useful for advanced input handling scenarios where both customization of the input source and handling of partially consumed sequences are needed.

      Specified by:
      readMouseEvent in interface Terminal
      Parameters:
      reader - the input supplier that provides the raw bytes of the mouse event data
      prefix - the prefix that has already been consumed, or null if none
      Returns:
      the decoded mouse event containing event type, button, modifiers, and coordinates
      See Also:
    • hasFocusSupport

      public boolean hasFocusSupport()
      Description copied from interface: Terminal
      Returns whether the terminal has support for focus tracking.

      Focus tracking allows the terminal to report when it gains or loses focus. This can be useful for applications that need to change their behavior or appearance based on whether they are currently in focus.

      Not all terminals support focus tracking, so this method should be called before attempting to enable focus tracking with Terminal.trackFocus(boolean).

      When focus tracking is enabled and supported, the terminal will send special escape sequences to the input stream when focus is gained ("\33[I") or lost ("\33[O"). Applications can detect these sequences to respond to focus changes.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      if (terminal.hasFocusSupport()) {
          // Enable focus tracking
          terminal.trackFocus(true);
      
          // Now the application can detect focus changes
          // by looking for "\33[I" and "\33[O" in the input stream
      } else {
          System.out.println("Focus tracking not supported by this terminal");
      }
      
      Specified by:
      hasFocusSupport in interface Terminal
      Returns:
      true if the terminal supports focus tracking, false otherwise
      See Also:
    • trackFocus

      public boolean trackFocus(boolean tracking)
      Description copied from interface: Terminal
      Enables or disables focus tracking mode.

      Focus tracking allows applications to detect when the terminal window gains or loses focus. When focus tracking is enabled, the terminal will send special escape sequences to the input stream whenever the focus state changes:

      • When the terminal gains focus: "\33[I" (ESC [ I)
      • When the terminal loses focus: "\33[O" (ESC [ O)

      Applications can monitor the input stream for these sequences to detect focus changes and respond accordingly, such as by changing the cursor appearance, pausing animations, or adjusting the display.

      Not all terminals support focus tracking. Use Terminal.hasFocusSupport() to check whether focus tracking is supported before enabling it.

      Example usage:

      Terminal terminal = TerminalBuilder.terminal();
      
      if (terminal.hasFocusSupport()) {
          // Enable focus tracking
          boolean enabled = terminal.trackFocus(true);
      
          if (enabled) {
              System.out.println("Focus tracking enabled");
              // Set up input processing to detect focus change sequences
          }
      }
      
      Specified by:
      trackFocus in interface Terminal
      Parameters:
      tracking - true to enable focus tracking, false to disable it
      Returns:
      true if focus tracking is supported and the operation succeeded, false otherwise
      See Also:
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException