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
-
#text ⇒ StyledString, ...
@return — the current text.
Instance Method Summary collapse
-
#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). -
#relayout ⇒ void
Re-pads the rows to the new width; a height change costs a cheap redundant pass, which beats keying the cache.
-
#repaint(canvas) ⇒ void
Paints the text into #rect.
-
#update_rows ⇒ void
Recomputes @rows 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 |
# File 'lib/tuile/component/label.rb', line 15 def initialize(text = nil) super() @text = StyledString::EMPTY @rows = [] @blank_row = "" self.text = text unless text.nil? end |
Instance Attribute Details
#text ⇒ StyledString, ...
@return — the current text. Defaults to an empty StyledString.
25 26 27 |
# File 'lib/tuile/component/label.rb', line 25 def text @text end |
Instance Method Details
#relayout ⇒ void
This method returns an undefined value.
Re-pads the rows to the new width; a height change costs a cheap redundant pass, which beats keying the cache.
66 67 68 69 |
# File 'lib/tuile/component/label.rb', line 66 def relayout super update_rows end |
#repaint(canvas) ⇒ 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::Canvas#set_text, so the text, the trailing padding and the blank rows all take Tuile::Component#bg_color, and a span that carries its own background keeps it.
@param canvas — see Tuile::Component#repaint.
52 53 54 55 56 57 58 59 |
# File 'lib/tuile/component/label.rb', line 52 def repaint(canvas) return if rect.empty? (0...rect.height).each do |row| line = @rows[row] || @blank_row canvas.set_text(0, row, line) end end |
#update_rows ⇒ void
This method returns an undefined value.
Recomputes @rows 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_text per row. @blank_row covers rows past the last text line.
78 79 80 81 82 |
# File 'lib/tuile/component/label.rb', line 78 def update_rows width = rect.width.clamp(0, nil) @blank_row = StyledString.plain(" " * width) @rows = @text.lines.map { |line| line.ellipsize(width).ljust(width) } end |