Skip to content
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
29 changes: 29 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fastrand = "2.0.0"
glob = "0.3.0"
ouroboros = "0.18.3"
serde = { version = "1.0.116", features = ["derive"] }
walkdir = "2.5.0"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.30.1", default-features = false, features = [
Expand Down
4 changes: 4 additions & 0 deletions contrib/completions/_zoxide

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.ps1

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

4 changes: 2 additions & 2 deletions contrib/completions/zoxide.bash

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.elv

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.fish

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.nu

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

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

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

13 changes: 10 additions & 3 deletions src/cmd/add.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};

use anyhow::{Result, bail};

Expand All @@ -18,10 +18,17 @@ impl Run for Add {

let mut db = Database::open()?;

for path in &self.paths {
// Build a unified iterator of PathBufs, whether recursive or not.
let paths: Box<dyn Iterator<Item = PathBuf>> = if self.recursive {
Box::new(self.paths.iter().flat_map(|p| util::walk_dir(p)))
} else {
Box::new(self.paths.iter().cloned())
};

for path in paths {
let path =
if config::resolve_symlinks() { util::canonicalize } else { util::resolve_path }(
path,
&path,
)?;
let path = util::path_to_str(&path)?;

Expand Down
8 changes: 8 additions & 0 deletions src/cmd/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ pub struct Add {
/// doesn't
#[clap(short, long)]
pub score: Option<f64>,

/// Recursively add directories
#[clap(short, long, default_value_t = false)]
pub recursive: bool,
}

/// Edit the database
Expand Down Expand Up @@ -201,4 +205,8 @@ pub struct Query {
pub struct Remove {
#[clap(value_hint = ValueHint::DirPath)]
pub paths: Vec<String>,

/// Recursively remove directories
#[clap(short, long, default_value_t = false)]
pub recursive: bool,
}
2 changes: 1 addition & 1 deletion src/cmd/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Run for Edit {
match cmd {
EditCommand::Decrement { path } => db.add(path, -1.0, now),
EditCommand::Delete { path } => {
db.remove(path);
db.remove(path, false);
}
EditCommand::Increment { path } => db.add(path, 1.0, now),
EditCommand::Reload => {}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ impl Run for Remove {
let mut db = Database::open()?;

for path in &self.paths {
if !db.remove(path) {
if !db.remove(path, self.recursive) {
let path_abs = util::resolve_path(path)?;
let path_abs = util::path_to_str(&path_abs)?;
if path_abs == path || !db.remove(path_abs) {
if path_abs == path || !db.remove(path_abs, self.recursive) {
bail!("path not found in database: {path}")
}
}
Expand Down
36 changes: 27 additions & 9 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,35 @@ impl Database {
}

/// Removes the directory with `path` from the store. This does not preserve
/// ordering, but is O(1).
pub fn remove(&mut self, path: impl AsRef<str>) -> bool {
match self.dirs().iter().position(|dir| dir.path == path.as_ref()) {
Some(idx) => {
self.swap_remove(idx);
true
/// ordering, but is O(1). If recursive, this will remove all directories
/// starting with `path`. This is O(n)
pub fn remove(&mut self, path: impl AsRef<str>, recursive: bool) -> bool {
if recursive {
self.remove_recursive(path)
} else {
match self.dirs().iter().position(|dir| dir.path == path.as_ref()) {
Some(idx) => {
self.swap_remove(idx);
true
}
None => false,
}
None => false,
}
}

pub fn remove_recursive(&mut self, path: impl AsRef<str>) -> bool {
let mut removed = false;
self.with_dirs_mut(|dirs| {
dirs.retain(|dir| {
let keep = !dir.path.starts_with(path.as_ref());
removed |= !keep;
keep
});
});
self.with_dirty_mut(|dirty| *dirty = removed);
removed
}

pub fn swap_remove(&mut self, idx: usize) {
self.with_dirs_mut(|dirs| dirs.swap_remove(idx));
self.with_dirty_mut(|dirty| *dirty = true);
Expand Down Expand Up @@ -273,14 +291,14 @@ mod tests {

{
let mut db = Database::open_dir(data_dir.path()).unwrap();
assert!(db.remove(path));
assert!(db.remove(path, false));
db.save().unwrap();
}

{
let mut db = Database::open_dir(data_dir.path()).unwrap();
assert!(db.dirs().is_empty());
assert!(!db.remove(path));
assert!(!db.remove(path, false));
db.save().unwrap();
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{env, mem};
#[cfg(windows)]
use anyhow::anyhow;
use anyhow::{Context, Result, bail};
use walkdir::WalkDir;

use crate::db::{Dir, Epoch};
use crate::error::SilentExit;
Expand Down Expand Up @@ -382,3 +383,12 @@ pub fn to_lowercase(s: impl AsRef<str>) -> String {
let s = s.as_ref();
if s.is_ascii() { s.to_ascii_lowercase() } else { s.to_lowercase() }
}

/// Recursively walk a directory and yield all directories.
pub fn walk_dir(path: &Path) -> impl Iterator<Item = PathBuf> {
WalkDir::new(path)
.into_iter()
.filter_map(|p| p.ok())
.filter(|p| p.file_type().is_dir())
.map(|p| p.into_path())
}
Loading