Class: Tuile::Component::Label
- Inherits:
-
Component
- Object
- Component
- Tuile::Component::Label
- Defined in:
- lib/tuile/component/label.rb,
sig/tuile.rbs
Overview
A label which shows static text. No word-wrapping; long lines are truncated with an ellipsis. Text is modeled as a StyledString; #text= accepts a String (parsed via StyledString.parse, so embedded ANSI is honored) or a StyledString directly. #text always returns the StyledString.
Instance Attribute Summary collapse
-
#bg ⇒ Color, ...
@return — a local background laid over every span and the row padding (via StyledString#with_bg), overriding the text's own span bgs — stronger than the inherited #bg_color.
-
#text ⇒ StyledString, ...
@return — the current text.
Instance Method Summary collapse
-
#apply_bg(line) ⇒ StyledString
@param
line. -
#initialize(text = nil) ⇒ Label
constructor
@param
text— initial text, coerced the same way #text= coerces it (aStringis parsed via StyledString.parse;nilis an empty label). - #on_width_changed ⇒ void
-
#pad_to(line, width) ⇒ StyledString
@param
line. -
#repaint ⇒ void
Paints the text into #rect.
-
#update_clipped_lines ⇒ void
Recomputes @clipped_lines for the current text and rect width.
Constructor Details
#initialize(text = nil) ⇒ Label
@param text — initial text, coerced the same way #text= coerces it (a String is parsed via StyledString.parse; nil is an empty label). Equivalent to constructing empty and assigning #text=.
15 16 17 18 19 20 21 22 |
# File 'lib/tuile/component/label.rb', line 15 def initialize(text = nil) super() @text = StyledString::EMPTY @bg = nil @clipped_lines = [] @blank_line = "" self.text = text unless text.nil? end |
Instance Attribute Details
#bg ⇒ Color, ...
@return — a local background laid over every span and the
row padding (via StyledString#with_bg), overriding the text's own
span bgs — stronger than the inherited Tuile::Component#bg_color. nil (default)
keeps each span's bg and lets the inherited Tuile::Component#effective_bg_color
fill the rest.
33 34 35 |
# File 'lib/tuile/component/label.rb', line 33 def bg @bg end |
#text ⇒ StyledString, ...
@return — the current text. Defaults to an empty StyledString.
26 27 28 |
# File 'lib/tuile/component/label.rb', line 26 def text @text end |
Instance Method Details
#apply_bg(line) ⇒ StyledString
@param line
109 110 111 |
# File 'lib/tuile/component/label.rb', line 109 def apply_bg(line) @bg ? line.with_bg(@bg) : line end |
#on_width_changed ⇒ void
This method returns an undefined value.
87 88 89 90 |
# File 'lib/tuile/component/label.rb', line 87 def on_width_changed super update_clipped_lines end |
#pad_to(line, width) ⇒ StyledString
@param line
@param width
116 117 118 119 120 121 |
# File 'lib/tuile/component/label.rb', line 116 def pad_to(line, width) diff = width - line.display_width return line if diff <= 0 line + StyledString.plain(" " * diff) end |
#repaint ⇒ void
This method returns an undefined value.
Paints the text into Tuile::Component#rect.
Skips the Tuile::Component#repaint default's auto-clear: every row is painted explicitly (with pre-padded blanks past the last line), so the "fully draw over your rect" contract is met without an upfront wipe. Rows go through Tuile::Component#draw_line, so the padding and blank rows inherit Tuile::Component#effective_bg_color when #bg is unset.
75 76 77 78 79 80 81 82 |
# File 'lib/tuile/component/label.rb', line 75 def repaint return if rect.empty? (0...rect.height).each do |row| line = @clipped_lines[row] || @blank_line draw_line(rect.left, rect.top + row, line) end end |
#update_clipped_lines ⇒ void
This method returns an undefined value.
Recomputes @clipped_lines for the current text and rect width. Each line is ellipsized to fit and padded with trailing spaces out to the full width, so #repaint is just a lookup + Buffer#set_line per row. @blank_line covers rows past the last text line. When #bg is set, every produced line (and the blank row) has the bg applied uniformly.
101 102 103 104 105 |
# File 'lib/tuile/component/label.rb', line 101 def update_clipped_lines width = rect.width.clamp(0, nil) @blank_line = apply_bg(StyledString.plain(" " * width)) @clipped_lines = @text.lines.map { |line| apply_bg(pad_to(line.ellipsize(width), width)) } end |