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; 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

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, #tab_stop?

Constructor Details

#initializeLabel

Returns a new instance of Label.



11
12
13
14
15
16
# File 'lib/tuile/component/label.rb', line 11

def initialize
  super
  @text = StyledString::EMPTY
  @clipped_lines = []
  @blank_line = ""
end

Instance Attribute Details

#textStyledString

Returns the current text. Defaults to an empty StyledString.

Returns:



20
21
22
# File 'lib/tuile/component/label.rb', line 20

def text
  @text
end

Instance Method Details

#content_sizeSize

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)`.

Returns:

  • (Size)

    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)`.



41
42
43
44
45
46
47
48
49
50
# File 'lib/tuile/component/label.rb', line 41

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

#repaintvoid

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.



59
60
61
62
63
64
65
66
# File 'lib/tuile/component/label.rb', line 59

def repaint
  return if rect.empty? || rect.left.negative? || rect.top.negative?

  (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