Class: Tuile::Component::Label

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

Instance Method Summary collapse

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=.

Parameters:



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

#textStyledString, ...

@return — the current text. Defaults to an empty StyledString.

Returns:



25
26
27
# File 'lib/tuile/component/label.rb', line 25

def text
  @text
end

Instance Method Details

#handle_width_changedvoid

This method returns an undefined value.



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

def handle_width_changed
  super
  update_rows
end

#pad_to(line, width) ⇒ StyledString

@param line

@param width

Parameters:

Returns:



84
85
86
87
88
89
# File 'lib/tuile/component/label.rb', line 84

def pad_to(line, width)
  diff = width - line.display_width
  return line if diff <= 0

  line + StyledString.plain(" " * diff)
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. Rows go through Tuile::Component#draw_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.



51
52
53
54
55
56
57
58
# File 'lib/tuile/component/label.rb', line 51

def repaint
  return if rect.empty?

  (0...rect.height).each do |row|
    line = @rows[row] || @blank_row
    draw_text(rect.left, rect.top + row, line)
  end
end

#update_rowsvoid

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.



75
76
77
78
79
# File 'lib/tuile/component/label.rb', line 75

def update_rows
  width = rect.width.clamp(0, nil)
  @blank_row = StyledString.plain(" " * width)
  @rows = @text.lines.map { |line| pad_to(line.ellipsize(width), width) }
end