Skip to content

Add comprehensive conditional styling feature#81

Merged
onevcat merged 2 commits intomasterfrom
feature/conditional-styling
Jul 18, 2025
Merged

Add comprehensive conditional styling feature#81
onevcat merged 2 commits intomasterfrom
feature/conditional-styling

Conversation

@onevcat
Copy link
Owner

@onevcat onevcat commented Jul 18, 2025

Summary

This PR introduces a powerful conditional styling system for the Rainbow library that allows developers to apply colors and styles based on runtime conditions, significantly improving code readability and reducing the need for ternary operators in styling logic.

Key Features

🎯 Basic Conditional APIs

  • colorIf(_:_:) - Apply colors conditionally
  • backgroundColorIf(_:_:) - Apply background colors conditionally
  • styleIf(_:_:) - Apply styles conditionally
  • stylesIf(_:_:) - Apply multiple styles conditionally
  • applyIf(_:transform:) - Apply custom transformations conditionally

🚀 Advanced Conditional Builder

  • ConditionalStyleBuilder - Fluent interface for complex conditional styling
  • ConditionalStyleStep - Chain multiple conditional styles
  • Convenience properties: .red, .bold, .underline, etc.
  • Support for closure-based predicates

📝 Usage Examples

Basic Conditional Styling:

let errorMessage = "Error occurred"
    .colorIf(isError, .red)
    .styleIf(isError, .bold)

let logMessage = message
    .colorIf(level == .error, .red)
    .colorIf(level == .warning, .yellow)
    .colorIf(level == .info, .cyan)

Advanced Conditional Builder:

let styledText = "Status: Active"
    .conditionalStyled
    .when(isActive).green.bold
    .when(isWarning).yellow
    .when(isError).red.underline
    .build()

let dynamicStyling = "Processing..."
    .conditionalStyled
    .when { progress < 50 }.yellow.italic
    .when { progress >= 50 && progress < 100 }.blue.bold
    .when { progress == 100 }.green.bold.underline
    .build()

🔧 Implementation Details

  • String+ConditionalStyling.swift: Core conditional styling extensions with comprehensive API
  • ConditionalStyleBuilder.swift: Advanced builder pattern implementation with fluent interface
  • ConditionalStylingTests.swift: Comprehensive test suite with 32 new tests
  • Optimized ANSI Generation: Produces more efficient ANSI escape sequences (e.g., \u{001B}[31;1m instead of \u{001B}[1m\u{001B}[31m)

✅ Quality Assurance

  • Complete Test Coverage: 32 new tests covering all conditional styling scenarios
  • Backward Compatibility: All existing APIs remain unchanged
  • Performance Optimized: Efficient ANSI code generation
  • No Regressions: All 128 tests pass successfully
  • Rainbow Disabled Support: Proper handling when Rainbow.enabled = false

🎨 Design Philosophy

The conditional styling system follows a SwiftUI-inspired fluent interface design, making it intuitive for developers familiar with modern Swift APIs. The implementation prioritizes:

  1. Readability: Clear, expressive syntax that reduces cognitive load
  2. Flexibility: Support for both simple conditions and complex builder patterns
  3. Performance: Optimized ANSI escape sequence generation
  4. Compatibility: Seamless integration with existing Rainbow APIs

📊 Impact

This feature addresses a common pain point in terminal styling where developers often resort to verbose ternary operators or if-else statements to apply conditional styling. The new APIs provide a clean, chainable interface that makes styling logic more declarative and maintainable.

Test Plan

  • All existing tests pass (128 total tests)
  • 32 new comprehensive tests for conditional styling
  • Edge cases covered (empty strings, disabled Rainbow, existing styles)
  • Performance validation with optimized ANSI generation
  • Backward compatibility verified
  • Builder pattern functionality validated
  • Closure-based predicates tested

Breaking Changes

None - this is a purely additive feature that maintains full backward compatibility.

onevcat added 2 commits July 18, 2025 21:38
Add new source files to Xcode project:
- Sources/ConditionalStyleBuilder.swift
- Sources/String+ConditionalStyling.swift
- Tests/RainbowTests/ConditionalStylingTests.swift

This ensures proper integration with Xcode IDE and project management.
Document the new conditional styling feature with practical examples:

- Basic conditional styling with colorIf, styleIf
- Log level styling patterns
- Advanced conditional builder with fluent interface
- Closure-based conditional styling
- Progress-based styling examples

The examples showcase both simple and complex use cases, demonstrating
how conditional styling improves code readability and reduces the need
for ternary operators in styling logic.
@onevcat onevcat merged commit fd9683b into master Jul 18, 2025
2 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