Class: Tuile::StyledString::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/tuile/styled_string.rb

Overview

A frozen value type describing the visual style of a Span. Colors are stored as Color instances (or ‘nil` for the terminal default); inputs to Style.new and #merge are coerced via Color.coerce, so the four accepted color forms — `nil`, Symbol, Integer 0..255, RGB Array — work transparently.

Constant Summary collapse

DEFAULT =

The style with no color and no attributes — what the terminal shows without any SGR applied.

Returns:

new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bgColor? (readonly)

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tuile/styled_string.rb', line 72

class Style < Data.define(:fg, :bg, :bold, :italic, :underline)
  # @param fg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bold [Boolean]
  # @param italic [Boolean]
  # @param underline [Boolean]
  # @return [Style]
  # @raise [ArgumentError] when a color is not one of the accepted forms.
  def self.new(fg: nil, bg: nil, bold: false, italic: false, underline: false)
    super(fg: Color.coerce(fg), bg: Color.coerce(bg), bold:, italic:, underline:)
  end

  # The style with no color and no attributes — what the terminal shows
  # without any SGR applied.
  # @return [Style]
  DEFAULT = new

  # @return [Boolean]
  def default? = self == DEFAULT

  # Returns a new {Style} with the given attributes overridden.
  # @param overrides [Hash{Symbol => Object}]
  # @return [Style]
  def merge(**overrides) = self.class.new(**to_h.merge(overrides))
end

#boldBoolean (readonly)

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tuile/styled_string.rb', line 72

class Style < Data.define(:fg, :bg, :bold, :italic, :underline)
  # @param fg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bold [Boolean]
  # @param italic [Boolean]
  # @param underline [Boolean]
  # @return [Style]
  # @raise [ArgumentError] when a color is not one of the accepted forms.
  def self.new(fg: nil, bg: nil, bold: false, italic: false, underline: false)
    super(fg: Color.coerce(fg), bg: Color.coerce(bg), bold:, italic:, underline:)
  end

  # The style with no color and no attributes — what the terminal shows
  # without any SGR applied.
  # @return [Style]
  DEFAULT = new

  # @return [Boolean]
  def default? = self == DEFAULT

  # Returns a new {Style} with the given attributes overridden.
  # @param overrides [Hash{Symbol => Object}]
  # @return [Style]
  def merge(**overrides) = self.class.new(**to_h.merge(overrides))
end

#fgColor? (readonly)

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tuile/styled_string.rb', line 72

class Style < Data.define(:fg, :bg, :bold, :italic, :underline)
  # @param fg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bold [Boolean]
  # @param italic [Boolean]
  # @param underline [Boolean]
  # @return [Style]
  # @raise [ArgumentError] when a color is not one of the accepted forms.
  def self.new(fg: nil, bg: nil, bold: false, italic: false, underline: false)
    super(fg: Color.coerce(fg), bg: Color.coerce(bg), bold:, italic:, underline:)
  end

  # The style with no color and no attributes — what the terminal shows
  # without any SGR applied.
  # @return [Style]
  DEFAULT = new

  # @return [Boolean]
  def default? = self == DEFAULT

  # Returns a new {Style} with the given attributes overridden.
  # @param overrides [Hash{Symbol => Object}]
  # @return [Style]
  def merge(**overrides) = self.class.new(**to_h.merge(overrides))
end

#italicBoolean (readonly)

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tuile/styled_string.rb', line 72

class Style < Data.define(:fg, :bg, :bold, :italic, :underline)
  # @param fg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bold [Boolean]
  # @param italic [Boolean]
  # @param underline [Boolean]
  # @return [Style]
  # @raise [ArgumentError] when a color is not one of the accepted forms.
  def self.new(fg: nil, bg: nil, bold: false, italic: false, underline: false)
    super(fg: Color.coerce(fg), bg: Color.coerce(bg), bold:, italic:, underline:)
  end

  # The style with no color and no attributes — what the terminal shows
  # without any SGR applied.
  # @return [Style]
  DEFAULT = new

  # @return [Boolean]
  def default? = self == DEFAULT

  # Returns a new {Style} with the given attributes overridden.
  # @param overrides [Hash{Symbol => Object}]
  # @return [Style]
  def merge(**overrides) = self.class.new(**to_h.merge(overrides))
end

#underlineBoolean (readonly)

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tuile/styled_string.rb', line 72

class Style < Data.define(:fg, :bg, :bold, :italic, :underline)
  # @param fg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bg [Color, Symbol, Integer, Array<Integer>, nil] coerced via {Color.coerce}.
  # @param bold [Boolean]
  # @param italic [Boolean]
  # @param underline [Boolean]
  # @return [Style]
  # @raise [ArgumentError] when a color is not one of the accepted forms.
  def self.new(fg: nil, bg: nil, bold: false, italic: false, underline: false)
    super(fg: Color.coerce(fg), bg: Color.coerce(bg), bold:, italic:, underline:)
  end

  # The style with no color and no attributes — what the terminal shows
  # without any SGR applied.
  # @return [Style]
  DEFAULT = new

  # @return [Boolean]
  def default? = self == DEFAULT

  # Returns a new {Style} with the given attributes overridden.
  # @param overrides [Hash{Symbol => Object}]
  # @return [Style]
  def merge(**overrides) = self.class.new(**to_h.merge(overrides))
end

Class Method Details

.new(fg: nil, bg: nil, bold: false, italic: false, underline: false) ⇒ Style

Parameters:

  • fg (Color, Symbol, Integer, Array<Integer>, nil) (defaults to: nil)

    coerced via Color.coerce.

  • bg (Color, Symbol, Integer, Array<Integer>, nil) (defaults to: nil)

    coerced via Color.coerce.

  • bold (Boolean) (defaults to: false)
  • italic (Boolean) (defaults to: false)
  • underline (Boolean) (defaults to: false)

Returns:

Raises:

  • (ArgumentError)

    when a color is not one of the accepted forms.



80
81
82
# File 'lib/tuile/styled_string.rb', line 80

def self.new(fg: nil, bg: nil, bold: false, italic: false, underline: false)
  super(fg: Color.coerce(fg), bg: Color.coerce(bg), bold:, italic:, underline:)
end

Instance Method Details

#default?Boolean

Returns:

  • (Boolean)


90
# File 'lib/tuile/styled_string.rb', line 90

def default? = self == DEFAULT

#merge(**overrides) ⇒ Style

Returns a new Tuile::StyledString::Style with the given attributes overridden.

Parameters:

  • overrides (Hash{Symbol => Object})

Returns:



95
# File 'lib/tuile/styled_string.rb', line 95

def merge(**overrides) = self.class.new(**to_h.merge(overrides))