Implement _ZO_EXCLUDE_DIRS

_ZO_EXCLUDE_DIRS is a list of paths (separated by colons, `:`, on
Unix-based systems, and semicolons, `;`, on Windows) that should be
excluded from the database. Example:

    _ZO_EXCLUDE_DIRS="$HOME:$HOME/something/super/secret:$HOME/caused/by/background/cds"
This commit is contained in:
Cole Helbling 2020-03-27 14:56:48 -07:00 committed by Ajeet D'Souza
parent f29b642ffc
commit 1190106849
2 changed files with 22 additions and 9 deletions

View File

@ -1,6 +1,7 @@
use crate::types::Rank;
use anyhow::{bail, Context, Result};
use std::env;
use std::fs;
use std::path::PathBuf;
@ -25,6 +26,13 @@ pub fn zo_data() -> Result<PathBuf> {
Ok(path)
}
pub fn zo_exclude_dirs() -> Vec<PathBuf> {
match env::var_os("_ZO_EXCLUDE_DIRS") {
Some(dirs_osstr) => env::split_paths(&dirs_osstr).collect(),
None => Vec::new(),
}
}
pub fn zo_maxage() -> Result<Rank> {
match env::var_os("_ZO_MAXAGE") {
Some(maxage_osstr) => match maxage_osstr.to_str() {

View File

@ -2,13 +2,15 @@ use crate::config;
use crate::util;
use anyhow::{Context, Result};
use std::env;
use structopt::StructOpt;
use std::env;
use std::path::PathBuf;
#[derive(Debug, StructOpt)]
#[structopt(about = "Add a new directory or increment its rank")]
pub struct Add {
path: Option<String>,
path: Option<PathBuf>,
}
impl Add {
@ -16,14 +18,17 @@ impl Add {
let mut db = util::get_db()?;
let now = util::get_current_time()?;
let maxage = config::zo_maxage()?;
let excluded_dirs = config::zo_exclude_dirs();
match &self.path {
Some(path) => db.add(path, maxage, now),
None => {
let current_dir =
env::current_dir().context("unable to fetch current directory")?;
db.add(current_dir, maxage, now)
}
let path = match &self.path {
Some(path) => path.clone(),
None => env::current_dir().context("unable to fetch current directory")?,
};
if excluded_dirs.contains(&path) {
return Ok(());
}
db.add(path, maxage, now)
}
}