use unicode_segmentation::UnicodeSegmentation;
use super::width;
#[derive(Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct PrintableLines(str);
impl PrintableLines {
pub fn new(text: &str) -> &Self {
debug_assert!(
!text
.chars()
.any(|character| character.is_control() && character != '\n'),
"plain text must not carry terminal control characters: {text:?}"
);
Self::adopt(text)
}
fn adopt(text: &str) -> &Self {
unsafe { &*(std::ptr::from_ref::<str>(text) as *const Self) }
}
pub fn lines(&self) -> Vec<&PrintableText> {
let lines: Vec<&PrintableText> = self.0.lines().map(PrintableText::adopt).collect();
if lines.is_empty() {
vec![PrintableText::adopt("")]
} else {
lines
}
}
}
#[derive(Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct PrintableText(str);
impl PrintableText {
pub fn new(text: &str) -> &Self {
debug_assert!(
!text.chars().any(char::is_control),
"a plain-text line must not carry control characters: {text:?}"
);
Self::adopt(text)
}
fn adopt(text: &str) -> &Self {
unsafe { &*(std::ptr::from_ref::<str>(text) as *const Self) }
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn width(&self) -> usize {
width::text(self)
}
pub fn truncate(&self, width: usize) -> &Self {
if self.width() <= width {
return self;
}
let mut end = 0;
let mut consumed = 0;
for (offset, grapheme) in self.0.grapheme_indices(true) {
let grapheme_width = width::grapheme(Grapheme::adopt(grapheme));
if consumed + grapheme_width > width {
break;
}
consumed += grapheme_width;
end = offset + grapheme.len();
}
Self::adopt(&self.0[..end])
}
pub fn graphemes(&self) -> impl Iterator<Item = &Grapheme> {
self.0.graphemes(true).map(Grapheme::adopt)
}
}
#[derive(Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Grapheme(str);
impl Grapheme {
pub fn new(text: &str) -> &Self {
debug_assert!(
!text.chars().any(char::is_control),
"a grapheme must not carry control characters: {text:?}"
);
debug_assert!(
text.graphemes(true).count() == 1,
"a grapheme must be exactly one cluster: {text:?}"
);
Self::adopt(text)
}
fn adopt(text: &str) -> &Self {
unsafe { &*(std::ptr::from_ref::<str>(text) as *const Self) }
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn width(&self) -> usize {
width::grapheme(self)
}
pub(crate) fn space() -> &'static Self {
Self::adopt(" ")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_wide_characters() {
assert_eq!(PrintableText::new("日本語").width(), 6);
assert_eq!(PrintableText::new("aあ").width(), 3);
assert_eq!(PrintableText::new("👩💻").width(), 2);
assert_eq!(PrintableText::new("e\u{301}").width(), 1);
assert_eq!(PrintableText::new("").width(), 0);
}
#[test]
fn empty_text_is_one_empty_line() {
assert_eq!(
PrintableLines::new("").lines(),
vec![PrintableText::new("")]
);
assert_eq!(
PrintableLines::new("a\nb").lines(),
vec![PrintableText::new("a"), PrintableText::new("b")]
);
}
#[test]
fn a_grapheme_measures_the_cells_of_its_own_cluster() {
assert_eq!(Grapheme::new("a").width(), 1);
assert_eq!(Grapheme::new("\u{3042}").width(), 2);
assert_eq!(Grapheme::new("\u{1F469}\u{200D}\u{1F4BB}").width(), 2);
assert_eq!(Grapheme::new("e\u{301}").width(), 1);
}
#[test]
#[should_panic(expected = "exactly one cluster")]
fn rejects_several_clusters_handed_to_a_grapheme() {
let _ = Grapheme::new("ab");
}
#[test]
#[should_panic(expected = "exactly one cluster")]
fn rejects_empty_text_handed_to_a_grapheme() {
let _ = Grapheme::new("");
}
#[test]
fn graphemes_keep_clusters_whole() {
let clusters: Vec<&str> = PrintableText::new("👩💻x")
.graphemes()
.map(Grapheme::as_str)
.collect();
assert_eq!(clusters, ["👩💻", "x"]);
}
#[test]
#[should_panic(expected = "terminal control characters")]
fn rejects_rendered_output_handed_to_the_plain_domain() {
let _ = PrintableLines::new("\x1b[31mred\x1b[0m");
}
#[test]
#[should_panic(expected = "terminal control characters")]
fn rejects_cursor_movement_in_plain_text() {
let _ = PrintableLines::new("a\tb");
}
#[test]
#[should_panic(expected = "must not carry control characters")]
fn rejects_a_line_break_inside_a_line() {
let _ = PrintableText::new("a\nb");
}
#[test]
fn truncation_drops_a_grapheme_that_would_straddle_the_limit() {
assert_eq!(PrintableText::new("A日本").truncate(3).as_str(), "A日");
assert_eq!(PrintableText::new("日本").truncate(1).as_str(), "");
assert_eq!(
PrintableText::new("e\u{301}x").truncate(1).as_str(),
"e\u{301}"
);
}
}