mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-08 08:54:39 +01:00
feat(config): added recognition of $BAT_CONFIG_DIR
This commit is contained in:
parent
f8498b260b
commit
bf78288e9e
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
|
- $BAT_CONFIG_DIR is now a recognized environment variable and has precedent over $XDG_CONFIG_HOME. (@billrisher)
|
||||||
|
|
||||||
## Bugfixes
|
## Bugfixes
|
||||||
|
|
||||||
|
@ -4,8 +4,9 @@ use std::path::{Path, PathBuf};
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
|
/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
|
||||||
/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
|
/// The `XDG_CACHE_HOME` environment variable is checked first. `BAT_CONFIG_DIR`
|
||||||
/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
|
/// is then checked before the `XDG_CONFIG_HOME` environment variable.
|
||||||
|
/// The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
|
||||||
pub struct BatProjectDirs {
|
pub struct BatProjectDirs {
|
||||||
cache_dir: PathBuf,
|
cache_dir: PathBuf,
|
||||||
config_dir: PathBuf,
|
config_dir: PathBuf,
|
||||||
@ -14,17 +15,24 @@ pub struct BatProjectDirs {
|
|||||||
impl BatProjectDirs {
|
impl BatProjectDirs {
|
||||||
fn new() -> Option<BatProjectDirs> {
|
fn new() -> Option<BatProjectDirs> {
|
||||||
let cache_dir = BatProjectDirs::get_cache_dir()?;
|
let cache_dir = BatProjectDirs::get_cache_dir()?;
|
||||||
|
|
||||||
|
// Checks whether or not $BAT_CONFIG_DIR exists. If it doesn't, set our config dir
|
||||||
|
// to our system's default configuration home.
|
||||||
|
let config_dir = if let Some(config_dir_op) = env::var_os("BAT_CONFIG_DIR")
|
||||||
|
.map(PathBuf::from) {
|
||||||
|
config_dir_op
|
||||||
|
} else {
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
let config_dir_op = env::var_os("XDG_CONFIG_HOME")
|
||||||
|
.map(PathBuf::from)
|
||||||
|
.filter(|p| p.is_absolute())
|
||||||
|
.or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(not(target_os = "macos"))]
|
||||||
let config_dir_op = env::var_os("XDG_CONFIG_HOME")
|
let config_dir_op = dirs_next::config_dir();
|
||||||
.map(PathBuf::from)
|
|
||||||
.filter(|p| p.is_absolute())
|
|
||||||
.or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
config_dir_op.map(|d| d.join("bat"))?
|
||||||
let config_dir_op = dirs_next::config_dir();
|
};
|
||||||
|
|
||||||
let config_dir = config_dir_op.map(|d| d.join("bat"))?;
|
|
||||||
|
|
||||||
Some(BatProjectDirs {
|
Some(BatProjectDirs {
|
||||||
cache_dir,
|
cache_dir,
|
||||||
|
@ -747,6 +747,25 @@ fn config_location_when_generating() {
|
|||||||
assert!(tmp_config_path.exists());
|
assert!(tmp_config_path.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn config_location_from_bat_config_dir_variable() {
|
||||||
|
let bat_conf_dir = tempdir().expect("can't create temporary directory.").path().join("bat_conf_dir/");
|
||||||
|
|
||||||
|
// Create file at BAT_CONFIG_DIR
|
||||||
|
bat_with_config()
|
||||||
|
.env("BAT_CONFIG_DIR", bat_conf_dir.to_str().unwrap())
|
||||||
|
.arg("--generate-config-file")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(
|
||||||
|
predicate::str::is_match("Success! Config file written to .*config\n")
|
||||||
|
.unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Ensure generated config file exists.
|
||||||
|
assert!(bat_conf_dir.join("config").exists());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn config_read_arguments_from_file() {
|
fn config_read_arguments_from_file() {
|
||||||
bat_with_config()
|
bat_with_config()
|
||||||
|
Loading…
Reference in New Issue
Block a user