Skip to content
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

zoxide with file paths #1017

Open
wants to merge 5 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
21 changes: 20 additions & 1 deletion src/cmd/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ 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_keywords(self.transformed_keywords())
.with_exclude(config::exclude_dirs()?);
if !self.all {
let resolve_symlinks = config::resolve_symlinks();
Expand Down Expand Up @@ -117,4 +117,23 @@ impl Query {
}
.spawn()
}

/// ## Returns
/// `self.keywords` with file paths transformed into
/// their parent directory paths, directory paths unchanged
///
/// ## Notes
/// - Heap allocates an iterator of Strings with length of self.keywords
/// - Only uses sans-IO Path methods
fn transformed_keywords(&self) -> impl Iterator<Item = String> + use<'_> {
self.keywords.iter().map(|keyword| {
let path = std::path::Path::new(keyword);
match path.parent() {
None => keyword,
Some(path) if path.to_str() == Some("") => keyword,
Some(path) => path.to_str().unwrap(),
}
.to_string()
})
}
}
6 changes: 3 additions & 3 deletions src/db/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ impl<'a> Stream<'a> {
}

fn filter_by_keywords(&self, path: &str) -> bool {
let (keywords_last, keywords) = match self.options.keywords.split_last() {
let (last_keyword, keywords) = match self.options.keywords.split_last() {
Some(split) => split,
None => return true,
};

let path = util::to_lowercase(path);
let mut path = path.as_str();
match path.rfind(keywords_last) {
match path.rfind(last_keyword) {
Some(idx) => {
if path[idx + keywords_last.len()..].contains(path::is_separator) {
if path[idx + last_keyword.len()..].contains(path::is_separator) {
return false;
}
path = &path[..idx];
Expand Down
Loading