Lines
100 %
Functions
50 %
Branches
//! The end-of-text clip of cells whose tail matters more than their start.
use std::borrow::Cow;
use ratatui::layout::Rect;
use tvui::ARROW_CELLS;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use super::Item;
impl Item<'_> {
/// The item shows the end of its text, not the start, while it is not focused and clipped.
#[must_use]
pub fn tail(mut self) -> Self {
self.tail = true;
self
}
/// The text to draw in `rect`: a clipped tail item keeps the end of its text after a `…`.
pub fn shown(&self, rect: Rect) -> Cow<'_, str> {
let room = usize::from(self.area(rect).width.saturating_sub(ARROW_CELLS));
if !self.tail || self.focused || self.text.width() <= room {
return Cow::Borrowed(&self.text);
let mut used = 1;
let kept = self.text.char_indices().rev().take_while(|(_, ch)| {
used += ch.width().unwrap_or(0);
used <= room
});
let start = kept.last().map_or(self.text.len(), |(at, _)| at);
let tail = self.text[start..].trim_start_matches(|ch: char| ch.width() == Some(0));
Cow::Owned(format!("…{tail}"))