Skip to content

Add selective reset for text attributes vs background #140

@Lopinosaurus

Description

@Lopinosaurus

Description:

Currently, Rainbow applies a full reset (\e[0m) after styling text, which clears all formatting including text color, styles (bold/italic), and background color. This makes it challenging to maintain a persistent background color while applying different text styles throughout a section.

Use case:

I'm building a syntax-highlighted diff viewer that displays additions and deletions with colored backgrounds (green/red). Within these colored backgrounds, I need to apply different text colors and styles to keywords, strings, comments, etc.

With the current implementation, each Rainbow-styled text resets the background, creating gaps in the background coloring.

Current behavior:

puts Rainbow('').bg(255, 0, 0)      # Set red background
puts Rainbow('keyword').green.bold   # Outputs: \e[32;1mkeyword\e[0m (resets background!)
puts Rainbow('string').cyan          # Background is gone

Desired Behavior

The ability to selectively reset only text attributes without affecting the background:

puts Rainbow('').bg(255, 0, 0)                    # Set red background
puts Rainbow('keyword').green.bold.reset(:text)   # Reset only text color/style
puts Rainbow('string').cyan.reset(:text)          # Background persists
puts Rainbow('').reset(:background)               # Explicitly reset background when done

Proposed API

Add a reset method with optional scope parameter:

Rainbow(text).color.reset(:text)         # Reset text color and styles only (\e[39;22;23;24m)
Rainbow(text).color.reset(:background)   # Reset background only (\e[49m)
Rainbow(text).color.reset(:all)          # Reset everything (\e[0m) - current behavior
Rainbow(text).color.reset               # Default to :all for backward compatibility

Or alternatively, provide methods to emit only the opening codes without automatic reset:

Rainbow.bg_code(255, 0, 0)  # Returns "\e[48;2;255;0;0m" without closing
Rainbow.reset_code(:text)   # Returns "\e[39;22;23;24m"

Thank you for maintaining this excellent library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions