Skip to main content

truncate_path

Function truncate_path 

Source
pub fn truncate_path(path: &str, max_cols: usize) -> String
Expand description

Truncate a file-system path to max_cols display columns, using a path-aware strategy that preserves the last segment (the project or folder name — the most useful bit) and replaces leading segments with .../. Both / and \ are treated as separators.

Examples (max_cols = 20):

~/Documents/WPSDrive/NotLoginPage → …/NotLoginPage (keeps the last segment)

~/a/b/c (max_cols = 6) → …/c (keeps .../ + last segment)

~/foo (max_cols = 5) → ~/foo (fits, no truncation)

If the last segment alone exceeds max_cols, the function falls back to a plain truncate_with_ellipsis so the output always fits.