1
1
mirror of https://github.com/wez/wezterm.git synced 2024-09-11 22:37:11 +03:00

Improve XDG configuration searching

In addition to XDG_CONFIG_HOME, use XDG_CONFIG_DIRS
(https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
to append paths for searching for configuration files.
This commit is contained in:
Jared Baur 2023-02-23 12:19:17 -08:00 committed by Wez Furlong
parent 78e8d90e64
commit 2408367e6f
4 changed files with 31 additions and 9 deletions

View File

@ -23,7 +23,7 @@ use crate::wsl::WslDomain;
use crate::{
default_config_with_overrides_applied, default_one_point_oh, default_one_point_oh_f64,
default_true, GpuInfo, KeyMapPreference, LoadedConfig, MouseEventTriggerMods, RgbaColor,
WebGpuPowerPreference, CONFIG_DIR, CONFIG_FILE_OVERRIDE, CONFIG_OVERRIDES, CONFIG_SKIP,
WebGpuPowerPreference, CONFIG_DIRS, CONFIG_FILE_OVERRIDE, CONFIG_OVERRIDES, CONFIG_SKIP,
HOME_DIR,
};
use anyhow::Context;
@ -780,10 +780,11 @@ impl Config {
// multiple. In addition, it spawns a lot of subprocesses,
// so we do this bit "by-hand"
let mut paths = vec![
PathPossibility::optional(CONFIG_DIR.join("wezterm.lua")),
PathPossibility::optional(HOME_DIR.join(".wezterm.lua")),
];
let mut paths = vec![PathPossibility::optional(HOME_DIR.join(".wezterm.lua"))];
for dir in CONFIG_DIRS.iter() {
paths.push(PathPossibility::optional(dir.join("wezterm.lua")))
}
if cfg!(windows) {
// On Windows, a common use case is to maintain a thumb drive
// with a set of portable tools that don't need to be installed
@ -1178,7 +1179,9 @@ impl Config {
fn compute_color_scheme_dirs(&self) -> Vec<PathBuf> {
let mut paths = self.color_scheme_dirs.clone();
paths.push(CONFIG_DIR.join("colors"));
for dir in CONFIG_DIRS.iter() {
paths.push(dir.join("colors"));
}
if cfg!(windows) {
// See commentary re: portable tools above!
if let Ok(exe_name) = std::env::current_exe() {

View File

@ -61,7 +61,7 @@ type ErrorCallback = fn(&str);
lazy_static! {
pub static ref HOME_DIR: PathBuf = dirs_next::home_dir().expect("can't find HOME dir");
pub static ref CONFIG_DIR: PathBuf = xdg_config_home();
pub static ref CONFIG_DIRS: Vec<PathBuf> = config_dirs();
pub static ref RUNTIME_DIR: PathBuf = compute_runtime_dir().unwrap();
static ref CONFIG: Configuration = Configuration::new();
static ref CONFIG_FILE_OVERRIDE: Mutex<Option<PathBuf>> = Mutex::new(None);
@ -337,6 +337,18 @@ fn xdg_config_home() -> PathBuf {
}
}
fn config_dirs() -> Vec<PathBuf> {
let mut dirs = Vec::new();
dirs.push(xdg_config_home());
#[cfg(unix)]
if let Some(d) = std::env::var_os("XDG_CONFIG_DIRS") {
dirs.extend(std::env::split_paths(&d).map(|s| PathBuf::from(s).join("wezterm")));
}
dirs
}
pub fn set_config_file_override(path: &Path) {
CONFIG_FILE_OVERRIDE
.lock()

View File

@ -247,7 +247,9 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
}
prefix_path(&mut path_array, &crate::HOME_DIR.join(".wezterm"));
prefix_path(&mut path_array, &crate::CONFIG_DIR);
for dir in crate::CONFIG_DIRS.iter() {
prefix_path(&mut path_array, dir);
}
path_array.insert(
2,
format!("{}/plugins/?/plugin/init.lua", crate::RUNTIME_DIR.display()),

View File

@ -73,7 +73,12 @@ pub fn fixup_appimage() {
clean_wezterm_config_env();
}
if config::CONFIG_DIR.starts_with(append_extra_file_name_suffix(&appimage, ".config")) {
if std::env::var("XDG_CONFIG_HOME")
.map(|d| {
PathBuf::from(d).starts_with(append_extra_file_name_suffix(&appimage, ".config"))
})
.unwrap_or_default()
{
std::env::remove_var("XDG_CONFIG_HOME");
clean_wezterm_config_env();
}