Class: Tuile::Component::Label

Inherits:
Tuile::Component show all
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

#key_shortcut, #parent, #rect

Instance Method Summary collapse

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

#initializeLabel

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_sizeSize

Returns:



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

#repaintvoid

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.

Parameters:

  • text (String, nil)

    draws this text. May contain ANSI formatting. Clipped automatically.



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