diff --git a/src/cmd/cmd.rs b/src/cmd/cmd.rs index 59775c6..1e8fd42 100644 --- a/src/cmd/cmd.rs +++ b/src/cmd/cmd.rs @@ -117,6 +117,10 @@ pub struct Query { #[clap(long, short)] pub score: bool, + /// Search only through current working directory + #[clap(long, short)] + pub workingdir: bool, + /// Exclude a path from results #[clap(long, value_hint = ValueHint::DirPath, value_name = "path")] pub exclude: Option, diff --git a/src/cmd/query.rs b/src/cmd/query.rs index b6a9657..a5d7487 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -29,6 +29,9 @@ impl Query { if let Some(path) = &self.exclude { stream = stream.with_exclude(path); } + if self.workingdir { + stream = stream.with_workingdir(self.workingdir); + } if self.interactive { let mut fzf = Fzf::new(false)?; diff --git a/src/db/stream.rs b/src/db/stream.rs index 275d52e..2ec8deb 100644 --- a/src/db/stream.rs +++ b/src/db/stream.rs @@ -5,6 +5,8 @@ use std::{fs, path}; use crate::db::{Database, Dir, Epoch}; use crate::util; +use std::env; + pub struct Stream<'db, 'file> { db: &'db mut Database<'file>, idxs: Rev>, @@ -14,6 +16,7 @@ pub struct Stream<'db, 'file> { check_exists: bool, expire_below: Epoch, resolve_symlinks: bool, + workingdir: bool, exclude_path: Option, } @@ -34,6 +37,7 @@ impl<'db, 'file> Stream<'db, 'file> { check_exists: false, expire_below, resolve_symlinks: false, + workingdir: false, exclude_path: None, } } @@ -43,6 +47,11 @@ impl<'db, 'file> Stream<'db, 'file> { self } + pub fn with_workingdir(mut self, workingdir: bool) -> Self { + self.workingdir = workingdir; + self + } + pub fn with_exists(mut self, resolve_symlinks: bool) -> Self { self.check_exists = true; self.resolve_symlinks = resolve_symlinks; @@ -74,6 +83,11 @@ impl<'db, 'file> Stream<'db, 'file> { continue; } + // Check if working directory only mode is on and do the check if it is a sub directory of the working directory + if self.workingdir && !dir.path.starts_with(env::current_dir().unwrap().to_str().unwrap()) { + continue; + } + let dir = &self.db.dirs[idx]; return Some(dir); }