Class: RubyTUI::Style
- Inherits:
-
Object
- Object
- RubyTUI::Style
- Defined in:
- lib/rubytui/style.rb
Overview
Composable text style with foreground/background colors and modifiers.
Style is immutable — builder methods return new Style instances. Use #patch to merge styles for overlay composition.
Constant Summary collapse
- DEFAULT =
Default style: no colors, no modifiers.
new.freeze
Instance Attribute Summary collapse
-
#modifiers ⇒ Integer
readonly
Modifier bitmask (see Modifier constants).
Modifier Builders collapse
-
#bold ⇒ Style
New Style with bold enabled.
-
#dim ⇒ Style
New Style with dim enabled.
-
#italic ⇒ Style
New Style with italic enabled.
-
#reversed ⇒ Style
New Style with reverse video enabled.
-
#strikethrough ⇒ Style
New Style with strikethrough enabled.
-
#underlined ⇒ Style
New Style with underline enabled.
Modifier Queries collapse
-
#bold? ⇒ Boolean
True if bold is enabled.
-
#dim? ⇒ Boolean
True if dim is enabled.
-
#italic? ⇒ Boolean
True if italic is enabled.
-
#reversed? ⇒ Boolean
True if reverse video is enabled.
-
#strikethrough? ⇒ Boolean
True if strikethrough is enabled.
-
#underlined? ⇒ Boolean
True if underline is enabled.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Value equality: same colors and modifiers.
-
#add_modifier(mod) ⇒ Style
Add a modifier flag.
-
#bg(color = UNSET) ⇒ Object
Get or set the background color.
-
#fg(color = UNSET) ⇒ Object
Get or set the foreground color.
-
#hash ⇒ Integer
Hash derived from foreground, background and modifiers.
-
#initialize(fg: nil, bg: nil, modifiers: Modifier::NONE) ⇒ Style
constructor
A new instance of Style.
-
#patch(other) ⇒ Style
Merge another style on top of this one.
-
#remove_modifier(mod) ⇒ Style
Remove a modifier flag.
Constructor Details
Instance Attribute Details
#modifiers ⇒ Integer (readonly)
Returns modifier bitmask (see Modifier constants).
18 19 20 |
# File 'lib/rubytui/style.rb', line 18 def modifiers @modifiers end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Value equality: same colors and modifiers.
132 133 134 |
# File 'lib/rubytui/style.rb', line 132 def ==(other) other.is_a?(Style) && @fg == other.fg && @bg == other.bg && @modifiers == other.modifiers end |
#add_modifier(mod) ⇒ Style
Add a modifier flag.
100 101 102 |
# File 'lib/rubytui/style.rb', line 100 def add_modifier(mod) Style.new(fg: @fg, bg: @bg, modifiers: @modifiers | mod) end |
#bg ⇒ Color? #bg(color) ⇒ Style
Get or set the background color.
55 56 57 58 59 |
# File 'lib/rubytui/style.rb', line 55 def bg(color = UNSET) return @bg if color.equal?(UNSET) Style.new(fg: @fg, bg: color, modifiers: @modifiers) end |
#bold ⇒ Style
Returns new Style with bold enabled.
65 66 |
# File 'lib/rubytui/style.rb', line 65 def bold = add_modifier(Modifier::BOLD) # @return [Style] new Style with dim enabled |
#bold? ⇒ Boolean
Returns true if bold is enabled.
82 83 |
# File 'lib/rubytui/style.rb', line 82 def bold? = Modifier.contains?(@modifiers, Modifier::BOLD) # @return [Boolean] true if dim is enabled |
#dim ⇒ Style
Returns new Style with dim enabled.
67 68 |
# File 'lib/rubytui/style.rb', line 67 def dim = add_modifier(Modifier::DIM) # @return [Style] new Style with italic enabled |
#dim? ⇒ Boolean
Returns true if dim is enabled.
84 85 |
# File 'lib/rubytui/style.rb', line 84 def dim? = Modifier.contains?(@modifiers, Modifier::DIM) # @return [Boolean] true if italic is enabled |
#fg ⇒ Color? #fg(color) ⇒ Style
Get or set the foreground color.
41 42 43 44 45 |
# File 'lib/rubytui/style.rb', line 41 def fg(color = UNSET) return @fg if color.equal?(UNSET) Style.new(fg: color, bg: @bg, modifiers: @modifiers) end |
#hash ⇒ Integer
Returns hash derived from foreground, background and modifiers.
138 |
# File 'lib/rubytui/style.rb', line 138 def hash = [@fg, @bg, @modifiers].hash |
#italic ⇒ Style
Returns new Style with italic enabled.
69 70 |
# File 'lib/rubytui/style.rb', line 69 def italic = add_modifier(Modifier::ITALIC) # @return [Style] new Style with underline enabled |
#italic? ⇒ Boolean
Returns true if italic is enabled.
86 87 |
# File 'lib/rubytui/style.rb', line 86 def italic? = Modifier.contains?(@modifiers, Modifier::ITALIC) # @return [Boolean] true if underline is enabled |
#patch(other) ⇒ Style
Merge another style on top of this one.
The other style's non-nil colors override this style's colors. Modifiers are OR'd together (additive).
119 120 121 122 123 124 125 |
# File 'lib/rubytui/style.rb', line 119 def patch(other) Style.new( fg: other.fg || @fg, bg: other.bg || @bg, modifiers: @modifiers | other.modifiers ) end |
#remove_modifier(mod) ⇒ Style
Remove a modifier flag.
108 109 110 |
# File 'lib/rubytui/style.rb', line 108 def remove_modifier(mod) Style.new(fg: @fg, bg: @bg, modifiers: @modifiers & ~mod) end |
#reversed ⇒ Style
Returns new Style with reverse video enabled.
73 74 |
# File 'lib/rubytui/style.rb', line 73 def reversed = add_modifier(Modifier::REVERSED) # @return [Style] new Style with strikethrough enabled |
#reversed? ⇒ Boolean
Returns true if reverse video is enabled.
90 91 |
# File 'lib/rubytui/style.rb', line 90 def reversed? = Modifier.contains?(@modifiers, Modifier::REVERSED) # @return [Boolean] true if strikethrough is enabled |
#strikethrough ⇒ Style
Returns new Style with strikethrough enabled.
75 |
# File 'lib/rubytui/style.rb', line 75 def strikethrough = add_modifier(Modifier::STRIKETHROUGH) |
#strikethrough? ⇒ Boolean
Returns true if strikethrough is enabled.
92 |
# File 'lib/rubytui/style.rb', line 92 def strikethrough? = Modifier.contains?(@modifiers, Modifier::STRIKETHROUGH) |
#underlined ⇒ Style
Returns new Style with underline enabled.
71 72 |
# File 'lib/rubytui/style.rb', line 71 def underlined = add_modifier(Modifier::UNDERLINED) # @return [Style] new Style with reverse video enabled |
#underlined? ⇒ Boolean
Returns true if underline is enabled.
88 89 |
# File 'lib/rubytui/style.rb', line 88 def underlined? = Modifier.contains?(@modifiers, Modifier::UNDERLINED) # @return [Boolean] true if reverse video is enabled |