-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to homedir since tilde_expand is old
- Loading branch information
Showing
4 changed files
with
154 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "rytm-external" | ||
version = "0.1.0" | ||
authors = [ "Ali Somay <[email protected]>" ] | ||
authors = ["Ali Somay <[email protected]>"] | ||
edition = "2021" | ||
|
||
[lib] | ||
|
@@ -25,5 +25,4 @@ tracing-core = "0.1" | |
parking_lot = "0.12" | ||
tracing-error = "0.2" | ||
camino = "1" | ||
tilde-expand = "0.1.1" | ||
|
||
homedir = "0.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,53 @@ | ||
use camino::Utf8PathBuf; | ||
use tilde_expand::tilde_expand; | ||
use homedir::my_home; | ||
use median::object::MaxObj; | ||
use tracing::{debug, instrument, warn}; | ||
|
||
pub fn make_utf8_path_buf_respect_tilde(path_candidate: &str) -> Utf8PathBuf { | ||
Utf8PathBuf::from( | ||
String::from_utf8_lossy(&tilde_expand(path_candidate.as_bytes())).into_owned(), | ||
) | ||
use crate::{traits::Post, RytmExternal}; | ||
|
||
impl RytmExternal { | ||
#[instrument( | ||
skip(self), | ||
fields( | ||
path_candidate, | ||
home_dir, | ||
home_dir_str, | ||
path_without_tilde, | ||
expanded_path | ||
) | ||
)] | ||
pub fn make_utf8_path_buf_respect_tilde(&self, path_candidate: &str) -> Utf8PathBuf { | ||
let span = tracing::Span::current(); | ||
if path_candidate.starts_with('~') { | ||
// Attempt to get the user's home directory | ||
if let Some(home_dir) = my_home().ok().flatten() { | ||
span.record("home_dir", home_dir.to_string_lossy().to_string()); | ||
|
||
if let Some(home_dir_str) = home_dir.to_str() { | ||
span.record("home_dir_str", home_dir_str); | ||
// Replace the leading '~' with the home directory | ||
|
||
let path_without_tilde = path_candidate.trim_start_matches('~'); | ||
span.record("path_without_tilde", path_without_tilde); | ||
|
||
let expanded_path = format!("{home_dir_str}{path_without_tilde}"); | ||
span.record("expanded_path", &expanded_path); | ||
|
||
debug!("Expanded path with home directory"); | ||
return Utf8PathBuf::from(expanded_path); | ||
} | ||
} | ||
|
||
let warning = "Failed to get home directory, the path will be returned as is"; | ||
warn!("{}", warning); | ||
warning.obj_warn(self.max_obj()); | ||
self.send_status_warning(); | ||
|
||
// If we can't get the home directory, return the original path | ||
return Utf8PathBuf::from(path_candidate); | ||
} | ||
|
||
debug!("Path does not start with '~', returning as is"); | ||
Utf8PathBuf::from(path_candidate) | ||
} | ||
} |