use unicode_width::{UnicodeWidthChar as _, UnicodeWidthStr};
pub(crate) const MIN_WIDTH: u16 = 20;
pub(crate) const DIFF_MIN_WIDTH: u16 = 48;
const DIFF_MIN_FOCUSED: u16 = 40;
pub(crate) fn width(content: u16, room: u16, chosen: Option<u16>, focused: bool) -> Option<u16> {
let most = (room * 2 / 5).min(room.saturating_sub(1 + DIFF_MIN_WIDTH));
if most >= MIN_WIDTH {
let ceiling = match chosen {
Some(_) => room.saturating_sub(1 + DIFF_MIN_WIDTH),
None => most,
};
return Some(chosen.unwrap_or(content).clamp(MIN_WIDTH, ceiling));
}
let squeezed = room.saturating_sub(1 + DIFF_MIN_FOCUSED);
(focused && squeezed >= MIN_WIDTH).then(|| chosen.unwrap_or(content).min(squeezed).max(1))
}
pub(crate) fn middle(name: &str, width: usize) -> String {
if name.width() <= width {
return name.to_owned();
}
if width <= 1 {
return "\u{2026}".repeat(width);
}
let room = width - 1;
let ext = name.rfind('.').filter(|&at| at > 0).map_or(0, |at| name.len() - at);
let ext_width = name.get(name.len() - ext..).map_or(0, UnicodeWidthStr::width);
let tail_width = room.div_ceil(2).max(ext_width.min(room.saturating_sub(1)));
let tail = take_end(name, tail_width);
let head = take_start(name, room - tail.width());
format!("{head}\u{2026}{tail}")
}
pub(crate) fn folder(label: &str, width: usize) -> String {
if label.width() <= width {
return label.to_owned();
}
let segments: Vec<&str> = label.trim_end_matches('/').split('/').collect();
let count = segments.len();
for keep in (1..count).rev() {
let tail = format!("{}/", segments.get(count - keep..).unwrap_or_default().join("/"));
if let Some(first) = segments.first().filter(|_| keep + 1 < count) {
let both = format!("{first}/\u{2026}/{tail}");
if both.width() <= width {
return both;
}
}
let cut = format!("\u{2026}/{tail}");
if cut.width() <= width {
return cut;
}
}
middle(label, width)
}
fn take_start(text: &str, width: usize) -> &str {
let mut used = 0;
for (at, ch) in text.char_indices() {
used += ch.width().unwrap_or(0);
if used > width {
return text.get(..at).unwrap_or_default();
}
}
text
}
fn take_end(text: &str, width: usize) -> &str {
let mut used = 0;
for (at, ch) in text.char_indices().rev() {
used += ch.width().unwrap_or(0);
if used > width {
return text.get(at + ch.len_utf8()..).unwrap_or_default();
}
}
text
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Shown {
pub(crate) status: bool,
pub(crate) counts: bool,
}
pub(crate) fn columns(names: &[usize], room: usize, status: usize, counts: usize) -> Shown {
let options = [
Shown { status: true, counts: true },
Shown { status: true, counts: false },
Shown { status: false, counts: false },
];
let cut = |shown: &Shown| {
let right = if shown.status { status } else { 0 } + if shown.counts { counts } else { 0 };
names.iter().filter(|&&name| name + right > room).count()
};
let mut best = Shown { status: true, counts: true };
let mut fewest = cut(&best);
for option in options.into_iter().skip(1) {
let n = cut(&option);
if n < fewest {
(best, fewest) = (option, n);
}
}
best
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_long_name_keeps_its_start_its_end_and_its_extension() {
let name = "ReviewAgentJobDetailPanel.tsx";
assert_eq!(middle(name, 40), name);
let cut = middle(name, 20);
assert_eq!(cut.width(), 20);
assert_eq!(cut, "ReviewAge\u{2026}lPanel.tsx");
let cut = middle(name, 8);
assert!(cut.starts_with('R') && cut.chars().rev().take(4).eq("xst.".chars()), "{cut}");
assert_eq!(middle(name, 1), "\u{2026}");
assert_eq!(middle(name, 0), "");
}
#[test]
fn wide_characters_are_cut_by_columns() {
let cut = middle("設計ドキュメント一覧表.md", 12);
assert!(cut.width() <= 12 && cut.chars().rev().take(3).eq("dm.".chars()), "{cut}");
}
#[test]
fn a_folder_row_keeps_its_last_folders() {
let label = "packages/review-editor/components/";
assert_eq!(folder(label, 40), label);
assert_eq!(folder(label, 30), "\u{2026}/review-editor/components/");
assert_eq!(folder(label, 24), "packages/\u{2026}/components/");
assert_eq!(folder(label, 15), "\u{2026}/components/");
let cut = folder(label, 8);
assert_eq!(cut.width(), 8);
assert!(cut.ends_with('/'), "{cut}");
}
#[test]
fn columns_go_for_every_row_and_only_when_that_shows_more_names_whole() {
assert_eq!(columns(&[10, 18], 30, 2, 9), Shown { status: true, counts: true });
assert_eq!(columns(&[10, 20], 25, 2, 9), Shown { status: true, counts: false });
assert_eq!(columns(&[10, 24], 25, 2, 9), Shown { status: false, counts: false });
assert_eq!(columns(&[10, 60], 25, 2, 9), Shown { status: true, counts: true });
}
#[test]
fn the_list_fits_its_rows_within_two_fifths_of_the_room() {
assert_eq!(width(14, 160, None, false), Some(MIN_WIDTH));
assert_eq!(width(30, 160, None, false), Some(30));
assert_eq!(width(90, 160, None, false), Some(64));
assert_eq!(width(30, 160, Some(100), false), Some(100));
assert_eq!(width(30, 160, Some(200), false), Some(160 - 1 - DIFF_MIN_WIDTH));
assert_eq!(width(30, 160, Some(4), false), Some(MIN_WIDTH));
}
#[test]
fn without_room_the_list_hides_unless_it_has_the_focus() {
assert_eq!(width(30, 51, None, false), None);
assert_eq!(width(30, 51, None, true), None);
assert_eq!(width(30, 64, None, true), Some(23));
assert_eq!(width(40, 69, None, false), Some(MIN_WIDTH));
assert_eq!(width(40, 80, None, false), Some(31));
}
}