Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Aug 22, 2024
1 parent 843d979 commit e4a2979
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ macro_rules! is_flag_enabled {
};
}

cfg_if::cfg_if! {
if #[cfg(test)] {
const DEFAULT_CONFIG_FILE_PATH: &str = "bottom_this_is_a_test/bottom.toml";
} else {
const DEFAULT_CONFIG_FILE_PATH: &str = "bottom/bottom.toml";
}
}
/// The default config file sub-path.
const DEFAULT_CONFIG_FILE_LOCATION: &str = "bottom/bottom.toml";

/// Returns the config path to use. If `override_config_path` is specified, then
/// we will use that. If not, then return the "default" config path, which is:
Expand All @@ -82,32 +77,39 @@ fn get_config_path(override_config_path: Option<&Path>) -> Option<PathBuf> {
} else if let Some(home_path) = dirs::home_dir() {
let mut old_home_path = home_path;
old_home_path.push(".config/");
old_home_path.push(DEFAULT_CONFIG_FILE_PATH);
if old_home_path.exists() {
// We used to create it at `<HOME>/DEFAULT_CONFIG_FILE_PATH`, but changed it
// to be more correct later. However, for legacy reasons, if it already exists,
// use the old one.
return Some(old_home_path);
old_home_path.push(DEFAULT_CONFIG_FILE_LOCATION);
if let Ok(res) = old_home_path.try_exists() {
if res {
// We used to create it at `<HOME>/DEFAULT_CONFIG_FILE_PATH`, but changed it
// to be more correct later. However, for legacy reasons, if it already exists,
// use the old one.
return Some(old_home_path);
}
}
}

let config_path = dirs::config_dir().map(|mut path| {
path.push(DEFAULT_CONFIG_FILE_PATH);
path.push(DEFAULT_CONFIG_FILE_LOCATION);
path
});

if cfg!(target_os = "macos") {
if let Some(old_macos_path) = &config_path {
if old_macos_path.exists() {
return config_path;
}
}

if let Ok(xdg_config_path) = std::env::var("XDG_CONFIG_HOME") {
if !xdg_config_path.is_empty() {
// If XDG_CONFIG_HOME exists and is non-empty, _but_ we previously used the Library-based path
// for a config and it exists, then use that instead for backwards-compatibility.
if let Some(old_macos_path) = &config_path {
if let Ok(res) = old_macos_path.try_exists() {
if res {
return config_path;
}
}
}

// Otherwise, try and use the XDG_CONFIG_HOME-based path.
let mut cfg_path = PathBuf::new();
cfg_path.push(xdg_config_path);
cfg_path.push(DEFAULT_CONFIG_FILE_PATH);
cfg_path.push(DEFAULT_CONFIG_FILE_LOCATION);

return Some(cfg_path);
}
Expand Down Expand Up @@ -1215,7 +1217,7 @@ mod test {
#[test]
fn test_get_config_path_macos() {
use super::get_config_path;
use super::DEFAULT_CONFIG_FILE_PATH;
use super::DEFAULT_CONFIG_FILE_LOCATION;
use std::path::PathBuf;

// Case three: no previous config, no XDG var.
Expand Down

0 comments on commit e4a2979

Please sign in to comment.