ratatui / ratatui-macros

Macros for simplifying boilerplate for creating UI using Ratatui.
https://ratatui.rs
MIT License
28 stars 2 forks source link

Consider bringing in macros from https://github.com/sophacles/extra-widgets/tree/main/src/text_macros #10

Closed joshka closed 6 months ago

joshka commented 9 months ago

@sophacles wrote a few interesting macros in https://github.com/sophacles/extra-widgets/tree/main/src/text_macros Ref https://github.com/ratatui-org/ratatui/issues/118

These might be good candidates to bring into this repo.

@sophacles, can you clarify these are ok to be MIT licensed to "The Ratatui Developers"

kdheepak commented 6 months ago

It looks like we converged on the same ideas and between the Stylize trait and the new text!, line! and span! macros, there's only one macro that we haven't brought over the functionality for.

/// Create a [`Vec<Spans>`](ratatui::text::Spans) from lines of a string separated by '\n'
#[macro_export]
macro_rules! split {
    ($e:expr) => {{
        $e.lines()
            .map(|l| ::ratatui::text::Spans::from(l))
            .collect::<Vec<::ratatui::text::Spans>>()
    }};
}

We can bring this over too in the future if we think this is something users might want. I'll close this issue for now.

joshka commented 6 months ago

We can bring this over too in the future if we think this is something users might want. I'll close this issue for now.

Text has functionality to split \n to lines built in, so I think we can skip this probably unless I'm missing something obvious.

kdheepak commented 6 months ago

The Text struct in ratatui has functionality to split new line characters?

joshka commented 6 months ago

https://github.com/ratatui-org/ratatui/blob/76e5fe5a9a1934aa7cce8f0d48c1c9035ac0bf41/src/text/text.rs#L192-L196

        let lines: Vec<_> = match content.into() {
            Cow::Borrowed("") => vec![Line::from("")],
            Cow::Borrowed(s) => s.lines().map(Line::from).collect(),
            Cow::Owned(s) if s.is_empty() => vec![Line::from("")],
            Cow::Owned(s) => s.lines().map(|l| Line::from(l.to_owned())).collect(),
kdheepak commented 6 months ago

Ah, TIL!