use std::path::Path;
use crate::utils::path::format::abbreviate_path_keep;
use crate::utils::path::format::shorten_path;
use crate::utils::path::home::home_relative;
#[derive(Clone, Debug)]
pub struct PathDisplay {
pub max_width: Option<usize>,
pub use_tilde: bool,
pub abbreviate: bool,
pub keep_dirs: usize,
}
impl Default for PathDisplay {
fn default() -> Self {
Self {
max_width: None,
use_tilde: true,
abbreviate: false,
keep_dirs: 2,
}
}
}
impl PathDisplay {
pub fn new() -> Self {
Self::default()
}
pub fn max_width(mut self, width: usize) -> Self {
self.max_width = Some(width);
self
}
pub fn tilde(mut self, use_tilde: bool) -> Self {
self.use_tilde = use_tilde;
self
}
pub fn abbreviate(mut self, abbrev: bool) -> Self {
self.abbreviate = abbrev;
self
}
pub fn keep(mut self, count: usize) -> Self {
self.keep_dirs = count;
self
}
pub fn format(&self, path: impl AsRef<Path>) -> String {
let mut result = if self.use_tilde {
home_relative(path.as_ref())
} else {
path.as_ref().display().to_string()
};
if self.abbreviate {
result = abbreviate_path_keep(&result, self.keep_dirs);
}
if let Some(max) = self.max_width {
if result.len() > max {
result = shorten_path(&result, max);
}
}
result
}
}