Skip to content

Commit

Permalink
Add NormalPath::try_display_cwd (#28)
Browse files Browse the repository at this point in the history
For #21
  • Loading branch information
9999years authored Oct 15, 2024
1 parent 4e8746f commit dee5e77
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/git/worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
12 changes: 12 additions & 0 deletions src/normal_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,6 +34,17 @@ pub struct NormalPath {
}

impl NormalPath {
pub fn try_display_cwd(original: impl AsRef<Utf8Path>) -> 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<Path>, base: impl AsRef<Utf8Path>) -> miette::Result<Self> {
let base = base.as_ref();
Expand Down

0 comments on commit dee5e77

Please sign in to comment.