Class: RubyTUI::Widgets::Paragraph
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::Paragraph
- Includes:
- RubyTUI::Widget
- Defined in:
- lib/rubytui/widgets/paragraph.rb
Overview
Paragraph renders styled text with wrapping and alignment.
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
-
#alignment ⇒ Object
readonly
Returns the value of attribute alignment.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#wrap ⇒ Object
readonly
Returns the value of attribute wrap.
Instance Method Summary collapse
-
#initialize(text: nil, style: Style::DEFAULT, alignment: :left, wrap: WRAP_WORD, block: nil) ⇒ Paragraph
constructor
A new instance of Paragraph.
-
#render(area, buf) ⇒ void
Render the wrapped lines from the top of the area; lines beyond its height are not drawn.
Constructor Details
#initialize(text: nil, style: Style::DEFAULT, alignment: :left, wrap: WRAP_WORD, block: nil) ⇒ Paragraph
Returns a new instance of Paragraph.
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.
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 |