This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Rainbow is a Swift library that adds text color, background color and style for console and command line output. It is designed for cross-platform software logging in terminals, working on Apple platforms, Linux, and Windows.
# Build the project
swift build -v
# Run all tests
swift test -v
# Run tests with test discovery (Linux)
swift test -v --enable-test-discovery
# Run a specific test
swift test --filter TestClassName.testMethodName
# Build in release mode
swift build -c release
# Generate Xcode project (if needed)
swift package generate-xcodeproj- Update
CHANGELOG.mdunder the[Unreleased]section with the release notes. - Ensure a clean git status on
master(no uncommitted changes). - Run the release lane with the target version:
bundle exec fastlane release version:4.2.1The release lane runs tests, bumps build/version, updates the podspec, stamps the changelog, commits, tags, pushes, creates a GitHub release via gh, and runs pod push.
Note: CHANGELOG.md uses compare links and keeps link references ordered from oldest to newest (latest at the bottom) so stamp_changelog can append the next compare link. The lane also refreshes the [Unreleased] compare link.
-
Rainbow.swift - Central configuration and control
- Contains
OutputTargetenum (console, unknown) that determines whether to apply colors - Manages global settings like
enabledflag and environment variable handling (NO_COLOR,FORCE_COLOR) - Provides core string generation logic via
generateString(for:)method
- Contains
-
String+Rainbow.swift - Main user-facing API
- All color/style properties are lazy computed properties that create new strings
- Chain calls (e.g.,
"text".red.bold) create multiple string copies - performance consideration - Contains
applyingCodesmethod that handles the actual ANSI escape sequence application
-
Parser System
ConsoleEntryParser- Parses existing colored strings into segmentsModesExtractor- Extracts ANSI codes from strings (safe with natural iteration termination)- Used for the
rawproperty that strips existing colors
-
Color System
ColorTypeenum: supports named colors (.red), 8-bit colors, and 24-bit RGB colorsBackgroundColorTypeenum: parallel structure for background colorsColorApproximation- Converts hex colors to nearest 8-bit or 24-bit color
-
Lazy Evaluation Problem: Each property access in chains creates a new string. Consider implementing a builder pattern for better performance.
-
ANSI Escape Sequences: All colors/styles work by wrapping text in ANSI codes:
- Format:
\u{001B}[<codes>m<text>\u{001B}[0m - Multiple codes separated by semicolons
- Format:
-
Environment Detection:
- Automatically disables colors when output is redirected to file
- Respects
NO_COLORandFORCE_COLORenvironment variables - Uses
isatty()to detect TTY output
-
Performance: String extensions create new strings on each call. For performance-critical code, use
Rainbow.generateString()directly with pre-built entries. -
Thread Safety: No explicit thread safety mechanisms. String operations are generally safe but global settings (Rainbow.enabled, Rainbow.outputTarget) may need synchronization in concurrent contexts.
-
Platform Differences:
- Windows support exists but may need additional terminal detection
- Some styles may not work on all terminals
-
Code Duplication: The
hexmethod implementation is duplicated for foreground and background colors in String+Rainbow.swift.
Tests are organized by functionality:
RainbowTests.swift- Core functionality and mode extractionConsoleStringTests.swift- String extension API testsColorTests.swift- Color conversion testsColorApproximatedTests.swift- Hex color approximation testsConsoleTextParserTests.swift- Parser tests
Note: Missing tests for strikethrough style and Windows-specific behavior.
When adding new colors or styles:
- Add to appropriate enum (Color, BackgroundColor, or Style)
- Update ANSI code mappings
- Add string extension property
- Add tests for the new feature
- Update README with examples
When fixing parser issues:
- Check
ModesExtractor.swiftfor the core parsing logic - Be aware of the infinite loop risk in
parseTextmethod - Test with malformed ANSI sequences