Skip to content

Hardens terminal background detection#31

Merged
aboo merged 1 commit into
mainfrom
feature/pap-7045-harden-terminal-background-detection
Apr 29, 2026
Merged

Hardens terminal background detection#31
aboo merged 1 commit into
mainfrom
feature/pap-7045-harden-terminal-background-detection

Conversation

@aboo
Copy link
Copy Markdown
Contributor

@aboo aboo commented Apr 29, 2026

What does this PR do?

Improves the robustness of terminal background mode detection by addressing several edge cases and potential failure points. This change aims to provide more reliable theme detection for the CLI.

  • Prevents detection in non-TTY environments: The function now immediately returns 'dark' if stdout or stdin are not TTYs, ensuring correct behavior in non-interactive contexts like CI/CD pipelines where querying the terminal might lead to hangs or incorrect results.
  • Enhances color format validation: Introduces strict validation for the detected background color, ensuring it matches a 6-digit hexadecimal format (#RRGGBB). Malformed or unsupported color formats (e.g., short hex #RGB, 8-digit hex #RRGGBBAA, or rgb() strings) will now correctly trigger a fallback to 'dark' mode, preventing potential parsing errors.
  • Adjusts detection timeouts: Increases the timeout for both OSC (Operating System Command) support and color detection queries from 150ms to 300ms. This provides more leeway for various terminal emulators to respond, reducing false negatives due to slow responses.
  • Adds comprehensive unit tests: The test suite has been significantly expanded to cover all new hardening conditions, including non-TTY scenarios, invalid color formats, OSC support failures, and a wider range of valid light and dark background colors.

Relates to PAP-7045

- Add TTY guard checking both stdout.isTTY and stdin.isTTY for non-TTY environments
- Replace hardcoded 150ms timeout with OSC_TIMEOUT_MS constant (300ms)
- Add regex validation for defaultBackground to reject malformed colour formats
- Add comprehensive test suite with 19 test cases covering all failure modes

Delivers PAP-7045
@aboo aboo merged commit eb476e6 into main Apr 29, 2026
1 check passed
@aboo aboo deleted the feature/pap-7045-harden-terminal-background-detection branch April 29, 2026 05:22
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.

1 participant