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
22
# File 'lib/tuile/component/label.rb', line 15

def initialize(text = nil)
  super()
  @text = StyledString::EMPTY
  @bg = nil
  @clipped_lines = []
  @blank_line = ""
  self.text = text unless text.nil?
end

Instance Attribute Details

#bgColor, ...

@return — background color applied uniformly across every painted row (including padding past the text). nil (default) leaves whatever bg the text's own styling carries.

Returns:

  • (Color, Symbol, Integer, ::Array[Integer], nil)


31
32
33
# File 'lib/tuile/component/label.rb', line 31

def bg
  @bg
end

#textStyledString, ...

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

Returns:



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

def text
  @text
end

Instance Method Details

#apply_bg(line) ⇒ StyledString

@param line

Parameters:

Returns:



106
107
108
# File 'lib/tuile/component/label.rb', line 106

def apply_bg(line)
  @bg ? line.with_bg(@bg) : line
end

#on_width_changedvoid

This method returns an undefined value.



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

def on_width_changed
  super
  update_clipped_lines
end

#pad_to(line, width) ⇒ StyledString

@param line

@param width

Parameters:

Returns:



113
114
115
116
117
118
# File 'lib/tuile/component/label.rb', line 113

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.



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

def repaint
  return if rect.empty?

  (0...rect.height).each do |row|
    line = @clipped_lines[row] || @blank_line
    screen.buffer.set_line(rect.left, rect.top + row, line)
  end
end

#update_clipped_linesvoid

This method returns an undefined value.

Recomputes @clipped_lines 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_line per row. @blank_line covers rows past the last text line. When #bg is set, every produced line (and the blank row) has the bg applied uniformly.



98
99
100
101
102
# File 'lib/tuile/component/label.rb', line 98

def update_clipped_lines
  width = rect.width.clamp(0, nil)
  @blank_line = apply_bg(StyledString.plain(" " * width))
  @clipped_lines = @text.lines.map { |line| apply_bg(pad_to(line.ellipsize(width), width)) }
end