use gpui_kit::{
App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, Window, component::tooltip::Tooltip, div,
prelude::FluentBuilder as _, relative, rems,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TruncateMode {
#[default]
End,
Middle,
Path,
}
impl TruncateMode {
pub const ALL: [TruncateMode; 3] = [Self::End, Self::Middle, Self::Path];
pub const fn name(self) -> &'static str {
match self {
Self::End => "end",
Self::Middle => "middle",
Self::Path => "path",
}
}
}
const MIDDLE_TAIL_MAX: usize = 12;
pub fn split(text: &str, mode: TruncateMode) -> (String, String) {
match mode {
TruncateMode::End => (text.to_string(), String::new()),
TruncateMode::Path => match text.rfind('/') {
Some(i) => (text[..=i].to_string(), text[i + 1..].to_string()),
None => (String::new(), text.to_string()),
},
TruncateMode::Middle => {
let total = text.chars().count();
let tail_n = total.div_ceil(2).min(MIDDLE_TAIL_MAX);
let head_n = total.saturating_sub(tail_n);
let i = text
.char_indices()
.nth(head_n)
.map_or(text.len(), |(i, _)| i);
(text[..i].to_string(), text[i..].to_string())
}
}
}
#[derive(IntoElement)]
pub struct Truncate {
id: ElementId,
text: SharedString,
mode: TruncateMode,
title: bool,
}
impl Truncate {
pub fn new(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
text: text.into(),
mode: TruncateMode::End,
title: true,
}
}
pub fn mode(mut self, mode: TruncateMode) -> Self {
self.mode = mode;
self
}
pub fn title(mut self, title: bool) -> Self {
self.title = title;
self
}
}
fn uses_mid_split(head: &str, tail: &str, mode: TruncateMode) -> bool {
!head.is_empty()
&& !tail.is_empty()
&& !(mode == TruncateMode::Middle && head.chars().count() < 4)
}
impl RenderOnce for Truncate {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let full = self.text.clone();
let tip = self.title.then(|| full.clone());
let base = div()
.id(self.id.clone())
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.when_some(tip, |d, t| {
d.tooltip(move |window, cx| Tooltip::new(t.clone()).build(window, cx))
});
if self.mode == TruncateMode::End {
return base.text_ellipsis().child(full);
}
let (head, tail) = split(&self.text, self.mode);
if !uses_mid_split(&head, &tail, self.mode) {
return base.text_ellipsis().child(full);
}
base.flex()
.child(
div()
.flex_shrink_1()
.min_w(rems(1.5 * 0.875))
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(head),
)
.child(
div()
.flex_none()
.max_w(relative(0.7))
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(tail),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn splits_follow_the_reference() {
assert_eq!(
split(
"packages/web/src/components/sidebar/SidebarThreadRow.tsx",
TruncateMode::Path
),
(
"packages/web/src/components/sidebar/".to_string(),
"SidebarThreadRow.tsx".to_string()
)
);
assert_eq!(
split("feature/dark", TruncateMode::Middle),
("featur".to_string(), "e/dark".to_string())
);
assert_eq!(
split(
"feat/onboarding-banner-for-long-languages-ENG-1142",
TruncateMode::Middle
),
(
"feat/onboarding-banner-for-long-langua".to_string(),
"ges-ENG-1142".to_string()
)
);
assert_eq!(split("README.md", TruncateMode::Path).0, "");
assert_eq!(split("设置通用", TruncateMode::Middle).1, "通用");
let (h, t) = split("README.md", TruncateMode::Path);
assert!(!uses_mid_split(&h, &t, TruncateMode::Path));
let (h, t) = split("main", TruncateMode::Middle);
assert!(!uses_mid_split(&h, &t, TruncateMode::Middle));
let (h, t) = split("feature/dark", TruncateMode::Middle);
assert!(uses_mid_split(&h, &t, TruncateMode::Middle));
}
}