From ff47f9480b34d560a3965734af727ef637f5f74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20B=C3=BCsch?= Date: Thu, 6 Sep 2018 11:54:29 +1000 Subject: [PATCH] Introduce builder methods for Text to make it more ergonomic --- examples/paragraph.rs | 14 +++++++------- examples/user_input.rs | 2 +- src/widgets/paragraph.rs | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/examples/paragraph.rs b/examples/paragraph.rs index 8633f736..20afe2ab 100644 --- a/examples/paragraph.rs +++ b/examples/paragraph.rs @@ -55,15 +55,15 @@ fn draw(t: &mut Terminal, size: Rect) -> Result<(), io::Error> { .split(size); let text = [ - Text::Data("This a line\n".into()), - Text::StyledData("This a line\n".into(), Style::default().fg(Color::Red)), - Text::StyledData("This a line\n".into(), Style::default().bg(Color::Blue)), - Text::StyledData( - "This a longer line\n".into(), + Text::data("This a line\n"), + Text::styled_data("This a line\n", Style::default().fg(Color::Red)), + Text::styled_data("This a line\n", Style::default().bg(Color::Blue)), + Text::styled_data( + "This a longer line\n", Style::default().modifier(Modifier::CrossedOut), ), - Text::StyledData( - "This a line\n".into(), + Text::styled_data( + "This a line\n", Style::default().fg(Color::Green).modifier(Modifier::Italic), ), ]; diff --git a/examples/user_input.rs b/examples/user_input.rs index db8e41f3..ecde3c42 100644 --- a/examples/user_input.rs +++ b/examples/user_input.rs @@ -114,7 +114,7 @@ fn draw(t: &mut Terminal, app: &App) -> Result<(), io::E .margin(2) .constraints([Constraint::Length(3), Constraint::Min(1)].as_ref()) .split(app.size); - Paragraph::new([Text::Data((&app.input).into())].iter()) + Paragraph::new([Text::data(&app.input)].iter()) .style(Style::default().fg(Color::Yellow)) .block(Block::default().borders(Borders::ALL).title("Input")) .render(&mut f, chunks[0]); diff --git a/src/widgets/paragraph.rs b/src/widgets/paragraph.rs index f02efa78..fa3bbebe 100644 --- a/src/widgets/paragraph.rs +++ b/src/widgets/paragraph.rs @@ -21,8 +21,8 @@ use widgets::{Block, Widget}; /// # use tui::layout::{Alignment}; /// # fn main() { /// let text = [ -/// Text::Data("First line\n".into()), -/// Text::StyledData("Second line\n".into(), Style::default().fg(Color::Red)) +/// Text::data("First line\n"), +/// Text::styled_data("Second line\n", Style::default().fg(Color::Red)) /// ]; /// Paragraph::new(text.iter()) /// .block(Block::default().title("Paragraph").borders(Borders::ALL)) @@ -56,6 +56,16 @@ pub enum Text<'b> { StyledData(Cow<'b, str>, Style), } +impl<'b> Text<'b> { + pub fn data>>(data: D) -> Text<'b> { + Text::Data(data.into()) + } + + pub fn styled_data>>(data: D, style: Style) -> Text<'b> { + Text::StyledData(data.into(), style) + } +} + impl<'a, 't, T> Paragraph<'a, 't, T> where T: Iterator>,