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:
Lena 2025-06-28 21:53:29 +00:00 committed by GitHub
parent b9da1926a0
commit 4c301e891d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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> { impl<'a> core::ops::AddAssign<Line<'a>> for Text<'a> {
fn add_assign(&mut self, line: Line<'a>) { fn add_assign(&mut self, line: Line<'a>) {
self.push_line(line); 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] #[test]
fn add_assign_line() { fn add_assign_line() {
let mut text = Text::raw("Red").red(); let mut text = Text::raw("Red").red();