Skip to content

feat(runner): ANSI colors on by default with TTY auto-detection#53

Merged
usernane merged 2 commits into
devfrom
feat/ansi-auto-detect
Jun 13, 2026
Merged

feat(runner): ANSI colors on by default with TTY auto-detection#53
usernane merged 2 commits into
devfrom
feat/ansi-auto-detect

Conversation

@usernane

Copy link
Copy Markdown
Member

Summary

Enable ANSI colored output by default when stdout is a TTY, with explicit flags to override.

Motivation

Colors were disabled by default, requiring --ansi every time. This contradicts CLI conventions — modern tools auto-detect terminal capabilities. Closes #50.

Changes

  • Added shouldUseAnsi() static method with TTY detection (posix_isatty(STDOUT))
  • Added --no-color global argument (respects no-color.org convention)
  • Added isAnsi() public method to Runner
  • Added isAnsiEnabled() private method to Command (uses Runner value, falls back to arg check)
  • Added resolveAnsi() private method — only auto-enables when output is StdOut
  • Added removeAnsiArgs() helper to strip --ansi/--no-color from arg arrays
  • Updated run(), interactive mode, and runCommand() to handle --no-color
  • Updated existing tests to account for new --no-color global argument in help output

Resolution precedence:

  1. --no-color flag → OFF
  2. NO_COLOR env variable → OFF
  3. --ansi flag → ON (force in piped contexts)
  4. TTY auto-detection → ON if terminal, OFF if piped/array stream

Platform support:

  • Unix: posix_isatty(STDOUT)
  • Windows: ANSICON/ConEmuANSI/TERM env fallback
  • Test mode (ArrayOutputStream): always OFF (no false positives)

How to Test / Verify

Unit tests with full coverage on reachable code paths:

  • tests/WebFiori/Tests/Cli/AnsiAutoDetectTest.php (18 tests)
  • Updated RunnerTest.php for new global arg

Run: composer test

Breaking Changes and Migration Steps

Behavioral: terminal output that was previously uncolored now has colors by default. Users who want no colors use --no-color or NO_COLOR=1.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed) Docs Repo
  • I ran lint/cs-fixer (if applicable) (composer fix-cs)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #50

- Add shouldUseAnsi() static method with TTY detection via posix_isatty
- Add --no-color global argument (respects no-color.org convention)
- Add isAnsi() public method to Runner
- Resolution precedence: --no-color > NO_COLOR env > --ansi > TTY detect
- Command println/prints now use Runner's resolved ANSI value
- Graceful Windows fallback via ANSICON/ConEmuANSI/TERM env checks
- ANSI only auto-enables when output stream is StdOut (not in tests)

Closes #50
@codecov

codecov Bot commented Jun 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.79592% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.95%. Comparing base (f256870) to head (a4cd174).

Files with missing lines Patch % Lines
WebFiori/Cli/Runner.php 88.09% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                dev      #53      +/-   ##
============================================
- Coverage     87.97%   87.95%   -0.03%     
- Complexity     1284     1305      +21     
============================================
  Files            34       34              
  Lines          3693     3727      +34     
============================================
+ Hits           3249     3278      +29     
- Misses          444      449       +5     
Flag Coverage Δ
php-8.3 87.95% <89.79%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add hidden: false to all changelog-sections so commits like
fix(ci), test:, docs:, refactor:, etc. appear in the changelog.
@sonarqubecloud

Copy link
Copy Markdown

@usernane usernane merged commit 32eddbb into dev Jun 13, 2026
8 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.

1 participant