Skip to main content

RatatuiTextGenerator

Struct RatatuiTextGenerator 

Source
pub struct RatatuiTextGenerator<P = NoopCustomTagParser<Style>> { /* private fields */ }
Available on crate feature ratatui only.
Expand description

Generator for ratatui crate’s Text type.

See docs/ratatui-tags.ebnf for supported tags.

§Example

use tui_markup::{compile, generator::RatatuiTextGenerator};

assert_eq!(
    compile::<RatatuiTextGenerator>("I have a <green green text>"),
    Ok(Text::from(vec![Line::from(vec![
        Span::raw("I have a "),
        Span::styled("green text", Style::default().fg(Color::Green)),
    ])])),
);

assert_eq!(
    compile::<RatatuiTextGenerator>("I can set <bg:blue background>"),
    Ok(Text::from(vec![Line::from(vec![
        Span::raw("I can set "),
        Span::styled("background", Style::default().bg(Color::Blue)),
    ])])),
);

assert_eq!(
    compile::<RatatuiTextGenerator>("I can add <b bold>, <d dim>, <i italic> modifiers"),
    Ok(Text::from(vec![Line::from(vec![
        Span::raw("I can add "),
        Span::styled("bold", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(", "),
        Span::styled("dim", Style::default().add_modifier(Modifier::DIM)),
        Span::raw(", "),
        Span::styled("italic", Style::default().add_modifier(Modifier::ITALIC)),
        Span::raw(" modifiers"),
    ])])),
);

assert_eq!(
    compile::<RatatuiTextGenerator>("I can <bg:blue combine <green them <b <i all>>>>"),
    Ok(Text::from(vec![Line::from(vec![
        Span::raw("I can "),
        Span::styled("combine ", Style::default().bg(Color::Blue)),
        Span::styled("them ", Style::default().bg(Color::Blue).fg(Color::Green)),
        Span::styled(
            "all",
            Style::default()
                .bg(Color::Blue)
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD | Modifier::ITALIC)
        ),
    ])])),
);

assert_eq!(
    compile::<RatatuiTextGenerator>("I can use <bg:66ccff custom color>"),
    Ok(Text::from(vec![Line::from(vec![
        Span::raw("I can use "),
        Span::styled(
            "custom color",
            Style::default().bg(Color::Rgb(0x66, 0xcc, 0xff))
        ),
    ])])),
);

assert_eq!(
    compile::<RatatuiTextGenerator>("I can set <bg:blue,green,b,i many style> in one tag"),
    Ok(Text::from(vec![Line::from(vec![
        Span::raw("I can set "),
        Span::styled(
            "many style",
            Style::default()
                .bg(Color::Blue)
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD | Modifier::ITALIC)
        ),
        Span::raw(" in one tag"),
    ])])),
);

§With custom tags

use tui_markup::{compile_with, generator::RatatuiTextGenerator};

let g = RatatuiTextGenerator::new(|tag: &str| match tag {
    "keyboard" => Some(
        Style::default()
            .bg(Color::White)
            .fg(Color::Green)
            .add_modifier(Modifier::BOLD),
    ),
    _ => None,
});

assert_eq!(
    compile_with("Press <keyboard W> to move up", g),
    Ok(Text::from(vec![Line::from(vec![
        Span::raw("Press "),
        Span::styled(
            "W",
            Style::default()
                .bg(Color::White)
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD)
        ),
        Span::raw(" to move up"),
    ])])),
);

§Show output

Use any widget of the ratatui crate that supports it’s Text type, for example: ratatui::widgets::Paragraph.

Note that the Paragraph widget includes a ratatui::widgets::Paragraph::wrap option that defaults to trimming leading whitespace. You need to turn this option off if you require full control over the output.

Implementations§

Source§

impl<P> RatatuiTextGenerator<P>

Source

pub fn new(p: P) -> Self

Create a new generator, with a custom tag parser.

Trait Implementations§

Source§

impl<P: Debug> Debug for RatatuiTextGenerator<P>

Source§

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

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

impl<P> Default for RatatuiTextGenerator<P>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, P> Generator<'a> for RatatuiTextGenerator<P>
where P: CustomTagParser<Output = Style>,

Source§

type Convertor = RatatuiTagConvertor<P>

Tag convertor type.
Source§

type Err = GeneratorInfallible

Error type. Read more
Source§

type Output = Text<'a>

Output type.
Source§

fn convertor(&mut self) -> &mut Self::Convertor

Get the tag convertor.
Source§

fn generate( &mut self, markup: Vec<Vec<ItemG<'a, Self>>>, ) -> Result<Self::Output, Self::Err>

Generates final output from IR, which is output result of the Convertor. Read more

Auto Trait Implementations§

§

impl<P> Freeze for RatatuiTextGenerator<P>
where P: Freeze,

§

impl<P> RefUnwindSafe for RatatuiTextGenerator<P>
where P: RefUnwindSafe,

§

impl<P> Send for RatatuiTextGenerator<P>
where P: Send,

§

impl<P> Sync for RatatuiTextGenerator<P>
where P: Sync,

§

impl<P> Unpin for RatatuiTextGenerator<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for RatatuiTextGenerator<P>
where P: UnsafeUnpin,

§

impl<P> UnwindSafe for RatatuiTextGenerator<P>
where P: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.