Class: Tuile::Component::Label
- Inherits:
-
Tuile::Component
- Object
- Tuile::Component
- Tuile::Component::Label
- Defined in:
- lib/tuile/component/label.rb
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?
Background color applied uniformly across every painted row (including padding past the text).
-
#text ⇒ StyledString
The current text.
Attributes inherited from Tuile::Component
Instance Method Summary collapse
-
#content_size ⇒ Size
Longest hard-line’s display width × number of hard lines.
-
#initialize ⇒ Label
constructor
A new instance of Label.
-
#repaint ⇒ void
Paints the text into #rect.
Methods inherited from Tuile::Component
#active=, #active?, #attached?, #children, #cursor_position, #depth, #find_shortcut_component, #focus, #focusable?, #handle_key, #handle_mouse, #keyboard_hint, #on_child_removed, #on_focus, #on_tree, #root, #screen, #tab_stop?
Constructor Details
#initialize ⇒ Label
Returns a new instance of Label.
11 12 13 14 15 16 17 |
# File 'lib/tuile/component/label.rb', line 11 def initialize super @text = StyledString::EMPTY @bg = nil @clipped_lines = [] @blank_line = "" end |
Instance Attribute Details
#bg ⇒ Color?
Returns background color applied uniformly across every painted row (including padding past the text). ‘nil` (default) leaves whatever bg the text’s own styling carries.
26 27 28 |
# File 'lib/tuile/component/label.rb', line 26 def bg @bg end |
#text ⇒ StyledString
Returns the current text. Defaults to an empty StyledString.
21 22 23 |
# File 'lib/tuile/component/label.rb', line 21 def text @text end |
Instance Method Details
#content_size ⇒ Size
Returns longest hard-line’s display width × number of hard lines. Reported on the unclipped text — sizing is intrinsic to the content, not the viewport. Empty text returns ‘Size.new(0, 0)`.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tuile/component/label.rb', line 64 def content_size @content_size ||= if @text.empty? Size::ZERO else hard_lines = @text.lines width = hard_lines.map(&:display_width).max || 0 Size.new(width, hard_lines.size) end 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.
82 83 84 85 86 87 88 89 |
# File 'lib/tuile/component/label.rb', line 82 def repaint return if rect.empty? (0...rect.height).each do |row| line = @clipped_lines[row] || @blank_line screen.print TTY::Cursor.move_to(rect.left, rect.top + row), line end end |