From 3be75b1bb43a5c325fe26cb5162f877f20758ea6 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Fri, 27 Mar 2020 14:56:48 -0700 Subject: [PATCH 1/2] 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" --- src/config.rs | 8 ++++++++ src/subcommand/add.rs | 23 ++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index cdde196..39a8ceb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 { Ok(path) } +pub fn zo_exclude_dirs() -> Vec { + match env::var_os("_ZO_EXCLUDE_DIRS") { + Some(dirs_osstr) => env::split_paths(&dirs_osstr).collect(), + None => Vec::new(), + } +} + pub fn zo_maxage() -> Result { match env::var_os("_ZO_MAXAGE") { Some(maxage_osstr) => match maxage_osstr.to_str() { diff --git a/src/subcommand/add.rs b/src/subcommand/add.rs index 71550ee..c3c4b22 100644 --- a/src/subcommand/add.rs +++ b/src/subcommand/add.rs @@ -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, + path: Option, } 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) } } From c49375e135492af954b499b21f67990976ca9587 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Fri, 27 Mar 2020 23:06:25 -0700 Subject: [PATCH 2/2] Document _ZO_EXCLUDE_DIRS env var --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d5f1f82..3693075 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,9 @@ zoxide init fish | source ### Environment variables -- `$_ZO_ECHO`: `z` will print the matched directory before navigating to it - `$_ZO_DATA`: sets the location of the database (default: `~/.zo`) +- `$_ZO_ECHO`: `z` will print the matched directory before navigating to it +- `$_ZO_EXCLUDE_DIRS`: list of directories separated by platform-specific + characters (`:` on Linux and macOS, and `;` on Windows) to be excluded from + the database - `$_ZO_MAXAGE`: sets the maximum total rank after which entries start getting deleted