Added option to query only within current directory

This commit is contained in:
Priyanshu Verma 2022-10-02 20:35:37 +05:30
parent c3e3c855ca
commit 8c03027923
3 changed files with 21 additions and 0 deletions

View File

@ -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<String>,

View File

@ -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)?;

View File

@ -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<Range<usize>>,
@ -14,6 +16,7 @@ pub struct Stream<'db, 'file> {
check_exists: bool,
expire_below: Epoch,
resolve_symlinks: bool,
workingdir: bool,
exclude_path: Option<String>,
}
@ -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);
}