Skip to main content

get_truncated_path

Function get_truncated_path 

Source
pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf
Expand description

Returns a truncated filepath where the basepart of the path is reduced to the first char of the folder and the whole filename appended.

Also strip the current working directory from the beginning of the path. Note that this function does not check if the truncated path is unambiguous.

   use helix_stdx::path::get_truncated_path;
   use std::path::Path;

   assert_eq!(
        get_truncated_path("/home/cnorris/documents/jokes.txt").as_path(),
        Path::new("/h/c/d/jokes.txt")
    );
    assert_eq!(
        get_truncated_path("jokes.txt").as_path(),
        Path::new("jokes.txt")
    );
    assert_eq!(
        get_truncated_path("/jokes.txt").as_path(),
        Path::new("/jokes.txt")
    );
    assert_eq!(
        get_truncated_path("/h/c/d/jokes.txt").as_path(),
        Path::new("/h/c/d/jokes.txt")
    );
    assert_eq!(get_truncated_path("").as_path(), Path::new(""));