diff --git a/CHANGELOG.md b/CHANGELOG.md index b1f8286..aefc9e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/fzf.rs b/src/fzf.rs index 629b182..96c01ec 100644 --- a/src/fzf.rs +++ b/src/fzf.rs @@ -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")?; diff --git a/src/subcommand/init/shell/fish.rs b/src/subcommand/init/shell/fish.rs index b9c2160..5cffe83 100644 --- a/src/subcommand/init/shell/fish.rs +++ b/src/subcommand/init/shell/fish.rs @@ -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 ) diff --git a/src/subcommand/init/shell/posix.rs b/src/subcommand/init/shell/posix.rs index b76cdcf..16abfef 100644 --- a/src/subcommand/init/shell/posix.rs +++ b/src/subcommand/init/shell/posix.rs @@ -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 ) diff --git a/src/subcommand/init/shell/powershell.rs b/src/subcommand/init/shell/powershell.rs index a019b93..2c7ac2a 100644 --- a/src/subcommand/init/shell/powershell.rs +++ b/src/subcommand/init/shell/powershell.rs @@ -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 ) diff --git a/src/subcommand/remove.rs b/src/subcommand/remove.rs index 530ce8b..43a97cb 100644 --- a/src/subcommand/remove.rs +++ b/src/subcommand/remove.rs @@ -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, - #[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::>(); - - 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"); -}