Class: RubyTUI::Style

Inherits:
Object
  • Object
show all
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.

Examples:

Builder pattern

style = Style.new.fg(Color::RED).bold.italic

Merge styles

base = Style.new.fg(Color::WHITE)
highlight = Style.new.bold.underlined
combined = base.patch(highlight)  # white, bold, underlined

Constant Summary collapse

DEFAULT =

Default style: no colors, no modifiers.

new.freeze

Instance Attribute Summary collapse

Modifier Builders collapse

Modifier Queries collapse

Instance Method Summary collapse

Constructor Details

#initialize(fg: nil, bg: nil, modifiers: Modifier::NONE) ⇒ Style

Returns a new instance of Style.

Parameters:

  • fg (Color, nil) (defaults to: nil) —

    foreground color

  • bg (Color, nil) (defaults to: nil) —

    background color

  • modifiers (Integer) (defaults to: Modifier::NONE) —

    modifier bitmask (default: Modifier::NONE)



27
28
29
30
31
# File 'lib/rubytui/style.rb', line 27

def initialize(fg: nil, bg: nil, modifiers: Modifier::NONE)
  @fg = fg
  @bg = bg
  @modifiers = modifiers
end

Instance Attribute Details

#modifiers ⇒ Integer (readonly)

Returns modifier bitmask (see Modifier constants).

Returns:

  • (Integer) —

    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.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean) —

    true if other is a Style with equal foreground, background 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.

Parameters:

  • mod (Integer) —

    modifier constant from Modifier

Returns:

  • (Style) —

    new Style with the flag added



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.

Overloads:

  • #bg ⇒ Color?

    Returns current background color.

    Returns:

    • (Color, nil) —

      current background color

  • #bg(color) ⇒ Style

    Returns new Style with the given background color.

    Parameters:

    • color (Color) —

      new background color

    Returns:

    • (Style) —

      new Style with the given 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.

Returns:

  • (Style) —

    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.

Returns:

  • (Boolean) —

    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.

Returns:

  • (Style) —

    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.

Returns:

  • (Boolean) —

    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.

Overloads:

  • #fg ⇒ Color?

    Returns current foreground color.

    Returns:

    • (Color, nil) —

      current foreground color

  • #fg(color) ⇒ Style

    Returns new Style with the given foreground color.

    Parameters:

    • color (Color) —

      new foreground color

    Returns:

    • (Style) —

      new Style with the given 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.

Returns:

  • (Integer) —

    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.

Returns:

  • (Style) —

    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.

Returns:

  • (Boolean) —

    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).

Parameters:

  • other (Style) —

    style to merge on top

Returns:

  • (Style) —

    merged style



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.

Parameters:

  • mod (Integer) —

    modifier constant from Modifier

Returns:

  • (Style) —

    new Style with the flag removed



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.

Returns:

  • (Style) —

    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.

Returns:

  • (Boolean) —

    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.

Returns:

  • (Style) —

    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.

Returns:

  • (Boolean) —

    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.

Returns:

  • (Style) —

    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.

Returns:

  • (Boolean) —

    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