Skip to content

--basedir query option #1027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/completions/_zoxide

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contrib/completions/_zoxide.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion contrib/completions/zoxide.bash

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contrib/completions/zoxide.elv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contrib/completions/zoxide.fish

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions contrib/completions/zoxide.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/cmd/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ pub struct Query {
/// Exclude the current directory
#[clap(long, value_hint = ValueHint::DirPath, value_name = "path")]
pub exclude: Option<String>,

/// Only search within this directory
#[clap(long, value_hint = ValueHint::DirPath, value_name = "path", conflicts_with = "exclude")]
pub basedir: Option<String>,
}

/// Remove a directory from the database
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl Query {
fn get_stream<'a>(&self, db: &'a mut Database, now: Epoch) -> Result<Stream<'a>> {
let mut options = StreamOptions::new(now)
.with_keywords(self.keywords.iter().map(|s| s.as_str()))
.with_exclude(config::exclude_dirs()?);
.with_exclude(config::exclude_dirs()?)
.with_basedir(self.basedir.clone());
if !self.all {
let resolve_symlinks = config::resolve_symlinks();
options = options.with_exists(true).with_resolve_symlinks(resolve_symlinks);
Expand Down
26 changes: 25 additions & 1 deletion src/db/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::iter::Rev;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::{fs, path};

use glob::Pattern;
Expand Down Expand Up @@ -40,6 +41,10 @@ impl<'a> Stream<'a> {
continue;
}

if !self.filter_by_basedir(&dir.path) {
continue;
}

let dir = &self.db.dirs()[idx];
return Some(dir);
}
Expand Down Expand Up @@ -91,6 +96,15 @@ impl<'a> Stream<'a> {
if self.options.resolve_symlinks { fs::symlink_metadata } else { fs::metadata };
resolver(path).map(|metadata| metadata.is_dir()).unwrap_or_default()
}

fn filter_by_basedir(&self, path: &str) -> bool {
if let Some(basedir) = &self.options.basedir {
let path = Path::new(path);
return path.starts_with(basedir);
}

true
}
}

pub struct StreamOptions {
Expand All @@ -112,6 +126,10 @@ pub struct StreamOptions {
/// Directories that do not exist and haven't been accessed since TTL will
/// be lazily removed.
ttl: Epoch,

/// Only return directories within this parent directory
/// Does not check if the path exists
basedir: Option<String>,
}

impl StreamOptions {
Expand All @@ -123,6 +141,7 @@ impl StreamOptions {
exists: false,
resolve_symlinks: false,
ttl: now.saturating_sub(3 * MONTH),
basedir: None,
}
}

Expand All @@ -149,6 +168,11 @@ impl StreamOptions {
self.resolve_symlinks = resolve_symlinks;
self
}

pub fn with_basedir(mut self, basedir: Option<String>) -> Self {
self.basedir = basedir;
self
}
}

#[cfg(test)]
Expand Down Expand Up @@ -180,7 +204,7 @@ mod tests {
#[case(&["/foo/", "/bar"], "/foo/bar", false)]
#[case(&["/foo/", "/bar"], "/foo/baz/bar", true)]
fn query(#[case] keywords: &[&str], #[case] path: &str, #[case] is_match: bool) {
let db = &mut Database::new(PathBuf::new(), Vec::new(), |_| Vec::new(), false);
let db = &mut Database::new(PathBuf::default(), Vec::default(), |_| Vec::default(), false);
let options = StreamOptions::new(0).with_keywords(keywords.iter());
let stream = Stream::new(db, options);
assert_eq!(is_match, stream.filter_by_keywords(path));
Expand Down
Loading