//! Text measurement and padding support. //! //! This module defines the [`Text`] trait used by labels, lines, and painted //! values to measure terminal display width, truncate content, and pad rows. //! Cell storage itself lives in [`buffer`](crate::buffer); this module is about //! text-like values that occupy terminal columns. use std::fmt; use super::{Color, Filled, Line, Paint}; /// Default background color for text-like values. pub const DEFAULT_BACKGROUND: Color = Color::Unset; /// Text that can be displayed on the terminal, measured, truncated and padded. pub trait Text: fmt::Display { /// Type after truncation. type Truncated: Text; /// Type after padding. type Padded: Text; /// Cell display width in number of terminal columns. fn width(&self) -> usize; /// Background color of cell. fn background(&self) -> Color { DEFAULT_BACKGROUND } /// Truncate cell if longer than given width. Shows the delimiter if truncated. #[must_use] fn truncate(&self, width: usize, delim: &str) -> Self::Truncated; /// Pad the cell so that it is the given width, while keeping the content left-aligned. #[must_use] fn pad(&self, width: usize) -> Self::Padded; } impl Text for Paint { type Truncated = Self; type Padded = Self; fn width(&self) -> usize { Text::width(self.content()) } fn background(&self) -> Color { self.style.background } fn truncate(&self, width: usize, delim: &str) -> Self { Self { item: self.item.truncate(width, delim), style: self.style, } } fn pad(&self, width: usize) -> Self { Self { item: self.item.pad(width), style: self.style, } } } impl Text for Line { type Truncated = Line; type Padded = Line; fn width(&self) -> usize { Line::width(self) } fn pad(&self, width: usize) -> Self::Padded { let mut line = self.clone(); Line::pad(&mut line, width); line } fn truncate(&self, width: usize, delim: &str) -> Self::Truncated { let mut line = self.clone(); Line::truncate(&mut line, width, delim); line } } impl Text for Paint<&str> { type Truncated = Paint; type Padded = Paint; fn width(&self) -> usize { Text::width(self.item) } fn background(&self) -> Color { self.style.background } fn truncate(&self, width: usize, delim: &str) -> Paint { Paint { item: self.item.truncate(width, delim), style: self.style, } } fn pad(&self, width: usize) -> Paint { Paint { item: self.item.pad(width), style: self.style, } } } impl Text for String { type Truncated = Self; type Padded = Self; fn width(&self) -> usize { Text::width(self.as_str()) } fn truncate(&self, width: usize, delim: &str) -> Self { self.as_str().truncate(width, delim) } fn pad(&self, width: usize) -> Self { self.as_str().pad(width) } } impl Text for str { type Truncated = String; type Padded = String; fn width(&self) -> usize { if self.is_ascii() { self.len() } else { self.chars().map(char_width).sum() } } fn truncate(&self, width: usize, delim: &str) -> String { let d = Text::width(delim); let mut cols = 0; let mut truncation_boundary = 0; let mut truncated = false; let truncation_width = width.saturating_sub(d); for (index, ch) in self.char_indices() { let c = char_width(ch); if cols + c > width { truncated = true; break; } cols += c; if cols <= truncation_width { truncation_boundary = index + ch.len_utf8(); } } if !truncated { self.to_owned() } else if width < d { String::new() } else if self[truncation_boundary..].trim().is_empty() { self[..truncation_boundary].to_owned() } else { format!("{}{delim}", &self[..truncation_boundary]) } } fn pad(&self, max: usize) -> String { let width = Text::width(self); if width < max { format!("{self}{}", " ".repeat(max - width)) } else { self.to_owned() } } } impl Text for &T { type Truncated = T::Truncated; type Padded = T::Padded; fn width(&self) -> usize { T::width(self) } fn truncate(&self, width: usize, delim: &str) -> Self::Truncated { T::truncate(self, width, delim) } fn pad(&self, width: usize) -> Self::Padded { T::pad(self, width) } } impl Text for Filled { type Truncated = T::Truncated; type Padded = T::Padded; fn width(&self) -> usize { T::width(&self.item) } fn background(&self) -> Color { self.color } fn truncate(&self, width: usize, delim: &str) -> Self::Truncated { T::truncate(&self.item, width, delim) } fn pad(&self, width: usize) -> Self::Padded { T::pad(&self.item, width) } } /// Returns display width for one character. pub(crate) fn char_width(ch: char) -> usize { if ch == '\u{fe0f}' || ch == '\u{fe0e}' || ch.is_control() { 0 // TODO: Move these glyphs somewhere and make them configurable, or include the // full set of unicode glyphs. } else if matches!(ch, 'βœ“' | 'βœ”' | 'βœ—' | 'πŸ—©' | 'πŸž„') { 1 } else if matches!( ch as u32, 0x1100..=0x115f | 0x2329..=0x232a | 0x2600..=0x27bf | 0x2e80..=0xa4cf | 0xac00..=0xd7a3 | 0xf900..=0xfaff | 0xfe10..=0xfe19 | 0xfe30..=0xfe6f | 0xff00..=0xff60 | 0xffe0..=0xffe6 | 0x1f000..=0x1faff | 0x20000..=0x3fffd ) { 2 } else { 1 } } #[cfg(test)] mod tests { use super::Text as _; #[test] fn test_width() { assert_eq!("abc".width(), 3); assert_eq!("βœ“".width(), 1); assert_eq!("βœ”".width(), 1); assert_eq!("βœ—".width(), 1); assert_eq!("πŸ—©".width(), 1); assert_eq!("πŸž„".width(), 1); assert_eq!("2 βœ” Β· 4 hunks left".width(), 18); assert_eq!("❀️".width(), 2); assert_eq!("πŸͺ΅".width(), 2); } #[test] fn test_truncate() { assert_eq!("🍍".truncate(1, "…"), String::from("…")); assert_eq!("🍍".truncate(1, ""), String::new()); assert_eq!("🍍🍍".truncate(2, "…"), String::from("…")); assert_eq!("hello".truncate(3, "…"), String::from("he…")); assert_eq!("🍍🍍".truncate(3, "…"), String::from("πŸβ€¦")); assert_eq!("🍍".truncate(1, "🍎"), String::new()); assert_eq!("🍍".truncate(2, "🍎"), String::from("🍍")); assert_eq!("🍍🍍".truncate(3, "🍎"), String::from("🍎")); assert_eq!("🍍🍍🍍".truncate(4, "🍎"), String::from("🍍🍎")); } }