Class: RubyTUI::Widgets::Paragraph

Inherits:
Object
  • Object
show all
Includes:
RubyTUI::Widget
Defined in:
lib/rubytui/widgets/paragraph.rb

Overview

Paragraph renders styled text with wrapping and alignment.

Examples:

para = RubyTUI::Widgets::Paragraph.new(text: "Hello\nWorld", alignment: :center)
frame.render_widget(para, area)

Constant Summary collapse

WRAP_WORD =

Wrap at whitespace; a single word wider than the area is not broken.

:word
WRAP_CHAR =

Wrap at any character.

:char
WRAP_NONE =

Do not wrap.

:none

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: nil, style: Style::DEFAULT, alignment: :left, wrap: WRAP_WORD, block: nil) ⇒ Paragraph

Returns a new instance of Paragraph.

Parameters:

  • text (String, Line, Text, nil, #to_s) (defaults to: nil) —

    content; a String is split into lines on newlines, nil means no text, other objects are converted with to_s (default: nil)

  • style (Style) (defaults to: Style::DEFAULT) —

    base style under each span's style; when it is not Style::DEFAULT it is also patched onto every cell of the area (default: Style::DEFAULT)

  • alignment (Symbol) (defaults to: :left) —

    :left (default), :center or :right

  • wrap (Symbol) (defaults to: WRAP_WORD) —

    WRAP_WORD (default), WRAP_CHAR or WRAP_NONE. Wrapped lines take the style of the original line's first span

  • block (Widgets::Block, nil) (defaults to: nil) —

    optional wrapping block



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubytui/widgets/paragraph.rb', line 32

def initialize(
  text: nil,
  style: Style::DEFAULT,
  alignment: :left,
  wrap: WRAP_WORD,
  block: nil  # optional Block to render inside
)
  @text = normalize_text(text)
  @style = style
  @alignment = alignment
  @wrap = wrap
  @block = block
end

Instance Attribute Details

#alignment ⇒ Object (readonly)

Returns the value of attribute alignment.



20
21
22
# File 'lib/rubytui/widgets/paragraph.rb', line 20

def alignment
  @alignment
end

#style ⇒ Object (readonly)

Returns the value of attribute style.



20
21
22
# File 'lib/rubytui/widgets/paragraph.rb', line 20

def style
  @style
end

#text ⇒ Object (readonly)

Returns the value of attribute text.



20
21
22
# File 'lib/rubytui/widgets/paragraph.rb', line 20

def text
  @text
end

#wrap ⇒ Object (readonly)

Returns the value of attribute wrap.



20
21
22
# File 'lib/rubytui/widgets/paragraph.rb', line 20

def wrap
  @wrap
end

Instance Method Details

#render(area, buf) ⇒ void

This method returns an undefined value.

Render the wrapped lines from the top of the area; lines beyond its height are not drawn.

Lines are not clipped at the right edge of the area, only at the buffer edge, so unwrapped lines wider than the area extend past it.

Parameters:

  • area (Rect) —

    the rectangular area to render into

  • buf (Buffer) —

    the buffer to write cells into



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubytui/widgets/paragraph.rb', line 55

def render(area, buf)
  return if area.empty?

  render_area = area
  if @block
    @block.render(area, buf)
    render_area = @block.inner(area)
  end

  return if render_area.empty?

  # Apply background style to entire area
  buf.set_style(render_area, @style) if @style != Style::DEFAULT

  # Wrap and render lines
  wrapped = wrap_lines(render_area.width)

  wrapped.each_with_index do |line, i|
    break if i >= render_area.height

    render_line(line, render_area, i, buf)
  end
end