chiark / gitweb /
More ColouredString stuff I needed
authorSimon Tatham <anakin@pobox.com>
Sun, 24 Dec 2023 21:26:23 +0000 (21:26 +0000)
committerSimon Tatham <anakin@pobox.com>
Sun, 24 Dec 2023 21:26:23 +0000 (21:26 +0000)
src/coloured_string.rs

index 9d21ec83fe8ed4be3744109cd39dd45ccef1dbdc..b9a62940969fc1b1d774cb27b5b3b6fa15bca6ee 100644 (file)
@@ -65,6 +65,13 @@ impl<'a> ColouredStringSlice<'a> {
         }
     }
 
+    pub fn to_owned(&self) -> ColouredString {
+        ColouredString {
+            text: self.text.to_string(),
+            colour: self.colour.to_string(),
+        }
+    }
+
     pub fn nchars(&self) -> usize { self.text.chars().count() }
     pub fn width(&self) -> usize { UnicodeWidthStr::width(&self.text as &str) }
     pub fn text(&self) -> &'a str { &self.text }
@@ -80,6 +87,16 @@ impl std::ops::Add<ColouredString> for ColouredString {
     }
 }
 
+impl std::ops::Add<&ColouredString> for ColouredString {
+    type Output = Self;
+    fn add(self, rhs: &ColouredString) -> Self {
+        ColouredString {
+            text: self.text + &rhs.text,
+            colour: self.colour + &rhs.colour,
+        }
+    }
+}
+
 impl std::ops::Add<&str> for ColouredString {
     type Output = Self;
     fn add(self, rhs: &str) -> Self {
@@ -90,6 +107,16 @@ impl std::ops::Add<&str> for ColouredString {
     }
 }
 
+impl std::ops::Add<ColouredString> for &ColouredString {
+    type Output = ColouredString;
+    fn add(self, rhs: ColouredString) -> ColouredString {
+        ColouredString {
+            text: self.text.to_string() + &rhs.text,
+            colour: self.colour.to_string() + &rhs.colour,
+        }
+    }
+}
+
 impl std::ops::Add<ColouredString> for &str {
     type Output = ColouredString;
     fn add(self, rhs: ColouredString) -> ColouredString {