diff --git a/src/path_shared.rs b/src/path_shared.rs index 6cb2a61..90be80d 100644 --- a/src/path_shared.rs +++ b/src/path_shared.rs @@ -1,9 +1,13 @@ use std::hash::{Hash, Hasher}; -use std::path::Display; +// use std::path::Display; +use std::fmt; use std::path::Path; use std::path::PathBuf; +use std::path::MAIN_SEPARATOR; use std::sync::Arc; +use crate::util::path_home; + /// As a normal Arc-wrapped PathBuf cannot be a key in a mapping or set, we create this wrapped Arc PathBuf that implements hashability. Cloning this type will increment the reference count. #[derive(Debug, Clone)] pub(crate) struct PathShared(Arc); @@ -30,9 +34,9 @@ impl PathShared { self.0.join(part) } - pub(crate) fn display(&self) -> Display { - self.0.display() - } + // pub(crate) fn display(&self) -> Display { + // self.0.display() + // } } impl PartialEq for PathShared { @@ -41,6 +45,19 @@ impl PartialEq for PathShared { } } +/// Specialized Path display that replaces home directories with `~` +impl fmt::Display for PathShared { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(home) = path_home() { + let pre = Path::new(&home); + if let Ok(post) = self.0.strip_prefix(&pre) { + return write!(f, "~{}{}", MAIN_SEPARATOR, post.display()); + } + } + write!(f, "{}", self.0.display()) + } +} + impl Eq for PathShared {} impl Hash for PathShared { @@ -77,7 +94,7 @@ mod tests { #[test] fn test_b() { let path1 = PathShared::from_str("/home/user1"); - assert_eq!(format!("{}", path1.display()), "/home/user1"); + assert_eq!(format!("{}", path1.to_string()), "/home/user1"); } #[test] diff --git a/src/scan_report.rs b/src/scan_report.rs index c863c7e..209fd93 100644 --- a/src/scan_report.rs +++ b/src/scan_report.rs @@ -31,7 +31,7 @@ impl Rowable for ScanRecord { } else { pkg_display.clone() }; - rows.push(vec![p, path.display().to_string()]); + rows.push(vec![p, path.to_string()]); } rows } diff --git a/src/unpack_report.rs b/src/unpack_report.rs index 648a852..8f35c78 100644 --- a/src/unpack_report.rs +++ b/src/unpack_report.rs @@ -126,7 +126,7 @@ impl Rowable for UnpackFullRecord { let mut site_display = || { if !is_tty || !site_set { site_set = true; - self.site.display().to_string() + self.site.to_string() } else { "".to_string() } @@ -176,7 +176,7 @@ impl Rowable for UnpackCountRecord { fn to_rows(&self, _context: &RowableContext) -> Vec> { vec![vec![ self.package.to_string(), - self.site.display().to_string(), + self.site.to_string(), self.artifacts.files.len().to_string(), self.artifacts.dirs.len().to_string(), ]] diff --git a/src/validation_report.rs b/src/validation_report.rs index e1935d3..908fe0a 100644 --- a/src/validation_report.rs +++ b/src/validation_report.rs @@ -85,7 +85,7 @@ impl Rowable for ValidationRecord { let sites_display = match &self.sites { Some(sites) => sites .iter() - .map(|s| format!("{}", s.display())) + .map(|s| format!("{}", s.to_string())) .collect::>() .join(","), None => "".to_string(), @@ -141,7 +141,7 @@ impl ValidationReport { Some(sites) => Some( sites .iter() - .map(|s| format!("{}", s.display())) + .map(|s| format!("{}", s.to_string())) .collect::>(), ), None => None,