diff --git a/src/config.rs b/src/config.rs index 36873fe..923a906 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,6 @@ use std::process; use crate::output::*; use super::repo::RepoConfig; - use std::path::Path; use crate::get_token_from_command; @@ -147,6 +146,27 @@ impl Config { }) } + pub fn normalize(&mut self) { + if let Config::ConfigTree(config) = self { + let home = super::env_home().display().to_string(); + for tree in &mut config.trees.0 { + if tree.root.starts_with(&home) { + // The tilde is not handled differently, it's just a normal path component for `Path`. + // Therefore we can treat it like that during **output**. + // + // The `unwrap()` is safe here as we are testing via `starts_with()` + // beforehand + let mut path = tree.root.strip_prefix(&home).unwrap(); + if path.starts_with('/') { + path = path.strip_prefix('/').unwrap(); + } + + tree.root = Path::new("~").join(path).display().to_string(); + } + } + } + } + pub fn as_toml(&self) -> Result { match toml::to_string(self) { Ok(toml) => Ok(toml), diff --git a/src/grm/main.rs b/src/grm/main.rs index 3f202f0..7b5f54e 100644 --- a/src/grm/main.rs +++ b/src/grm/main.rs @@ -198,7 +198,9 @@ fn main() { }) { print_warning("No repositories found"); } else { - let config = trees.to_config(); + let mut config = trees.to_config(); + + config.normalize(); match args.format { cmd::ConfigFormat::Toml => { @@ -403,7 +405,9 @@ fn main() { trees.push(tree); } - let config = config::Config::from_trees(trees); + let mut config = config::Config::from_trees(trees); + + config.normalize(); match args.format { cmd::ConfigFormat::Toml => { diff --git a/src/lib.rs b/src/lib.rs index ef5bf1a..809778c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -487,15 +487,6 @@ pub fn find_in_tree(path: &Path) -> Result<(Tree, Vec), String> { } } } - let home = env_home(); - if root.starts_with(&home) { - // The tilde is not handled differently, it's just a normal path component for `Path`. - // Therefore we can treat it like that during **output**. - // - // The `unwrap()` is safe here as we are testing via `starts_with()` - // beforehand - root = Path::new("~").join(root.strip_prefix(&home).unwrap()); - } Ok(( Tree {