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

Generator for tui crate’s Text type.

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

Example

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

assert_eq!(
    compile::<TuiTextGenerator>("I have a <green green text>"),
    Ok(Text { lines: vec![Spans(vec![
        Span::raw("I have a "),
        Span::styled("green text", Style::default().fg(Color::Green)),
    ])] }),
);

assert_eq!(
    compile::<TuiTextGenerator>("I can set <bg:blue background>"),
    Ok(Text { lines: vec![Spans(vec![
        Span::raw("I can set "),
        Span::styled("background", Style::default().bg(Color::Blue)),
    ])] }),
);

assert_eq!(
    compile::<TuiTextGenerator>("I can add <b bold>, <d dim>, <i italic> modifiers"),
    Ok(Text { lines: vec![Spans(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::<TuiTextGenerator>("I can <bg:blue combine <green them <b <i all>>>>"),
    Ok(Text { lines: vec![Spans(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::<TuiTextGenerator>("I can use <bg:66ccff custom color>"),
    Ok(Text { lines: vec![Spans(vec![
        Span::raw("I can use "),
        Span::styled("custom color", Style::default().bg(Color::Rgb(0x66, 0xcc, 0xff))),
    ])] }),
);

assert_eq!(
    compile::<TuiTextGenerator>("I can set <bg:blue,green,b,i many style> in one tag"),
    Ok(Text { lines: vec![Spans(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::TuiTextGenerator};

let gen = TuiTextGenerator::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", gen),
    Ok(Text { lines: vec![Spans(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 tui crate that supports it’s Text type, for example: tui::widgets::Paragraph.

Implementations

Create a new generator, with a custom tag parser.

Trait Implementations

Formats the value using the given formatter. Read more

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

Tag convertor type.

Output type.

Error type. Read more

Get the tag convertor.

Generates final output from ast, which is output result of the Convertor.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.