From 9758f93d932ef8583c625eeca5563b03f69143b5 Mon Sep 17 00:00:00 2001 From: Elisabeth Henry Date: Fri, 16 Dec 2016 01:06:49 +0100 Subject: [PATCH] Remove current directory from begginnig of displayed paths --- src/lib/misc.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/misc.rs b/src/lib/misc.rs index 8bc132a..c1b8f7e 100644 --- a/src/lib/misc.rs +++ b/src/lib/misc.rs @@ -19,13 +19,22 @@ use std; use std::path::Path; +use std::io::Result; /// Try to canonicalize a path using std::fs::canonicalize, and returns the /// unmodified path if it fails (e.g. if the path doesn't exist (yet)) pub fn canonicalize>(path: P) -> String { - if let Ok(path) = std::fs::canonicalize(path.as_ref()) { + try_canonicalize(path.as_ref()) + .unwrap_or(format!("{}", path.as_ref().display())) +} + + +fn try_canonicalize>(path: P) -> Result { + let path = std::fs::canonicalize(path.as_ref())?; + let cwd = std::env::current_dir()?; + Ok(if let Ok(path) = path.strip_prefix(&cwd) { format!("{}", path.display()) } else { - format!("{}", path.as_ref().display()) - } + format!("{}", path.display()) + }) }