Skip to content

Commit

Permalink
Implement std::ops::Add for AnsiString (#433)
Browse files Browse the repository at this point in the history
Co-authored-by: LoricAndre <[email protected]>
  • Loading branch information
rspencer01 and LoricAndre authored Nov 9, 2024
1 parent 81f391d commit 703b224
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ impl<'a> From<(&'a str, &'a [usize], Attr)> for AnsiString<'a> {
}
}

impl<'a> std::ops::Add for AnsiString<'a> {
type Output = AnsiString<'a>;

fn add(mut self, rhs: Self) -> Self::Output {
let len = self.stripped.as_ref().len() as u32;
if let Some(fragments) = rhs.fragments {
if self.fragments.is_none() {
self.fragments = Some(vec![]);
}
for (attr, (start, end)) in fragments.iter() {
self.fragments.as_mut().unwrap().push((*attr, (start + len, end + len)));
}
}
self.stripped = Cow::owned(self.stripped.into_owned() + rhs.stripped.as_ref());
self
}
}

/// An iterator over all the (char, attr) characters.
pub struct AnsiStringIterator<'a> {
fragments: &'a [(Attr, (u32, u32))],
Expand Down Expand Up @@ -595,6 +613,26 @@ mod tests {
);
}

#[test]
fn test_ansi_string_add() {
let default_attr = Attr::default();
let ar = Attr::default().bg(Color::RED);
let ab = Attr::default().bg(Color::BLUE);

let string_a = AnsiString::new_str("foo", vec![(ar, (1, 3))]);
let string_b = AnsiString::new_str("bar", vec![(ab, (0, 2))]);
let string_c = string_a + string_b;

let mut it = string_c.iter();
assert_eq!(Some(('f', default_attr)), it.next());
assert_eq!(Some(('o', ar)), it.next());
assert_eq!(Some(('o', ar)), it.next());
assert_eq!(Some(('b', ab)), it.next());
assert_eq!(Some(('a', ab)), it.next());
assert_eq!(Some(('r', default_attr)), it.next());
assert_eq!(None, it.next());
}

#[test]
fn test_multi_byte_359() {
// https://github.com/lotabout/skim/issues/359
Expand Down

0 comments on commit 703b224

Please sign in to comment.