use std::path::Path;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Paths {
home: Option<String>,
}
impl Paths {
pub fn detect() -> Self {
Self::under(crate::home::user_home().ok().as_deref())
}
pub fn under(home: Option<&Path>) -> Self {
let home = home
.map(|h| h.to_string_lossy().trim_end_matches(['/', '\\']).to_owned())
.filter(|h| !h.is_empty() && !h.ends_with(':'));
Self { home }
}
pub fn shorten(&self, text: &str) -> String {
let Some(home) = self.home.as_deref() else {
return text.to_owned();
};
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find(home) {
let after = &rest[at + home.len()..];
let starts_path = at == 0 || !continues_name(rest[..at].chars().next_back());
let ends_home = after
.chars()
.next()
.is_none_or(|c| matches!(c, '/' | '\\') || !continues_name(Some(c)));
out.push_str(&rest[..at]);
if starts_path && ends_home {
out.push('~');
} else {
out.push_str(home);
}
rest = after;
}
out.push_str(rest);
out
}
pub fn path(&self, path: &Path) -> String {
self.shorten(&path.to_string_lossy())
}
}
fn continues_name(c: Option<char>) -> bool {
c.is_some_and(|c| c.is_alphanumeric() || matches!(c, '-' | '_' | '.' | '/' | '\\'))
}