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