pub mod text;
pub mod widget;
#[cfg(test)]
mod test {
use super::*;
use ansi_term::{ANSIString, ANSIStrings, Color, Style};
use std::borrow::Cow;
use text::*;
use widget::*;
fn make_spans(style: &Style, text: &str) -> Spans<Style> {
let ansistring: ANSIString = Style::paint(*style, text);
let span: Span<Style> = ansistring.into();
let mut spans: Spans<Style> = Default::default();
spans.push(&span);
spans
}
#[test]
fn split_path() {
let texts = vec![
Color::Black.paint("::"),
Color::Red.paint("SomeExtremelyLong"),
Color::Blue.paint("::"),
Color::Green.paint("RandomAndPoorlyNamed"),
Color::Cyan.paint("::"),
Color::White.paint("Path"),
Color::Yellow.paint("::"),
];
let spans: Spans<_> = texts.iter().map(Span::<Style>::from).collect();
let ellipsis_string = Color::Blue.paint("…");
let ellipsis_span = make_spans(&Color::Blue.normal(), "…");
let truncation = TruncationStyle::Inner(ellipsis_span.clone());
let actual = spans
.split("::")
.map(|Split { segment, delim }| vec![segment, delim])
.flatten()
.flatten()
.map(|s| {
let foo: Box<dyn Fitable<_>> =
Box::new(TextWidget::<Spans<_>, TruncationStyle<_>>::new(
Cow::Owned(s),
Cow::Borrowed(&truncation),
));
foo
})
.collect::<HBox<_>>()
.truncate(20)
.to_string();
let expected = format!(
"{}",
ANSIStrings(&[
Color::Black.paint("::"),
Color::Red.paint("So"),
ellipsis_string.clone(),
Color::Red.paint("g"),
Color::Blue.paint("::"),
Color::Green.paint("Ra"),
ellipsis_string,
Color::Green.paint("d"),
Color::Cyan.paint("::"),
Color::White.paint("Path"),
Color::Yellow.paint("::"),
])
);
assert_eq!(expected, actual);
}
#[test]
fn split_path_2() {
let path = Color::Blue.paint("some//path//with//segments");
let span: Span<Style> = path.clone().into();
let spans = {
let mut spans: Spans<Style> = Default::default();
spans.push(&span);
spans
};
let truncation = TruncationStyle::Inner(Some(make_spans(&Color::Blue.normal(), "……")));
let actual = spans
.split("::")
.map(|Split { segment, delim }| vec![segment, delim])
.flatten()
.flatten()
.map(|s| {
let foo: Box<dyn Fitable<_>> =
Box::new(TextWidget::<Spans<Style>, TruncationStyle<_>>::new(
Cow::Owned(s),
Cow::Borrowed(&truncation),
));
foo
})
.collect::<HBox<_>>()
.truncate(50)
.to_string();
let expected = format!("{}", path);
assert_eq!(expected, actual);
}
}