mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-29 22:11:34 +00:00
feat(text): implement AddAssign
for Text
(#1956)
This makes it possible to add a second `Text` instance to a first one using the += operator. ```rust let mut text = Text::from("line 1"); text += Text::from("line 2"); ``` Style and alignment applied to the second text is ignored (though styles and alignment of lines and spans are copied).
This commit is contained in:
parent
b9da1926a0
commit
4c301e891d
@ -665,6 +665,15 @@ impl core::ops::Add<Self> for Text<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds two `Text` together.
|
||||
///
|
||||
/// This ignores the style and alignment of the second `Text`.
|
||||
impl core::ops::AddAssign for Text<'_> {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.lines.extend(rhs.lines);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> core::ops::AddAssign<Line<'a>> for Text<'a> {
|
||||
fn add_assign(&mut self, line: Line<'a>) {
|
||||
self.push_line(line);
|
||||
@ -940,6 +949,20 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_assign_text() {
|
||||
let mut text = Text::raw("Red").red();
|
||||
text += Text::raw("Blue").blue();
|
||||
assert_eq!(
|
||||
text,
|
||||
Text {
|
||||
lines: vec![Line::raw("Red"), Line::raw("Blue")],
|
||||
style: Style::new().red(),
|
||||
alignment: None,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_assign_line() {
|
||||
let mut text = Text::raw("Red").red();
|
||||
|
Loading…
x
Reference in New Issue
Block a user