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; clips long lines.
Instance Attribute Summary
Attributes inherited from Tuile::Component
Instance Method Summary collapse
- #content_size ⇒ Size
-
#initialize ⇒ Label
constructor
A new instance of Label.
- #repaint ⇒ void
- #text=(text) ⇒ void
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
Constructor Details
#initialize ⇒ Label
Returns a new instance of Label.
7 8 9 10 11 |
# File 'lib/tuile/component/label.rb', line 7 def initialize super @lines = [] @clipped_lines = [] end |
Instance Method Details
#content_size ⇒ Size
23 24 25 26 27 28 |
# File 'lib/tuile/component/label.rb', line 23 def content_size @content_size ||= begin width = @lines.map { |line| Unicode::DisplayWidth.of(Rainbow.uncolor(line)) }.max || 0 Size.new(width, @lines.size) end end |
#repaint ⇒ void
This method returns an undefined value.
31 32 33 34 35 36 37 38 |
# File 'lib/tuile/component/label.rb', line 31 def repaint clear_background height = rect.height.clamp(0, nil) lines_to_print = @clipped_lines.length.clamp(nil, height) (0..lines_to_print - 1).each do |index| screen.print TTY::Cursor.move_to(rect.left, rect.top + index), @clipped_lines[index] end end |
#text=(text) ⇒ void
This method returns an undefined value.
16 17 18 19 20 |
# File 'lib/tuile/component/label.rb', line 16 def text=(text) @lines = text.to_s.split("\n") @content_size = nil update_clipped_text end |