paths.rs

 1use std::path::{Path, PathBuf};
 2
 3lazy_static::lazy_static! {
 4    pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
 5    pub static ref CONFIG_DIR: PathBuf = HOME.join(".config").join("zed");
 6    pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
 7    pub static ref SUPPORT_DIR: PathBuf = HOME.join("Library/Application Support/Zed");
 8    pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
 9    pub static ref COPILOT_DIR: PathBuf = HOME.join("Library/Application Support/Zed/copilot");
10    pub static ref DB_DIR: PathBuf = HOME.join("Library/Application Support/Zed/db");
11    pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
12    pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
13    pub static ref LAST_USERNAME: PathBuf = CONFIG_DIR.join("last-username.txt");
14    pub static ref LOG: PathBuf = LOGS_DIR.join("Zed.log");
15    pub static ref OLD_LOG: PathBuf = LOGS_DIR.join("Zed.log.old");
16}
17
18pub mod legacy {
19    use std::path::PathBuf;
20
21    lazy_static::lazy_static! {
22        static ref CONFIG_DIR: PathBuf = super::HOME.join(".zed");
23        pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
24        pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
25    }
26}
27
28/// Compacts a given file path by replacing the user's home directory
29/// prefix with a tilde (`~`).
30///
31/// # Arguments
32///
33/// * `path` - A reference to a `Path` representing the file path to compact.
34///
35/// # Examples
36///
37/// ```
38/// use std::path::{Path, PathBuf};
39/// use util::paths::compact;
40/// let path: PathBuf = [
41///     util::paths::HOME.to_string_lossy().to_string(),
42///     "some_file.txt".to_string(),
43///  ]
44///  .iter()
45///  .collect();
46/// if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
47///     assert_eq!(compact(&path).to_str(), Some("~/some_file.txt"));
48/// } else {
49///     assert_eq!(compact(&path).to_str(), path.to_str());
50/// }
51/// ```
52///
53/// # Returns
54///
55/// * A `PathBuf` containing the compacted file path. If the input path
56///   does not have the user's home directory prefix, or if we are not on
57///   Linux or macOS, the original path is returned unchanged.
58pub fn compact(path: &Path) -> PathBuf {
59    if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
60        match path.strip_prefix(HOME.as_path()) {
61            Ok(relative_path) => {
62                let mut shortened_path = PathBuf::new();
63                shortened_path.push("~");
64                shortened_path.push(relative_path);
65                shortened_path
66            }
67            Err(_) => path.to_path_buf(),
68        }
69    } else {
70        path.to_path_buf()
71    }
72}