zoxide/src/config.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

2021-09-13 08:01:58 +00:00
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
2021-01-08 15:15:47 +00:00
2022-09-16 16:54:46 +00:00
use anyhow::{Context, Result};
use glob::Pattern;
use crate::db::Rank;
pub fn data_dir() -> Result<PathBuf> {
let path = match env::var_os("_ZO_DATA_DIR") {
Some(path) => PathBuf::from(path),
2022-07-01 09:33:30 +00:00
None => dirs::data_local_dir()
.context("could not find data directory, please set _ZO_DATA_DIR manually")?
.join("zoxide"),
2020-03-27 19:08:36 +00:00
};
Ok(path)
2020-03-27 19:08:36 +00:00
}
pub fn echo() -> bool {
2022-07-01 09:33:30 +00:00
env::var_os("_ZO_ECHO").map_or(false, |var| var == "1")
}
pub fn exclude_dirs() -> Result<Vec<Pattern>> {
2022-07-01 09:33:30 +00:00
match env::var_os("_ZO_EXCLUDE_DIRS") {
Some(paths) => env::split_paths(&paths)
.map(|path| {
let pattern = path.to_str().context("invalid unicode in _ZO_EXCLUDE_DIRS")?;
Pattern::new(pattern)
.with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {pattern}"))
2022-07-01 09:33:30 +00:00
})
.collect(),
None => {
let pattern = (|| {
let home = dirs::home_dir()?;
2022-07-01 09:33:30 +00:00
let home = Pattern::escape(home.to_str()?);
Pattern::new(&home).ok()
})();
Ok(pattern.into_iter().collect())
2022-07-01 09:33:30 +00:00
}
}
}
pub fn fzf_opts() -> Option<OsString> {
env::var_os("_ZO_FZF_OPTS")
}
pub fn maxage() -> Result<Rank> {
2022-07-01 09:33:30 +00:00
env::var_os("_ZO_MAXAGE").map_or(Ok(10_000.0), |maxage| {
let maxage = maxage.to_str().context("invalid unicode in _ZO_MAXAGE")?;
let maxage = maxage
.parse::<u32>()
.with_context(|| format!("unable to parse _ZO_MAXAGE as integer: {maxage}"))?;
2022-07-01 09:33:30 +00:00
Ok(maxage as Rank)
})
2020-03-27 19:08:36 +00:00
}
pub fn resolve_symlinks() -> bool {
2022-07-01 09:33:30 +00:00
env::var_os("_ZO_RESOLVE_SYMLINKS").map_or(false, |var| var == "1")
}