Class TtyDetect

java.lang.Object
org.aesh.terminal.tty.TtyDetect

public final class TtyDetect extends Object
Utility for detecting whether file descriptors are connected to a terminal.

On Java 22+, uses FFM to call POSIX isatty() for accurate per-fd detection. On older Java versions, falls back to System.console() and Console.isTerminal() (Java 22+) as heuristics.

Typical usage:

if (TtyDetect.isStdinTty()) {
    // Interactive mode — show prompt, enable completion
} else {
    // Piped/redirected — read commands from stdin
}

if (!TtyDetect.isStdoutTty()) {
    // Output is piped — disable colors, use machine-readable format
}
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    File descriptor for standard error.
    static final int
    File descriptor for standard input.
    static final int
    File descriptor for standard output.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Check if standard error is connected to a terminal.
    static boolean
    Check if standard input is connected to a terminal.
    static boolean
    Check if standard output is connected to a terminal.
    static boolean
    isTty(int fd)
    Check if the given file descriptor is connected to a terminal.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FD_STDIN

      public static final int FD_STDIN
      File descriptor for standard input.
      See Also:
    • FD_STDOUT

      public static final int FD_STDOUT
      File descriptor for standard output.
      See Also:
    • FD_STDERR

      public static final int FD_STDERR
      File descriptor for standard error.
      See Also:
  • Method Details

    • isTty

      public static boolean isTty(int fd)
      Check if the given file descriptor is connected to a terminal.

      On Java 22+ with POSIX systems, this uses the native isatty() function via FFM for accurate detection. On older Java versions or unsupported platforms, falls back to heuristics based on System.console().

      Parameters:
      fd - the file descriptor (0=stdin, 1=stdout, 2=stderr)
      Returns:
      true if the file descriptor is connected to a terminal
    • isStdinTty

      public static boolean isStdinTty()
      Check if standard input is connected to a terminal. Result is cached after the first call.
      Returns:
      true if stdin is a terminal (interactive input)
    • isStdoutTty

      public static boolean isStdoutTty()
      Check if standard output is connected to a terminal. Result is cached after the first call.
      Returns:
      true if stdout is a terminal (not piped/redirected)
    • isStderrTty

      public static boolean isStderrTty()
      Check if standard error is connected to a terminal. Result is cached after the first call.
      Returns:
      true if stderr is a terminal (not piped/redirected)