mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-29 05:33:49 +00:00
Introduce compact function in util create
This commit is contained in:
parent
d5f53111e8
commit
69989d0463
1 changed files with 53 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
|
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
|
||||||
|
@ -23,3 +23,55 @@ pub mod legacy {
|
||||||
pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
|
pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Compacts a given file path by replacing the user's home directory
|
||||||
|
/// prefix with a tilde (`~`).
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `path` - A reference to a `Path` representing the file path to compact.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::path::{Path, PathBuf};
|
||||||
|
/// use util::paths::compact;
|
||||||
|
/// let path: PathBuf = [
|
||||||
|
/// util::paths::HOME.to_string_lossy().to_string(),
|
||||||
|
/// "some_file.txt".to_string(),
|
||||||
|
/// ]
|
||||||
|
/// .iter()
|
||||||
|
/// .collect();
|
||||||
|
/// if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||||
|
/// assert_eq!(compact(&path).to_str(), Some("~/some_file.txt"));
|
||||||
|
/// } else {
|
||||||
|
/// assert_eq!(compact(&path).to_str(), path.to_str());
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * A `PathBuf` containing the compacted file path. If the input path
|
||||||
|
/// does not have the user's home directory prefix, or if we are not on
|
||||||
|
/// Linux or macOS, the original path is returned unchanged.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// This function will not produce any errors, but in case the input path
|
||||||
|
/// cannot be stripped of the home directory prefix, the original path
|
||||||
|
/// will be returned unchanged.
|
||||||
|
pub fn compact(path: &Path) -> PathBuf {
|
||||||
|
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||||
|
match path.strip_prefix(HOME.as_path()) {
|
||||||
|
Ok(relative_path) => {
|
||||||
|
let mut shortened_path = PathBuf::new();
|
||||||
|
shortened_path.push("~");
|
||||||
|
shortened_path.push(relative_path);
|
||||||
|
shortened_path
|
||||||
|
}
|
||||||
|
Err(_) => path.to_path_buf(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
path.to_path_buf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue