Skip to content

Add POSIX isatty() checks for stdin/stdout redirection detection - #267

Merged
stalep merged 2 commits into
aeshell:masterfrom
stalep:posix-tty-detection
Sep 1, 2026
Merged

Add POSIX isatty() checks for stdin/stdout redirection detection#267
stalep merged 2 commits into
aeshell:masterfrom
stalep:posix-tty-detection

Conversation

@stalep

@stalep stalep commented Sep 1, 2026

Copy link
Copy Markdown
Member

TerminalBuilder.isSystemTerminal(): add TtyDetect.isStdinTty() check. When stdin is not a TTY (redirected from file or piped), go directly to ExternalTerminal without trying (and failing at) system terminal providers. This follows standard POSIX convention where programs check isatty(STDIN_FILENO) to decide whether to run interactively.

Previously, redirected stdin caused all SPI providers to be tried and fail (ExecPty forks 'tty' which returns 'not a tty', FfmPty calls isatty(0) which returns false), wasting ~8ms on subprocess calls before falling back to ExternalTerminal. Now the fallback is immediate.

TerminalConnection: add TtyDetect.isStdoutTty() check for ANSI output suppression. When stdout is not a TTY (redirected to file or piped to another program), set ansi=false to prevent ANSI escape codes from being written to the redirected output. This follows standard POSIX convention where programs check isatty(STDOUT_FILENO) to decide whether to emit escape sequences (like ls --color=auto, grep --color=auto).

This is independent of the stdin check — stdout can be redirected while stdin is still interactive (e.g., 'cmd | tee log.txt' where the user types interactively but output is also captured).

fixes #266

TerminalBuilder.isSystemTerminal(): add TtyDetect.isStdinTty() check.
When stdin is not a TTY (redirected from file or piped), go directly
to ExternalTerminal without trying (and failing at) system terminal
providers. This follows standard POSIX convention where programs
check isatty(STDIN_FILENO) to decide whether to run interactively.

Previously, redirected stdin caused all SPI providers to be tried
and fail (ExecPty forks 'tty' which returns 'not a tty', FfmPty
calls isatty(0) which returns false), wasting ~8ms on subprocess
calls before falling back to ExternalTerminal. Now the fallback is
immediate.

TerminalConnection: add TtyDetect.isStdoutTty() check for ANSI
output suppression. When stdout is not a TTY (redirected to file
or piped to another program), set ansi=false to prevent ANSI escape
codes from being written to the redirected output. This follows
standard POSIX convention where programs check isatty(STDOUT_FILENO)
to decide whether to emit escape sequences (like ls --color=auto,
grep --color=auto).

TtyDetect.isTty(): reorder detection to try Console.isTerminal()
(Java 22+) before FFM isatty(). Console.isTerminal() does not
require loading LibC or creating FFM downcall handles, avoiding
the JVM restricted method warning when --enable-native-access is
not set. FFM isatty() is only tried as a secondary path when
Console.isTerminal() is not available (pre-Java 22). Remove the
now-unused fallbackIsTty() method.

Fixes aeshell#266
@stalep
stalep force-pushed the posix-tty-detection branch from 1d3aece to 2027f30 Compare September 1, 2026 09:52
@tristantarrant

Copy link
Copy Markdown
Contributor

I have some tests I can add to this. I'll push a commit shortly

@stalep
stalep merged commit ea3bfe6 into aeshell:master Sep 1, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add POSIX isatty() checks for stdin/stdout redirection detection

2 participants