Skip to main content

Paragraph

Struct Paragraph 

Source
pub struct Paragraph { /* private fields */ }
Expand description

A widget combining multiple widgets implementing the Text trait into single widget. A Paragraph combines multiple widgets implementing the Text trait into single widget, displaying them sequantially with configurable separator.

Unlike layout-based widgets, Paragraph places spans directly next to each other, allowing for more natural inline text composition.

§Example

// Creates a Paragraph from a list of spans
let items: Vec<Box<dyn Text>> = vec![
    Box::new("This is a text in".fg(Color::Yellow)),
    Box::new("paragraph".modifier(Modifier::BOLD).fg(Color::Cyan)),
    Box::new("and it adds".to_span()),
    Box::new("separator".modifier(Modifier::ITALIC)),
];
let mut p = Paragraph::new(items)
    .wrap(Wrap::Letter)
    .separator("-");

// Alternatively, use the `paragraph!` macro for convenience
let mut p = paragraph!(
    "This is a text in".fg(Color::Yellow),
    "paragraph".modifier(Modifier::BOLD).fg(Color::Cyan),
    "and it adds".to_span(),
    "separator".modifier(Modifier::ITALIC),
);
// Add more text later if needed
p.push("between each span");

// Print the Paragraph as a string
println!("{p}");

// Or you can render it using `Term` (or manually using `Buffer`)
let mut term = Term::default();
term.render(p)?;

Implementations§

Source§

impl Paragraph

Source

pub fn new(children: Vec<Box<dyn Text>>) -> Self

Creates a new Paragraph with the given child elements.

Source

pub fn empty() -> Self

Creates an empty Paragraph with no children.

Source

pub fn get(&self) -> String

Gets the rendered content of the Paragraph as a String, joining all child elements with the configured separator.

It ignores wrapping and any other position based settings.

Source

pub fn separator(self, sep: &str) -> Self

Sets the separator used between child elements when rendering the Paragraph.

Source

pub fn wrap(self, wrap: Wrap) -> Self

Sets text wrapping style of the Paragraph.

Default value is Wrap::Word.

Source

pub fn add<T>(&mut self, child: T)
where T: Into<Box<dyn Text>>,

👎Deprecated since 0.6.0: Kept for compatibility purposes; use push function instead

Adds a child element to the Paragraph

Source

pub fn push<T>(&mut self, child: T)
where T: Into<Box<dyn Text>>,

Adds a child element to the Paragraph

Trait Implementations§

Source§

impl Debug for Paragraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Paragraph

Source§

fn default() -> Self

Creates Paragraph filled with default values

Source§

impl Display for Paragraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Paragraph> for Box<dyn Widget>

Source§

fn from(value: Paragraph) -> Self

Converts to this type from the input type.
Source§

impl From<Paragraph> for Element

Source§

fn from(value: Paragraph) -> Self

Converts to this type from the input type.
Source§

impl Widget for Paragraph

Source§

fn render(&self, buffer: &mut Buffer, rect: Rect, _cache: &mut Cache)

Renders the widget into the given Buffer within the provided Rect bounds.
Source§

fn height(&self, size: &Vec2) -> usize

Returns the height of the Widget based on the width of the given size.
Source§

fn width(&self, size: &Vec2) -> usize

Returns the width of the Widget based on the height of the given size.
Source§

fn children(&self) -> Vec<&Element>

Gets widget’s children

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Scale<T> for T

Source§

fn scale(self) -> T

Scale this type to the given other type.
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.