Skip to content

Conversation

@shyam-ramani
Copy link

Enhanced Terminal Color Support

Overview

This PR adds a new terminal color support module that improves color handling across different terminal types and environments. The implementation provides robust color detection, fallback options, and better Windows support.

Features

Terminal Color Detection

  • Intelligent detection of terminal color support
  • Environment variable controls:
    • NO_COLOR: Disable colors
    • FORCE_COLOR: Force color output
  • Terminal type detection (dumb, unknown, etc.)
  • Windows-specific console mode detection

Color Support Management

  • Comprehensive ANSI color code mapping
  • Support for both standard and bright colors
  • Fallback color mapping for unsupported colors
  • Caching of color support status

Windows Support

  • Native Windows console detection
  • Proper handling of Windows terminal modes
  • Fallback options for legacy Windows terminals

Implementation Details

New Module: rich/terminal.py

class TerminalColorSupport:
    def detect_color_support(self) -> bool:
        """Detect if the terminal supports colors."""
        # Environment checks
        # Terminal type checks
        # Windows-specific checks
        
    def get_supported_colors(self) -> Set[str]:
        """Get the set of supported colors."""
        
    def get_fallback_color(self, color: str) -> str:
        """Get a fallback color if the requested color is not supported."""
        
    def get_color_code(self, color: str) -> Tuple[int, bool]:
        """Get the ANSI color code and whether it's supported."""

Test Coverage: tests/test_terminal_color.py

  • Color support detection tests
  • Supported colors tests
  • Fallback color mapping tests
  • Color code generation tests
  • Windows-specific tests

Benefits

  1. Improved Compatibility

    • Better support for different terminal types
    • Graceful fallbacks for unsupported colors
    • Enhanced Windows terminal support
  2. Better User Experience

    • Consistent color output across platforms
    • No broken color codes in unsupported terminals
    • Environment-aware color handling
  3. Developer Friendly

    • Easy to use API
    • Comprehensive test coverage
    • Well-documented code

Testing

The implementation includes comprehensive tests covering:

  • Environment variable handling
  • Terminal type detection
  • Color support detection
  • Fallback color mapping
  • Windows-specific functionality

Example Usage

from rich.terminal import terminal_color

# Check color support
if terminal_color.detect_color_support():
    # Get color code with fallback
    code, supported = terminal_color.get_color_code('bright_red')
    if supported:
        print(f"\033[{code}mBright Red Text\033[0m")

Impact

  • No breaking changes to existing code
  • Improved color handling across platforms
  • Better Windows terminal support
  • More reliable color output

Additional Notes

  • Follows Rich's coding style and patterns
  • Includes comprehensive documentation
  • Maintains backward compatibility
  • Easy to extend for additional color features

@willmcgugan
Copy link
Member

This doesn't fix anything.

@willmcgugan willmcgugan closed this Apr 2, 2025
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.

2 participants