Class TtyDetect
java.lang.Object
org.aesh.terminal.tty.TtyDetect
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanCheck if standard error is connected to a terminal.static booleanCheck if standard input is connected to a terminal.static booleanCheck if standard output is connected to a terminal.static booleanisTty(int fd) Check if the given file descriptor is connected to a terminal.
-
Field Details
-
FD_STDIN
public static final int FD_STDINFile descriptor for standard input.- See Also:
-
FD_STDOUT
public static final int FD_STDOUTFile descriptor for standard output.- See Also:
-
FD_STDERR
public static final int FD_STDERRFile 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 onSystem.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)
-