Skip to content

Commit

Permalink
implemetned home path replacement in PathShared display
Browse files Browse the repository at this point in the history
  • Loading branch information
flexatone committed Nov 8, 2024
1 parent f1243f6 commit 57d31e8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
27 changes: 22 additions & 5 deletions src/path_shared.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>);
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/scan_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/unpack_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Rowable for UnpackCountRecord {
fn to_rows(&self, _context: &RowableContext) -> Vec<Vec<String>> {
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(),
]]
Expand Down
4 changes: 2 additions & 2 deletions src/validation_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.join(","),
None => "".to_string(),
Expand Down Expand Up @@ -141,7 +141,7 @@ impl ValidationReport {
Some(sites) => Some(
sites
.iter()
.map(|s| format!("{}", s.display()))
.map(|s| format!("{}", s.to_string()))
.collect::<Vec<_>>(),
),
None => None,
Expand Down

0 comments on commit 57d31e8

Please sign in to comment.