From 4c301e891d9704fd3578af96d63a3b068252ec6a Mon Sep 17 00:00:00 2001 From: Lena <126529524+acuteenvy@users.noreply.github.com> Date: Sat, 28 Jun 2025 21:53:29 +0000 Subject: [PATCH] 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). --- ratatui-core/src/text/text.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ratatui-core/src/text/text.rs b/ratatui-core/src/text/text.rs index 029545e0..f04981e1 100644 --- a/ratatui-core/src/text/text.rs +++ b/ratatui-core/src/text/text.rs @@ -665,6 +665,15 @@ impl core::ops::Add 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> 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();