fix: gracefully handle non-TTY environments#1562
Open
nazt wants to merge 1 commit intonvbn:masterfrom
Open
Conversation
When thefuck runs in a non-TTY context (e.g. Claude Code, CI pipelines, editor integrations), termios.tcgetattr() crashes because there is no terminal to query. This change: - Adds TTY detection in ui.select_command() to auto-select the first suggestion when stdin is not a terminal, matching --yeah behavior - Hardens getch() to catch termios.error and return a newline (auto-select) as a safety net if the interactive path is somehow reached without a TTY Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getch()to catchtermios.errorinstead of crashing in non-terminal contexts--yeahbehavior — no new flags or config neededProblem
When
thefuckruns in a non-TTY context (e.g. piped through$()in a subprocess, CI pipelines, AI coding assistants),termios.tcgetattr()ingetch()crashes with:This happens because the interactive key-reading path is reached even when there's no terminal to read from.
Changes
thefuck/ui.py: Added_is_interactive()TTY check inselect_command(). When no TTY is detected, auto-selects the first suggestion (same behavior as--yeah)thefuck/system/unix.py: Hardenedgetch()to catchtermios.errorand return'\n'(auto-select) as a defense-in-depth fallbackTest plan
_is_interactive()returnsFalsein subprocess contextgetch()returns'\n'instead of crashing without TTYthefuckflow works in non-TTY (auto-selects first suggestion)🤖 Generated with Claude Code