Skip to content

Commit 4c301e8

Browse files
authored
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).
1 parent b9da192 commit 4c301e8

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

ratatui-core/src/text/text.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,15 @@ impl core::ops::Add<Self> for Text<'_> {
665665
}
666666
}
667667

668+
/// Adds two `Text` together.
669+
///
670+
/// This ignores the style and alignment of the second `Text`.
671+
impl core::ops::AddAssign for Text<'_> {
672+
fn add_assign(&mut self, rhs: Self) {
673+
self.lines.extend(rhs.lines);
674+
}
675+
}
676+
668677
impl<'a> core::ops::AddAssign<Line<'a>> for Text<'a> {
669678
fn add_assign(&mut self, line: Line<'a>) {
670679
self.push_line(line);
@@ -940,6 +949,20 @@ mod tests {
940949
);
941950
}
942951

952+
#[test]
953+
fn add_assign_text() {
954+
let mut text = Text::raw("Red").red();
955+
text += Text::raw("Blue").blue();
956+
assert_eq!(
957+
text,
958+
Text {
959+
lines: vec![Line::raw("Red"), Line::raw("Blue")],
960+
style: Style::new().red(),
961+
alignment: None,
962+
}
963+
);
964+
}
965+
943966
#[test]
944967
fn add_assign_line() {
945968
let mut text = Text::raw("Red").red();

0 commit comments

Comments
 (0)