Class: RatatuiRuby::Widgets::Paragraph
- Inherits:
-
Object
- Object
- RatatuiRuby::Widgets::Paragraph
- Includes:
- CoerceableWidget
- Defined in:
- lib/ratatui_ruby/widgets/paragraph.rb
Overview
Displays a block of text.
Raw strings are insufficient for UIs. They overflow constraints. They don’t respect alignment (left, center, right).
This widget creates a smart text container. It wraps content to fit the area. It aligns text as requested. It supports scrolling.
Use it for everything from simple labels to complex, multi-paragraph documents.
See also: examples/widget_scroll_text/app.rb for scrollable paragraphs.
Examples
– SPDX-SnippetBegin SPDX-FileCopyrightText: 2026 Kerrick Long SPDX-License-Identifier: MIT-0 ++
# Basic Text
Paragraph.new(text: "Hello, World!")
# Styled container with wrapping
Paragraph.new(
text: "This is a long line that will wrap automatically.",
style: Style.new(fg: :green),
wrap: true,
block: Block.new(title: "Output", borders: [:all])
)
# Scrolling mechanism
Paragraph.new(text: large_text, scroll: [scroll_y, 0])
– SPDX-SnippetEnd ++
Class Method Summary collapse
-
.new(text:, style: nil, fg: nil, bg: nil, block: nil, wrap: false, alignment: :left, scroll: [0, 0]) ⇒ Object
Legacy constructor support.
Instance Method Summary collapse
-
#initialize(text:, style: RatatuiRuby::Style::Style.default, block: nil, wrap: false, alignment: :left, scroll: [0, 0]) ⇒ Paragraph
constructor
Creates a new Paragraph.
-
#line_count(width) ⇒ Object
Returns the number of lines the paragraph would take up if rendered with the given width.
-
#line_width ⇒ Object
Returns the minimum width needed to not wrap any text.
Methods included from CoerceableWidget
Constructor Details
#initialize(text:, style: RatatuiRuby::Style::Style.default, block: nil, wrap: false, alignment: :left, scroll: [0, 0]) ⇒ Paragraph
Creates a new Paragraph.
- text
-
String or Text::Line array.
- style
-
Style object.
- block
-
Block object.
- wrap
-
Boolean (default: false).
- alignment
-
Symbol (default:
:left). - scroll
-
Array of [y, x] integers (duck-typed via
to_int).
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ratatui_ruby/widgets/paragraph.rb', line 80 def initialize(text:, style: RatatuiRuby::Style::Style.default, block: nil, wrap: false, alignment: :left, scroll: [0, 0]) super( text:, style:, block:, wrap:, alignment:, scroll: [Integer(scroll[0]), Integer(scroll[1])] ) end |
Class Method Details
.new(text:, style: nil, fg: nil, bg: nil, block: nil, wrap: false, alignment: :left, scroll: [0, 0]) ⇒ Object
Legacy constructor support.
92 93 94 95 96 |
# File 'lib/ratatui_ruby/widgets/paragraph.rb', line 92 def self.new(text:, style: nil, fg: nil, bg: nil, block: nil, wrap: false, alignment: :left, scroll: [0, 0]) style ||= RatatuiRuby::Style::Style.new(fg:, bg:) coerced_scroll = [Integer(scroll[0]), Integer(scroll[1])] super(text:, style:, block:, wrap:, alignment:, scroll: coerced_scroll) end |
Instance Method Details
#line_count(width) ⇒ Object
Returns the number of lines the paragraph would take up if rendered with the given width.
- width
-
Integer (max width).
101 102 103 104 |
# File 'lib/ratatui_ruby/widgets/paragraph.rb', line 101 def line_count(width) RatatuiRuby.warn_experimental_feature("Paragraph#line_count") RatatuiRuby._paragraph_line_count(self, Integer(width)) end |
#line_width ⇒ Object
Returns the minimum width needed to not wrap any text.
107 108 109 110 |
# File 'lib/ratatui_ruby/widgets/paragraph.rb', line 107 def line_width RatatuiRuby.warn_experimental_feature("Paragraph#line_width") RatatuiRuby._paragraph_line_width(self) end |