Fix interactive remove

This commit is contained in:
Ajeet D'Souza 2021-12-24 17:49:36 +05:30
parent fd088b43db
commit 0a9081383d
9 changed files with 41 additions and 57 deletions

View File

@ -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.
</details>

View File

@ -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]' \

View File

@ -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')

View File

@ -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=()
;;

View File

@ -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'

View File

@ -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'

View File

@ -181,10 +181,7 @@ const completion: Fig.Spec = {
options: [
{
name: ["-i", "--interactive"],
args: {
name: "interactive",
isOptional: true,
},
description: "Use interactive selection",
},
{
name: ["-h", "--help"],

View File

@ -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<Vec<String>>,
#[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<String>,
}

View File

@ -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)
}
}
}