mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-29 22:11:34 +00:00
Introduce builder methods for Text to make it more ergonomic
This commit is contained in:
parent
70561b7c54
commit
ff47f9480b
@ -55,15 +55,15 @@ fn draw(t: &mut Terminal<MouseBackend>, 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),
|
||||
),
|
||||
];
|
||||
|
@ -114,7 +114,7 @@ fn draw(t: &mut Terminal<AlternateScreenBackend>, 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]);
|
||||
|
@ -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<D: Into<Cow<'b, str>>>(data: D) -> Text<'b> {
|
||||
Text::Data(data.into())
|
||||
}
|
||||
|
||||
pub fn styled_data<D: Into<Cow<'b, str>>>(data: D, style: Style) -> Text<'b> {
|
||||
Text::StyledData(data.into(), style)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 't, T> Paragraph<'a, 't, T>
|
||||
where
|
||||
T: Iterator<Item = &'t Text<'t>>,
|
||||
|
Loading…
x
Reference in New Issue
Block a user