diff --git a/src/git/worktree.rs b/src/git/worktree.rs index 609423f..3218ec7 100644 --- a/src/git/worktree.rs +++ b/src/git/worktree.rs @@ -378,14 +378,12 @@ pub struct Worktree { impl Display for Worktree { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let path = NormalPath::from_cwd(&self.path) - .map(|path| path.to_string()) - .unwrap_or_else(|_| { - self.path - .if_supports_color(Stream::Stdout, |text| text.cyan()) - .to_string() - }); - write!(f, "{path} {}", self.head)?; + write!( + f, + "{} {}", + NormalPath::try_display_cwd(&self.path), + self.head + )?; if self.is_main { write!( diff --git a/src/normal_path.rs b/src/normal_path.rs index 6c74be3..cfc3d60 100644 --- a/src/normal_path.rs +++ b/src/normal_path.rs @@ -12,6 +12,7 @@ use common_path::common_path; use miette::miette; use miette::IntoDiagnostic; use owo_colors::OwoColorize; +use owo_colors::Stream; use owo_colors::Stream::Stdout; use path_absolutize::Absolutize; @@ -33,6 +34,17 @@ pub struct NormalPath { } impl NormalPath { + pub fn try_display_cwd(original: impl AsRef) -> String { + let original = original.as_ref(); + Self::from_cwd(original) + .map(|normal_path| normal_path.to_string()) + .unwrap_or_else(|_| { + original + .if_supports_color(Stream::Stdout, |text| text.cyan()) + .to_string() + }) + } + /// Creates a new normalized path relative to the given base path. pub fn new(original: impl AsRef, base: impl AsRef) -> miette::Result { let base = base.as_ref();