Skip to content

Exact path matching command line option #980

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 1 commit 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
2 changes: 2 additions & 0 deletions contrib/completions/_zoxide

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

2 changes: 2 additions & 0 deletions contrib/completions/_zoxide.ps1

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

2 changes: 1 addition & 1 deletion contrib/completions/zoxide.bash

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

2 changes: 2 additions & 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.

4 changes: 4 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 match exact

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd spend a bit more time explaining this feature. This is what will show up in --help

#[clap(long, short)]
pub exact: bool,
}

/// 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_exact(self.exact);
if !self.all {
let resolve_symlinks = config::resolve_symlinks();
options = options.with_exists(true).with_resolve_symlinks(resolve_symlinks);
Expand Down
43 changes: 43 additions & 0 deletions src/db/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::ffi::OsString;
use std::iter::Rev;
use std::ops::Range;
use std::path::{Component, PathBuf};
use std::{fs, path};

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

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

let dir = &self.db.dirs()[idx];
return Some(dir);
}
Expand Down Expand Up @@ -91,6 +97,34 @@ 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_exact(&self, path: &str) -> bool {
if !self.options.exact {
return true;
}

let path = util::to_lowercase(path);
let path = PathBuf::from(path);
let mut components: Vec<Component> = path.components().collect();

for keyword in self.options.keywords.iter().rev().map(OsString::from) {
let idx = components.iter().rposition(|component| {
if let Component::Normal(sub_path) = component {
sub_path == &keyword
} else {
false
}
});

if let Some(idx) = idx {
components = components.drain(0..idx).collect();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This... is convoluted to say the least.

} else {
return false;
};
}

true
}
}

pub struct StreamOptions {
Expand All @@ -109,6 +143,9 @@ pub struct StreamOptions {
/// Whether to resolve symlinks when checking if a directory exists.
resolve_symlinks: bool,

// Whether to only allow exact match of directory names.
exact: bool,

/// Directories that do not exist and haven't been accessed since TTL will
/// be lazily removed.
ttl: Epoch,
Expand All @@ -122,6 +159,7 @@ impl StreamOptions {
exclude: Vec::new(),
exists: false,
resolve_symlinks: false,
exact: false,
ttl: now.saturating_sub(3 * MONTH),
}
}
Expand Down Expand Up @@ -149,6 +187,11 @@ impl StreamOptions {
self.resolve_symlinks = resolve_symlinks;
self
}

pub fn with_exact(mut self, exact: bool) -> Self {
self.exact = exact;
self
}
}

#[cfg(test)]
Expand Down
Loading