From 9b006f82536f19c71dba0db6be611fd74f412e7a Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 24 Dec 2023 22:29:48 +0000 Subject: [PATCH] Factor out truncation of a ColouredString --- src/coloured_string.rs | 25 ++++++++++++++++++++++++- src/text.rs | 4 ++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/coloured_string.rs b/src/coloured_string.rs index b9a6294..0806799 100644 --- a/src/coloured_string.rs +++ b/src/coloured_string.rs @@ -53,6 +53,10 @@ impl ColouredString { colour: &self.colour, } } + + pub fn truncate(&self, width: usize) -> ColouredStringSlice { + self.split(width).next().unwrap() + } } impl<'a> ColouredStringSlice<'a> { @@ -75,6 +79,10 @@ impl<'a> ColouredStringSlice<'a> { 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 } + + pub fn truncate(&'a self, width: usize) -> ColouredStringSlice<'a> { + self.split(width).next().unwrap() + } } impl std::ops::Add for ColouredString { @@ -138,6 +146,7 @@ pub struct ColouredStringSplitIterator<'a> { width: usize, textpos: usize, colourpos: usize, + delivered_empty: bool, } impl<'a> ColouredStringSlice<'a> { @@ -154,6 +163,7 @@ impl<'a> ColouredStringSlice<'a> { width: width, textpos: 0, colourpos: 0, + delivered_empty: false, } } } @@ -172,6 +182,7 @@ impl<'a> ColouredString { width: width, textpos: 0, colourpos: 0, + delivered_empty: false, } } } @@ -222,7 +233,14 @@ impl<'a> Iterator for ColouredStringSplitIterator<'a> { let colourslice = &self.cs.colour[self.colourpos..]; let mut colourit = colourslice.char_indices(); match (textit.next(), colourit.next()) { - (None, None) => None, + (None, None) => { + if self.textpos == 0 && !self.delivered_empty { + self.delivered_empty = true; + Some(ColouredStringSlice::general("", "")) + } else { + None + } + }, (Some((_, tc)), Some(_)) => { let mut tpos: usize = 0; let mut cpos: usize = 0; @@ -322,4 +340,9 @@ fn test_split() { assert_eq!(lines.next(), Some(ColouredStringSlice::general("efg", "qrs"))); assert_eq!(lines.next(), Some(ColouredStringSlice::general("h", "t"))); assert_eq!(lines.next(), None); + + let cs = ColouredStringSlice::general("", ""); + let mut lines = cs.split(3); + assert_eq!(lines.next(), Some(ColouredStringSlice::general("", ""))); + assert_eq!(lines.next(), None); } diff --git a/src/text.rs b/src/text.rs index 43f66f2..05f4711 100644 --- a/src/text.rs +++ b/src/text.rs @@ -72,7 +72,7 @@ impl TextFragment for SeparatorLine { suffix; } vec! { - suffix.split(width).next().unwrap().to_owned() + suffix.truncate(width).to_owned() } } } @@ -89,7 +89,7 @@ impl TextFragment for EditorHeaderSeparator { ColouredString::uniform( &((&"-".repeat(max(0, width - 2))).to_string() + "|"), '-', - ).split(width).next().unwrap().to_owned(), + ).truncate(width).to_owned(), } } } -- 2.30.2