colour: &self.colour,
}
}
+
+ pub fn truncate(&self, width: usize) -> ColouredStringSlice {
+ self.split(width).next().unwrap()
+ }
}
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<ColouredString> for ColouredString {
width: usize,
textpos: usize,
colourpos: usize,
+ delivered_empty: bool,
}
impl<'a> ColouredStringSlice<'a> {
width: width,
textpos: 0,
colourpos: 0,
+ delivered_empty: false,
}
}
}
width: width,
textpos: 0,
colourpos: 0,
+ delivered_empty: false,
}
}
}
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;
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);
}
suffix;
}
vec! {
- suffix.split(width).next().unwrap().to_owned()
+ suffix.truncate(width).to_owned()
}
}
}
ColouredString::uniform(
&((&"-".repeat(max(0, width - 2))).to_string() + "|"),
'-',
- ).split(width).next().unwrap().to_owned(),
+ ).truncate(width).to_owned(),
}
}
}