Change zri to a shell function

This commit is contained in:
Ajeet D'Souza 2020-05-27 05:40:55 +05:30
parent 65068d4bb5
commit e1c1570174
6 changed files with 23 additions and 61 deletions

View File

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Changed
- `zri` is now a shell function.
## [0.4.1] - 2020-05-25
### Added

View File

@ -46,7 +46,7 @@ impl Fzf {
// unwrap() is safe here since we have captured `stdin`
let stdin = self.child.stdin.as_mut().unwrap();
self.lines.sort_unstable_by(|line1, line2| line2.cmp(line1));
self.lines.sort_unstable();
for line in self.lines.iter() {
writeln!(stdin, "{}", line).context("could not write into fzf stdin")?;

View File

@ -67,7 +67,10 @@ abbr -a {0}q 'zoxide query'
abbr -a {0}qi 'zoxide query -i'
abbr -a {0}r 'zoxide remove'
abbr -a {0}ri 'zoxide remove -i'
function {0}ri
set result (zoxide query -i $argv)
and zoxide remove $result
end
"#,
cmd
)

View File

@ -62,7 +62,9 @@ alias {0}q='zoxide query'
alias {0}qi='zoxide query -i'
alias {0}r='zoxide remove'
alias {0}ri='zoxide remove -i'
{0}ri() {{
result=$(zoxide query -i "$@") && zoxide remove "$result"
}}
"#,
cmd
)

View File

@ -55,7 +55,12 @@ function {0}q {{ zoxide query @args }}
function {0}qi {{ zoxide query -i @args }}
function {0}r {{ zoxide remove @args }}
function {0}ri {{ zoxide remove -i @args }}
function {0}ri {{
$result = zoxide query -i @args
if ($LASTEXITCODE -eq 0) {{
zoxide remove $result
}}
}}
"#,
cmd
)

View File

@ -1,5 +1,4 @@
use crate::fzf::Fzf;
use crate::util::{canonicalize, get_current_time, get_db, path_to_str};
use crate::util::{canonicalize, get_db, path_to_str};
use anyhow::{bail, Result};
use structopt::StructOpt;
@ -7,27 +6,12 @@ use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(about = "Remove a directory")]
pub struct Remove {
query: Vec<String>,
#[structopt(short, long, help = "Opens an interactive selection menu using fzf")]
interactive: bool,
path: String,
}
impl Remove {
pub fn run(&self) -> Result<()> {
if self.interactive {
remove_interactive(&self.query)
} else if let [path] = self.query.as_slice() {
remove(&path)
} else {
clap::Error::with_description(
&format!(
"remove requires 1 value in non-interactive mode, but {} were provided",
self.query.len()
),
clap::ErrorKind::WrongNumberOfValues,
)
.exit();
}
remove(&self.path)
}
}
@ -51,41 +35,3 @@ fn remove(path: &str) -> Result<()> {
bail!("could not find path in database: {}", path)
}
fn remove_interactive(keywords: &[String]) -> Result<()> {
let mut db = get_db()?;
let now = get_current_time()?;
let keywords = keywords
.iter()
.map(|keyword| keyword.to_lowercase())
.collect::<Vec<_>>();
let mut fzf = Fzf::new()?;
for idx in (0..db.dirs.len()).rev() {
let dir = &db.dirs[idx];
if !dir.is_match(&keywords) {
continue;
}
if !dir.is_valid() {
db.dirs.swap_remove(idx);
db.modified = true;
continue;
}
fzf.write_dir(&dir, now);
}
if let Some(path) = fzf.wait_selection()? {
if let Some(idx) = db.dirs.iter().position(|dir| dir.path == path) {
db.dirs.swap_remove(idx);
db.modified = true;
return Ok(());
}
}
bail!("no match found");
}