From: Simon Tatham Date: Sun, 24 Dec 2023 21:26:23 +0000 (+0000) Subject: More ColouredString stuff I needed X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=b49aa7381ed3f55b57e3edc5126d26231b76d38c;p=mastodonochrome.git More ColouredString stuff I needed --- diff --git a/src/coloured_string.rs b/src/coloured_string.rs index 9d21ec8..b9a6294 100644 --- a/src/coloured_string.rs +++ b/src/coloured_string.rs @@ -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 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 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 for &str { type Output = ColouredString; fn add(self, rhs: ColouredString) -> ColouredString {