From 0a9081383df4a4a28d9418cf5a1bb7070b6262f6 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Fri, 24 Dec 2021 17:49:36 +0530 Subject: [PATCH] Fix interactive remove --- README.md | 3 +- contrib/completions/_zoxide | 4 +-- contrib/completions/_zoxide.ps1 | 4 +-- contrib/completions/zoxide.bash | 8 ----- contrib/completions/zoxide.elv | 4 +-- contrib/completions/zoxide.fish | 2 +- contrib/completions/zoxide.ts | 5 +-- src/app/_app.rs | 12 +++---- src/app/remove.rs | 56 ++++++++++++++++----------------- 9 files changed, 41 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index bf84ab8..38ec3ff 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,8 @@ Add this to your configuration (usually `~/.zshrc`): eval "$(zoxide init zsh)" ``` -For completions to work, this line must be added _after_ calling `compinit`. +For completions to work, the above line must be added _after_ `compinit` is +called. diff --git a/contrib/completions/_zoxide b/contrib/completions/_zoxide index 9516bed..d9ec10c 100644 --- a/contrib/completions/_zoxide +++ b/contrib/completions/_zoxide @@ -79,8 +79,8 @@ _arguments "${_arguments_options[@]}" \ ;; (remove) _arguments "${_arguments_options[@]}" \ -'()*-i+[]:keywords: ' \ -'()*--interactive=[]:keywords: ' \ +'-i[Use interactive selection]' \ +'--interactive[Use interactive selection]' \ '-h[Print help information]' \ '--help[Print help information]' \ '-V[Print version information]' \ diff --git a/contrib/completions/_zoxide.ps1 b/contrib/completions/_zoxide.ps1 index 72138e6..bb52da0 100644 --- a/contrib/completions/_zoxide.ps1 +++ b/contrib/completions/_zoxide.ps1 @@ -73,8 +73,8 @@ Register-ArgumentCompleter -Native -CommandName 'zoxide' -ScriptBlock { break } 'zoxide;remove' { - [CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'i') - [CompletionResult]::new('--interactive', 'interactive', [CompletionResultType]::ParameterName, 'interactive') + [CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'Use interactive selection') + [CompletionResult]::new('--interactive', 'interactive', [CompletionResultType]::ParameterName, 'Use interactive selection') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information') diff --git a/contrib/completions/zoxide.bash b/contrib/completions/zoxide.bash index 846d05f..792261b 100644 --- a/contrib/completions/zoxide.bash +++ b/contrib/completions/zoxide.bash @@ -126,14 +126,6 @@ _zoxide() { return 0 fi case "${prev}" in - --interactive) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - -i) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; *) COMPREPLY=() ;; diff --git a/contrib/completions/zoxide.elv b/contrib/completions/zoxide.elv index dfdebc2..d630aab 100644 --- a/contrib/completions/zoxide.elv +++ b/contrib/completions/zoxide.elv @@ -66,8 +66,8 @@ set edit:completion:arg-completer[zoxide] = [@words]{ cand --version 'Print version information' } &'zoxide;remove'= { - cand -i 'i' - cand --interactive 'interactive' + cand -i 'Use interactive selection' + cand --interactive 'Use interactive selection' cand -h 'Print help information' cand --help 'Print help information' cand -V 'Print version information' diff --git a/contrib/completions/zoxide.fish b/contrib/completions/zoxide.fish index 1ca8db0..9945b3a 100644 --- a/contrib/completions/zoxide.fish +++ b/contrib/completions/zoxide.fish @@ -23,6 +23,6 @@ complete -c zoxide -n "__fish_seen_subcommand_from query" -s l -l list -d 'List complete -c zoxide -n "__fish_seen_subcommand_from query" -s s -l score -d 'Print score with results' complete -c zoxide -n "__fish_seen_subcommand_from query" -s h -l help -d 'Print help information' complete -c zoxide -n "__fish_seen_subcommand_from query" -s V -l version -d 'Print version information' -complete -c zoxide -n "__fish_seen_subcommand_from remove" -s i -l interactive -r +complete -c zoxide -n "__fish_seen_subcommand_from remove" -s i -l interactive -d 'Use interactive selection' complete -c zoxide -n "__fish_seen_subcommand_from remove" -s h -l help -d 'Print help information' complete -c zoxide -n "__fish_seen_subcommand_from remove" -s V -l version -d 'Print version information' diff --git a/contrib/completions/zoxide.ts b/contrib/completions/zoxide.ts index 61253a9..c3c6fb7 100644 --- a/contrib/completions/zoxide.ts +++ b/contrib/completions/zoxide.ts @@ -181,10 +181,7 @@ const completion: Fig.Spec = { options: [ { name: ["-i", "--interactive"], - args: { - name: "interactive", - isOptional: true, - }, + description: "Use interactive selection", }, { name: ["-h", "--help"], diff --git a/src/app/_app.rs b/src/app/_app.rs index e40e3e7..3d19da5 100644 --- a/src/app/_app.rs +++ b/src/app/_app.rs @@ -123,13 +123,9 @@ pub struct Query { /// Remove a directory from the database #[derive(Debug, Parser)] pub struct Remove { - // Use interactive selection - #[clap(conflicts_with = "paths", long, short, value_name = "keywords")] - pub interactive: Option>, - #[clap( - conflicts_with = "interactive", - required_unless_present = "interactive", - value_hint = ValueHint::DirPath - )] + /// Use interactive selection + #[clap(long, short)] + pub interactive: bool, + #[clap(value_hint = ValueHint::DirPath)] pub paths: Vec, } diff --git a/src/app/remove.rs b/src/app/remove.rs index dba9e85..a967534 100644 --- a/src/app/remove.rs +++ b/src/app/remove.rs @@ -13,41 +13,39 @@ impl Run for Remove { let mut db = DatabaseFile::new(data_dir); let mut db = db.open()?; - match &self.interactive { - Some(keywords) => { - let now = util::current_time()?; - let mut stream = db.stream(now).with_keywords(keywords); + if self.interactive { + let keywords = &self.paths; + let now = util::current_time()?; + let mut stream = db.stream(now).with_keywords(keywords); - let mut fzf = Fzf::new(true)?; - let stdin = fzf.stdin(); + let mut fzf = Fzf::new(true)?; + let stdin = fzf.stdin(); - let selection = loop { - let dir = match stream.next() { - Some(dir) => dir, - None => break fzf.select()?, - }; - - match writeln!(stdin, "{}", dir.display_score(now)) { - Err(e) if e.kind() == io::ErrorKind::BrokenPipe => break fzf.select()?, - result => result.context("could not write to fzf")?, - } + let selection = loop { + let dir = match stream.next() { + Some(dir) => dir, + None => break fzf.select()?, }; - let paths = selection.lines().filter_map(|line| line.get(5..)); - for path in paths { - if !db.remove(path) { - bail!("path not found in database: {}", path); - } + match writeln!(stdin, "{}", dir.display_score(now)) { + Err(e) if e.kind() == io::ErrorKind::BrokenPipe => break fzf.select()?, + result => result.context("could not write to fzf")?, + } + }; + + let paths = selection.lines().filter_map(|line| line.get(5..)); + for path in paths { + if !db.remove(path) { + bail!("path not found in database: {}", path); } } - None => { - for path in &self.paths { - if !db.remove(path) { - let path_abs = util::resolve_path(path)?; - let path_abs = util::path_to_str(&path_abs)?; - if path_abs != path && !db.remove(path_abs) { - bail!("path not found in database: {} ({})", path, path_abs) - } + } else { + for path in &self.paths { + if !db.remove(path) { + let path_abs = util::resolve_path(path)?; + let path_abs = util::path_to_str(&path_abs)?; + if path_abs != path && !db.remove(path_abs) { + bail!("path not found in database: {} ({})", path, path_abs) } } }