From 66d882bad4bd680a646a960cf5f0fca23e337c6f Mon Sep 17 00:00:00 2001 From: Samuel Tschiedel Date: Tue, 1 Feb 2022 19:25:21 -0300 Subject: [PATCH 01/13] Use --keep-right in fzf (#346) --- CHANGELOG.md | 1 + Cargo.lock | 40 ++++++++++++++++++++-------------------- README.md | 3 ++- src/fzf.rs | 16 +++++++++++----- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6367f..c3ef538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - manpages: moved to `man/man1/*.1`. +- fzf: added `--keep-right` option by default, upgraded minimum version to v0.21.0. ### Fixed diff --git a/Cargo.lock b/Cargo.lock index f3297ec..55745e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.52" +version = "1.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84450d0b4a8bd1ba4144ce8ce718fbc5d071358b1e5384bace6536b3d1f2d5b3" +checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0" [[package]] name = "askama" @@ -123,9 +123,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.0.10" +version = "3.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a30c3bf9ff12dfe5dae53f0a96e0febcd18420d1c0e7fad77796d9d5c4b5375" +checksum = "08799f92c961c7a1cf0cc398a9073da99e21ce388b46372c37f3191f2f3eed3e" dependencies = [ "atty", "bitflags", @@ -140,9 +140,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "3.0.4" +version = "3.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d044e9db8cd0f68191becdeb5246b7462e4cf0c069b19ae00d1bf3fa9889498d" +checksum = "be4dabb7e2f006497e1da045feaa512acf0686f76b68d94925da2d9422dcb521" dependencies = [ "clap", ] @@ -159,9 +159,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.0.6" +version = "3.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "517358c28fcef6607bf6f76108e02afad7e82297d132a6b846dcc1fc3efcd153" +checksum = "0fd2078197a22f338bd4fbf7d6387eb6f0d6a3c69e6cbc09f5c93e97321fd92a" dependencies = [ "heck", "proc-macro-error", @@ -226,9 +226,9 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "fastrand" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "779d043b6a0b90cc4c0ed7ee380a6504394cee7efd7db050e3774eee387324b2" +checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf" dependencies = [ "instant", ] @@ -344,9 +344,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.113" +version = "0.2.116" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eef78b64d87775463c549fbd80e19249ef436ea3bf1de2a1eb7e717ec7fab1e9" +checksum = "565dbd88872dbe4cc8a46e527f26483c1d1f7afa6b884a3bd6cd893d4f98da74" [[package]] name = "log" @@ -475,9 +475,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" dependencies = [ "proc-macro2", ] @@ -590,18 +590,18 @@ checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" [[package]] name = "serde" -version = "1.0.133" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.133" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed201699328568d8d08208fdd080e3ff594e6c422e438b6705905da01005d537" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" dependencies = [ "proc-macro2", "quote", @@ -688,9 +688,9 @@ dependencies = [ [[package]] name = "thread_local" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" dependencies = [ "once_cell", ] diff --git a/README.md b/README.md index 7f9e597..3efa202 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,8 @@ eval "$(zoxide init posix --hook prompt)" ### *Step 3: Install fzf (optional)* [fzf] is a command-line fuzzy finder, used by zoxide for interactive -selection. It can be installed from [here][fzf-installation]. +selection. It can be installed from [here][fzf-installation]. zoxide supports +fzf v0.21.0+. ### *Step 4: Import your data (optional)* diff --git a/src/fzf.rs b/src/fzf.rs index 033cba5..6521d7d 100644 --- a/src/fzf.rs +++ b/src/fzf.rs @@ -23,13 +23,19 @@ impl Fzf { command.env("FZF_DEFAULT_OPTS", fzf_opts); } else { command.args(&[ - "--bind=ctrl-z:ignore", - "--exit-0", - "--height=40%", - "--inline-info", + // Search result "--no-sort", - "--reverse", + // Interface + "--keep-right", + // Layout + "--height=40%", + "--info=inline", + "--layout=reverse", + // Scripting + "--exit-0", "--select-1", + // Key/Event bindings + "--bind=ctrl-z:ignore", ]); if cfg!(unix) { command.arg("--preview=ls -p {2..}"); From b3366dd5b6bb44d7592f098afc67b4c07e5933eb Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Thu, 17 Feb 2022 04:16:45 +0530 Subject: [PATCH 02/13] Upgrade to clap v3.1.0 --- Cargo.lock | 83 +++++++++++++++++++++----------- Cargo.toml | 8 +-- build.rs | 24 ++++----- contrib/completions/_zoxide.ps1 | 3 +- src/app/mod.rs | 26 ---------- src/{app/_app.rs => cmd/_cmd.rs} | 8 +-- src/{app => cmd}/add.rs | 2 +- src/{app => cmd}/import.rs | 2 +- src/{app => cmd}/init.rs | 2 +- src/cmd/mod.rs | 26 ++++++++++ src/{app => cmd}/query.rs | 2 +- src/{app => cmd}/remove.rs | 2 +- src/main.rs | 6 +-- src/shell.rs | 2 +- xtask/Cargo.toml | 2 +- 15 files changed, 113 insertions(+), 85 deletions(-) delete mode 100644 src/app/mod.rs rename src/{app/_app.rs => cmd/_cmd.rs} (94%) rename src/{app => cmd}/add.rs (97%) rename src/{app => cmd}/import.rs (99%) rename src/{app => cmd}/init.rs (96%) create mode 100644 src/cmd/mod.rs rename src/{app => cmd}/query.rs (98%) rename src/{app => cmd}/remove.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index 55745e0..786ac91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,9 @@ checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0" [[package]] name = "askama" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d8f355701c672c2ba3d718acbd213f740beea577cc4eae66accdffe15be1882" +checksum = "fb98f10f371286b177db5eeb9a6e5396609555686a35e1d4f7b9a9c6d8af0139" dependencies = [ "askama_derive", "askama_escape", @@ -30,9 +30,9 @@ dependencies = [ [[package]] name = "askama_derive" -version = "0.11.0" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84704cab5b7ae0fd3a9f78ee5eb7b27f3749df445f04623db6633459ae283267" +checksum = "87bf87e6e8b47264efa9bde63d6225c6276a52e05e91bf37eaa8afd0032d6b71" dependencies = [ "askama_shared", "proc-macro2", @@ -41,17 +41,19 @@ dependencies = [ [[package]] name = "askama_escape" -version = "0.10.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a1bb320f97e6edf9f756bf015900038e43c7700e059688e5724a928c8f3b8d5" +checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" [[package]] name = "askama_shared" -version = "0.12.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dae03eebba55a2697a376e58b573a29fe36893157173ac8df312ad85f3c0e012" +checksum = "e3d372b233d121e841b4cc5dc716538755e338ca902b3e12ac131ffb6b9d5fbf" dependencies = [ "askama_escape", + "mime", + "mime_guess", "nom", "proc-macro2", "quote", @@ -85,9 +87,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bincode" @@ -123,9 +125,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.0.13" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08799f92c961c7a1cf0cc398a9073da99e21ce388b46372c37f3191f2f3eed3e" +checksum = "e5f1fea81f183005ced9e59cdb01737ef2423956dac5a6d731b06b2ecfaa3467" dependencies = [ "atty", "bitflags", @@ -140,18 +142,18 @@ dependencies = [ [[package]] name = "clap_complete" -version = "3.0.5" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4dabb7e2f006497e1da045feaa512acf0686f76b68d94925da2d9422dcb521" +checksum = "23eec4dd324308f49d8bf86a2732078c34d57955fec1e1d865554fc37c15d420" dependencies = [ "clap", ] [[package]] name = "clap_complete_fig" -version = "3.0.2" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29cc003d824770d10072f4aa4a958e66d33d74a9cb7339595ac2a445d80d50a0" +checksum = "5c11f6f44afea4aee21bb57a5297879c88ac8dc97224fbbbe796edd60a098f0e" dependencies = [ "clap", "clap_complete", @@ -159,9 +161,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.0.12" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fd2078197a22f338bd4fbf7d6387eb6f0d6a3c69e6cbc09f5c93e97321fd92a" +checksum = "5fd1122e63869df2cb309f449da1ad54a7c6dfeb7c7e6ccd8e0825d9eb93bb72" dependencies = [ "heck", "proc-macro-error", @@ -172,9 +174,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.6" +version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcae03edb34f947e64acdb1c33ec169824e20657e9ecb61cef6c8c74dcb8120" +checksum = "b5e5bed1f1c269533fa816a0a5492b3545209a205ca1a54842be180eb63a16a6" dependencies = [ "cfg-if", "lazy_static", @@ -344,9 +346,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.116" +version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "565dbd88872dbe4cc8a46e527f26483c1d1f7afa6b884a3bd6cd893d4f98da74" +checksum = "06e509672465a0504304aa87f9f176f2b2b716ed8fb105ebe5c02dc6dce96a94" [[package]] name = "log" @@ -363,6 +365,22 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +[[package]] +name = "mime" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" + +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -484,9 +502,9 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "rand_core", ] @@ -584,9 +602,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" +checksum = "0486718e92ec9a68fbed73bb5ef687d71103b142595b406835649bebd33f72c7" [[package]] name = "serde" @@ -610,9 +628,9 @@ dependencies = [ [[package]] name = "shell-words" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074" +checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" [[package]] name = "strsim" @@ -695,6 +713,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-xid" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index 4c8e4db..1337713 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ members = ["xtask/"] anyhow = "1.0.32" askama = { version = "0.11.0", default-features = false } bincode = "1.3.1" -clap = { version = "3.0.0", features = ["derive"] } +clap = { version = "3.1.0", features = ["derive"] } dirs = "4.0.0" dunce = "1.0.1" glob = "0.3.0" @@ -36,9 +36,9 @@ rand = { version = "0.8.4", features = [ ], default-features = false } [build-dependencies] -clap = { version = "3.0.0", features = ["derive"] } -clap_complete = "3.0.0" -clap_complete_fig = "3.0.0" +clap = { version = "3.1.0", features = ["derive"] } +clap_complete = "3.1.0" +clap_complete_fig = "3.1.0" [dev-dependencies] assert_cmd = "2.0.0" diff --git a/build.rs b/build.rs index 57df123..2c1df03 100644 --- a/build.rs +++ b/build.rs @@ -32,25 +32,25 @@ fn git_version() -> Option { } fn generate_completions() -> io::Result<()> { - #[path = "src/app/_app.rs"] - mod app; + #[path = "src/cmd/_cmd.rs"] + mod cmd; - use app::App; - use clap::IntoApp; + use clap::CommandFactory; use clap_complete::generate_to; - use clap_complete::Shell::{Bash, Elvish, Fish, PowerShell, Zsh}; + use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh}; use clap_complete_fig::Fig; + use cmd::Cmd; - let app = &mut App::into_app(); + let cmd = &mut Cmd::command(); let bin_name = env!("CARGO_PKG_NAME"); let out_dir = "contrib/completions"; - generate_to(Bash, app, bin_name, out_dir)?; - generate_to(Elvish, app, bin_name, out_dir)?; - generate_to(Fig, app, bin_name, out_dir)?; - generate_to(Fish, app, bin_name, out_dir)?; - generate_to(PowerShell, app, bin_name, out_dir)?; - generate_to(Zsh, app, bin_name, out_dir)?; + generate_to(Bash, cmd, bin_name, out_dir)?; + generate_to(Elvish, cmd, bin_name, out_dir)?; + generate_to(Fig, cmd, bin_name, out_dir)?; + generate_to(Fish, cmd, bin_name, out_dir)?; + generate_to(PowerShell, cmd, bin_name, out_dir)?; + generate_to(Zsh, cmd, bin_name, out_dir)?; Ok(()) } diff --git a/contrib/completions/_zoxide.ps1 b/contrib/completions/_zoxide.ps1 index bb52da0..ebe83de 100644 --- a/contrib/completions/_zoxide.ps1 +++ b/contrib/completions/_zoxide.ps1 @@ -12,7 +12,8 @@ Register-ArgumentCompleter -Native -CommandName 'zoxide' -ScriptBlock { $element = $commandElements[$i] if ($element -isnot [StringConstantExpressionAst] -or $element.StringConstantType -ne [StringConstantType]::BareWord -or - $element.Value.StartsWith('-')) { + $element.Value.StartsWith('-') -or + $element.Value -eq $wordToComplete) { break } $element.Value diff --git a/src/app/mod.rs b/src/app/mod.rs deleted file mode 100644 index 04c2fe1..0000000 --- a/src/app/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -mod _app; -mod add; -mod import; -mod init; -mod query; -mod remove; - -use anyhow::Result; - -pub use crate::app::_app::*; - -pub trait Run { - fn run(&self) -> Result<()>; -} - -impl Run for App { - fn run(&self) -> Result<()> { - match self { - App::Add(cmd) => cmd.run(), - App::Import(cmd) => cmd.run(), - App::Init(cmd) => cmd.run(), - App::Query(cmd) => cmd.run(), - App::Remove(cmd) => cmd.run(), - } - } -} diff --git a/src/app/_app.rs b/src/cmd/_cmd.rs similarity index 94% rename from src/app/_app.rs rename to src/cmd/_cmd.rs index 3d19da5..d75a86f 100644 --- a/src/app/_app.rs +++ b/src/cmd/_cmd.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use clap::{AppSettings, ArgEnum, Parser, ValueHint}; +use clap::{ArgEnum, Parser, ValueHint}; const ENV_HELP: &str = "ENVIRONMENT VARIABLES: _ZO_DATA_DIR Path for zoxide data files @@ -16,11 +16,11 @@ const ENV_HELP: &str = "ENVIRONMENT VARIABLES: about, author, after_help = ENV_HELP, - global_setting(AppSettings::DisableHelpSubcommand), - global_setting(AppSettings::PropagateVersion), + disable_help_subcommand = true, + propagate_version = true, version = option_env!("ZOXIDE_VERSION").unwrap_or_default() )] -pub enum App { +pub enum Cmd { Add(Add), Import(Import), Init(Init), diff --git a/src/app/add.rs b/src/cmd/add.rs similarity index 97% rename from src/app/add.rs rename to src/cmd/add.rs index 7477701..1bbe697 100644 --- a/src/app/add.rs +++ b/src/cmd/add.rs @@ -2,7 +2,7 @@ use std::path::Path; use anyhow::{bail, Result}; -use crate::app::{Add, Run}; +use crate::cmd::{Add, Run}; use crate::db::DatabaseFile; use crate::{config, util}; diff --git a/src/app/import.rs b/src/cmd/import.rs similarity index 99% rename from src/app/import.rs rename to src/cmd/import.rs index 90d66bc..83c939a 100644 --- a/src/app/import.rs +++ b/src/cmd/import.rs @@ -2,7 +2,7 @@ use std::fs; use anyhow::{bail, Context, Result}; -use crate::app::{Import, ImportFrom, Run}; +use crate::cmd::{Import, ImportFrom, Run}; use crate::config; use crate::db::{Database, DatabaseFile, Dir}; diff --git a/src/app/init.rs b/src/cmd/init.rs similarity index 96% rename from src/app/init.rs rename to src/cmd/init.rs index 64186ad..639ec0f 100644 --- a/src/app/init.rs +++ b/src/cmd/init.rs @@ -3,7 +3,7 @@ use std::io::{self, Write}; use anyhow::{Context, Result}; use askama::Template; -use crate::app::{Init, InitShell, Run}; +use crate::cmd::{Init, InitShell, Run}; use crate::config; use crate::error::BrokenPipeHandler; use crate::shell::{self, Opts}; diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs new file mode 100644 index 0000000..c9cab3c --- /dev/null +++ b/src/cmd/mod.rs @@ -0,0 +1,26 @@ +mod _cmd; +mod add; +mod import; +mod init; +mod query; +mod remove; + +use anyhow::Result; + +pub use crate::cmd::_cmd::*; + +pub trait Run { + fn run(&self) -> Result<()>; +} + +impl Run for Cmd { + fn run(&self) -> Result<()> { + match self { + Cmd::Add(cmd) => cmd.run(), + Cmd::Import(cmd) => cmd.run(), + Cmd::Init(cmd) => cmd.run(), + Cmd::Query(cmd) => cmd.run(), + Cmd::Remove(cmd) => cmd.run(), + } + } +} diff --git a/src/app/query.rs b/src/cmd/query.rs similarity index 98% rename from src/app/query.rs rename to src/cmd/query.rs index 9262c08..109640b 100644 --- a/src/app/query.rs +++ b/src/cmd/query.rs @@ -2,7 +2,7 @@ use std::io::{self, Write}; use anyhow::{Context, Result}; -use crate::app::{Query, Run}; +use crate::cmd::{Query, Run}; use crate::db::{Database, DatabaseFile}; use crate::error::BrokenPipeHandler; use crate::fzf::Fzf; diff --git a/src/app/remove.rs b/src/cmd/remove.rs similarity index 98% rename from src/app/remove.rs rename to src/cmd/remove.rs index a967534..7cf5212 100644 --- a/src/app/remove.rs +++ b/src/cmd/remove.rs @@ -2,7 +2,7 @@ use std::io::{self, Write}; use anyhow::{bail, Context, Result}; -use crate::app::{Remove, Run}; +use crate::cmd::{Remove, Run}; use crate::db::DatabaseFile; use crate::fzf::Fzf; use crate::{config, util}; diff --git a/src/main.rs b/src/main.rs index c9584e4..4bebcaf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -mod app; +mod cmd; mod config; mod db; mod error; @@ -11,7 +11,7 @@ use std::{env, process}; use clap::Parser; -use crate::app::{App, Run}; +use crate::cmd::{Cmd, Run}; use crate::error::SilentExit; pub fn main() { @@ -19,7 +19,7 @@ pub fn main() { env::remove_var("RUST_LIB_BACKTRACE"); env::remove_var("RUST_BACKTRACE"); - if let Err(e) = App::parse().run() { + if let Err(e) = Cmd::parse().run() { match e.downcast::() { Ok(SilentExit { code }) => process::exit(code), Err(e) => { diff --git a/src/shell.rs b/src/shell.rs index fe06e5d..877b632 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,4 +1,4 @@ -use crate::app::InitHook; +use crate::cmd::InitHook; #[derive(Debug, Eq, PartialEq)] pub struct Opts<'a> { diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 5f48bcd..b77af24 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -6,6 +6,6 @@ publish = false [dependencies] anyhow = "1.0.32" -clap = { version = "3.0.0", features = ["derive"] } +clap = { version = "3.1.0", features = ["derive"] } ignore = "0.4.18" shell-words = "1.0.0" From df148c834fa0eb4a99cac18720e05059bf771430 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Fri, 25 Feb 2022 04:15:19 +0530 Subject: [PATCH 03/13] Require bash 4.4+ for completions (#354) --- templates/bash.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/bash.txt b/templates/bash.txt index 88fb9a2..0cd3baf 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -115,11 +115,12 @@ function {{cmd}}i() { } # Load completions. -# - Bash 4.0+ is needed to use `mapfile`. +# - Bash 4.4+ is required to use `@Q`. # - Completions require line editing. Since Bash supports only two modes of # line editing (`vim` and `emacs`), we check if either them is enabled. # - Completions don't work on `dumb` terminals. -if [[ ${BASH_VERSINFO:-0} -ge 4 && :"${SHELLOPTS}": =~ :(vi|emacs): && ${TERM} != 'dumb' ]]; then +if [[ ${BASH_VERSINFO[0]:-0} -eq 4 && ${BASH_VERSINFO[1]:-0} -ge 4 || ${BASH_VERSINFO[0]:-0} -ge 5 ]] && + [[ :"${SHELLOPTS}": =~ :(vi|emacs): && ${TERM} != 'dumb' ]]; then # Use `printf '\e[5n'` to redraw line after fzf closes. \builtin bind '"\e[0n": redraw-current-line' &>/dev/null From ea096ac3ca56c9d99bc18bac5728aca43dd8bd9a Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Fri, 25 Feb 2022 09:49:45 +0530 Subject: [PATCH 04/13] Upgrade to Rust 1.59, strip binaries --- .github/workflows/ci.yml | 2 ++ CHANGELOG.md | 5 +++-- Cargo.lock | 24 ++++++++++++------------ Cargo.toml | 4 ++-- README.md | 2 +- man/man1/zoxide.1 | 18 ++++++++++-------- shell.nix | 2 +- 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cc79e5c..8500f7c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,8 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] steps: - uses: actions/checkout@v2 + with: + fetch-depth: 0 - uses: actions-rs/toolchain@v1 if: ${{ matrix.os == 'windows-latest' }} diff --git a/CHANGELOG.md b/CHANGELOG.md index c3ef538..a904bc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- manpages: moved to `man/man1/*.1`. -- fzf: added `--keep-right` option by default, upgraded minimum version to v0.21.0. +- Manpages: moved to `man/man1/*.1`. +- Fzf: added `--keep-right` option by default, upgraded minimum version to v0.21.0. +- Bash: only enable completions on 4.4+. ### Fixed diff --git a/Cargo.lock b/Cargo.lock index 786ac91..5fad256 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.53" +version = "1.0.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0" +checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" [[package]] name = "askama" @@ -125,9 +125,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.1.0" +version = "3.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5f1fea81f183005ced9e59cdb01737ef2423956dac5a6d731b06b2ecfaa3467" +checksum = "5177fac1ab67102d8989464efd043c6ff44191b1557ec1ddd489b4f7e1447e77" dependencies = [ "atty", "bitflags", @@ -161,9 +161,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.1.0" +version = "3.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fd1122e63869df2cb309f449da1ad54a7c6dfeb7c7e6ccd8e0825d9eb93bb72" +checksum = "01d42c94ce7c2252681b5fed4d3627cc807b13dfc033246bd05d5b252399000e" dependencies = [ "heck", "proc-macro-error", @@ -243,9 +243,9 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "getrandom" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" +checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" dependencies = [ "cfg-if", "libc", @@ -346,9 +346,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.118" +version = "0.2.119" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06e509672465a0504304aa87f9f176f2b2b716ed8fb105ebe5c02dc6dce96a94" +checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" [[package]] name = "log" @@ -602,9 +602,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0486718e92ec9a68fbed73bb5ef687d71103b142595b406835649bebd33f72c7" +checksum = "a4a3381e03edd24287172047536f20cabde766e2cd3e65e6b00fb3af51c4f38d" [[package]] name = "serde" diff --git a/Cargo.toml b/Cargo.toml index 1337713..66ab7b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ keywords = ["cli"] license = "MIT" name = "zoxide" repository = "https://github.com/ajeetdsouza/zoxide" -rust-version = "1.56" +rust-version = "1.59" version = "0.8.0" [badges] @@ -51,4 +51,4 @@ nix = [] [profile.release] codegen-units = 1 lto = true -# strip = true +strip = true diff --git a/README.md b/README.md index 3efa202..70b0748 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ z - # cd into previous directory zi foo # cd with interactive selection (using fzf) -z foo # show interactive completions (zoxide v0.8.0+, bash/fish/zsh only) +z foo # show interactive completions (zoxide v0.8.0+, bash 4.4+/fish/zsh only) ``` Read more about the matching algorithm [here][algorithm-matching]. diff --git a/man/man1/zoxide.1 b/man/man1/zoxide.1 index e7259d4..9acbe08 100644 --- a/man/man1/zoxide.1 +++ b/man/man1/zoxide.1 @@ -9,16 +9,18 @@ directories you use most frequently, and uses a ranking algorithm to navigate to the best match. .SH USAGE .nf -$ z foo # cd into highest ranked directory matching foo -$ z foo bar # cd into highest ranked directory matching foo and bar -$ z foo / # cd into a subdirectory starting with foo +z foo # cd into highest ranked directory matching foo +z foo bar # cd into highest ranked directory matching foo and bar +z foo / # cd into a subdirectory starting with foo .sp -$ z ~/foo # z also works like a regular cd command -$ z foo/ # cd into relative path -$ z .. # cd one level up -$ z - # cd into previous directory +z ~/foo # z also works like a regular cd command +z foo/ # cd into relative path +z .. # cd one level up +z - # cd into previous directory .sp -$ zi foo # cd with interactive selection (using fzf) +zi foo # cd with interactive selection (using fzf) +.sp +z foo # show interactive completions (bash 4.4+/fish/zsh only) .fi .SH SUBCOMMANDS .TP diff --git a/shell.nix b/shell.nix index e2c7176..1a4bd42 100644 --- a/shell.nix +++ b/shell.nix @@ -1,6 +1,6 @@ let rust = import (builtins.fetchTarball - "https://github.com/oxalica/rust-overlay/archive/203dc4fc3fe2a5df1aa481a3fc8a1bb27074d677.tar.gz"); + "https://github.com/oxalica/rust-overlay/archive/46d8d20fce510c6a25fa66f36e31f207f6ea49e4.tar.gz"); pkgs = import (builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/fae46e66a5df220327b45e0d7c27c6961cf922ce.tar.gz") { overlays = [ rust ]; From f2857f229fb76f128552b3d5a8472487744be2a3 Mon Sep 17 00:00:00 2001 From: sitiom Date: Sun, 27 Feb 2022 21:40:08 +0800 Subject: [PATCH 05/13] Fix Windows _ZO_DATA_DIR path in docs (#355) --- README.md | 2 +- man/man1/zoxide.1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 70b0748..0db5120 100644 --- a/README.md +++ b/README.md @@ -320,7 +320,7 @@ They must be set before `zoxide init` is called. | ----------- | ---------------------------------------- | ------------------------------------------ | | Linux / BSD | `$XDG_DATA_HOME` or `$HOME/.local/share` | `/home/alice/.local/share` | | macOS | `$HOME/Library/Application Support` | `/Users/Alice/Library/Application Support` | - | Windows | `{FOLDERID_RoamingAppData}` | `C:\Users\Alice\AppData\Roaming` | + | Windows | `%LOCALAPPDATA%` | `C:\Users\Alice\AppData\Local` | - `_ZO_ECHO` - When set to 1, `z` will print the matched directory before navigating to it. diff --git a/man/man1/zoxide.1 b/man/man1/zoxide.1 index 9acbe08..9ebf751 100644 --- a/man/man1/zoxide.1 +++ b/man/man1/zoxide.1 @@ -65,7 +65,7 @@ T} \fB/Users/Alice/Library/Application Support\fR T} \fBWindows\fR|T{ -\fB{FOLDERID_RoamingAppData}\fR, eg. \fBC:\\Users\\Alice\\AppData\\Roaming\fR +\fB%LOCALAPPDATA%\fR, eg. \fBC:\\Users\\Alice\\AppData\\Local\fR T} .TE .TP From 6307146fbb76cc2f04158f2648bd8bc728510443 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Fri, 4 Mar 2022 05:06:32 +0530 Subject: [PATCH 06/13] Add zabb and clink-zoxide to README --- README.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0db5120..94e67e4 100644 --- a/README.md +++ b/README.md @@ -346,16 +346,18 @@ They must be set before `zoxide init` is called. ## Third-party integrations -| Application | Description | Plugin | -| ------------------ | --------------------------------------- | -------------------------- | -| [emacs] | Text editor | [zoxide.el] | -| [nnn] | File manager | [nnn-autojump] | -| [ranger] | File manager | [ranger-zoxide] | -| [telescope.nvim] | Fuzzy finder for Neovim | [telescope-zoxide] | -| [vim] | Text editor | [zoxide.vim] | -| [xplr] | File manager | [zoxide.xplr] | -| [xxh] | Transports shell configuration over SSH | [xxh-plugin-prerun-zoxide] | -| [zsh-autocomplete] | Realtime completions for zsh | Supported by default | +| Application | Description | Plugin | +| ------------------ | -------------------------------------------- | -------------------------- | +| [clink] | Improved cmd.exe for Windows | [clink-zoxide] | +| [emacs] | Text editor | [zoxide.el] | +| [nnn] | File manager | [nnn-autojump] | +| [ranger] | File manager | [ranger-zoxide] | +| [telescope.nvim] | Fuzzy finder for Neovim | [telescope-zoxide] | +| [vim] | Text editor | [zoxide.vim] | +| [xplr] | File manager | [zoxide.xplr] | +| [xxh] | Transports shell configuration over SSH | [xxh-plugin-prerun-zoxide] | +| [zabb] | Finds the shortest possible query for a path | Natively supported | +| [zsh-autocomplete] | Realtime completions for zsh | Natively supported | [algorithm-aging]: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#aging [algorithm-matching]: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#matching @@ -364,6 +366,8 @@ They must be set before `zoxide init` is called. [builtwithnix-badge]: https://img.shields.io/badge/builtwith-nix-7d81f7?style=flat-square [builtwithnix]: https://builtwithnix.org/ [chocolatey]: https://community.chocolatey.org/packages/zoxide +[clink-zoxide]: https://github.com/shunsambongi/clink-zoxide +[clink]: https://github.com/mridgers/clink [conda-forge]: https://anaconda.org/conda-forge/zoxide [copr]: https://copr.fedorainfracloud.org/coprs/atim/zoxide/ [crates.io-badge]: https://img.shields.io/crates/v/zoxide?style=flat-square @@ -405,6 +409,7 @@ They must be set before `zoxide init` is called. [xplr]: https://github.com/sayanarijit/xplr [xxh-plugin-prerun-zoxide]: https://github.com/xxh/xxh-plugin-prerun-zoxide [xxh]: https://github.com/xxh/xxh +[zabb]: https://github.com/Mellbourn/zabb [zoxide.el]: https://gitlab.com/Vonfry/zoxide.el [zoxide.vim]: https://github.com/nanotee/zoxide.vim [zoxide.xplr]: https://github.com/sayanarijit/zoxide.xplr From dc64d912abda836cd78a2d2e3cc7e8aea0c6b003 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Mon, 7 Mar 2022 05:07:05 +0530 Subject: [PATCH 07/13] Reuse values in shell tests --- Cargo.lock | 44 +++++---- Cargo.toml | 1 + contrib/completions/zoxide.bash | 4 +- src/main.rs | 6 ++ src/shell.rs | 161 ++++++++------------------------ 5 files changed, 78 insertions(+), 138 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5fad256..9f91ed4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,9 +47,9 @@ checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" [[package]] name = "askama_shared" -version = "0.12.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d372b233d121e841b4cc5dc716538755e338ca902b3e12ac131ffb6b9d5fbf" +checksum = "bf722b94118a07fcbc6640190f247334027685d4e218b794dbfe17c32bf38ed0" dependencies = [ "askama_escape", "mime", @@ -125,9 +125,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.1.2" +version = "3.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5177fac1ab67102d8989464efd043c6ff44191b1557ec1ddd489b4f7e1447e77" +checksum = "ced1892c55c910c1219e98d6fc8d71f6bddba7905866ce740066d8bfea859312" dependencies = [ "atty", "bitflags", @@ -142,9 +142,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "3.1.0" +version = "3.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23eec4dd324308f49d8bf86a2732078c34d57955fec1e1d865554fc37c15d420" +checksum = "df6f3613c0a3cddfd78b41b10203eb322cb29b600cbdf808a7d3db95691b8e25" dependencies = [ "clap", ] @@ -161,9 +161,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.1.2" +version = "3.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01d42c94ce7c2252681b5fed4d3627cc807b13dfc033246bd05d5b252399000e" +checksum = "da95d038ede1a964ce99f49cbe27a7fb538d1da595e4b4f70b8c8f338d17bf16" dependencies = [ "heck", "proc-macro-error", @@ -409,9 +409,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" +checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" [[package]] name = "ordered-float" @@ -520,9 +520,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +checksum = "8380fe0152551244f0747b1bf41737e0f8a74f97a14ccefd1148187271634f3c" dependencies = [ "bitflags", ] @@ -582,6 +582,17 @@ dependencies = [ "syn", ] +[[package]] +name = "rstest_reuse" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b29d3117bce27ea307d1fb7ce12c64ba11b3fd04311a42d32bc5f0072e6e3d4d" +dependencies = [ + "quote", + "rustc_version", + "syn", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -665,9 +676,9 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" dependencies = [ "winapi-util", ] @@ -680,9 +691,9 @@ checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b" [[package]] name = "textwrap" -version = "0.14.2" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" [[package]] name = "thiserror" @@ -818,6 +829,7 @@ dependencies = [ "ordered-float", "rand", "rstest", + "rstest_reuse", "serde", "tempfile", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 66ab7b6..e67083b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ clap_complete_fig = "3.1.0" [dev-dependencies] assert_cmd = "2.0.0" rstest = "0.12.0" +rstest_reuse = "0.3.0" [features] default = [] diff --git a/contrib/completions/zoxide.bash b/contrib/completions/zoxide.bash index 792261b..e4ca62d 100644 --- a/contrib/completions/zoxide.bash +++ b/contrib/completions/zoxide.bash @@ -69,7 +69,7 @@ _zoxide() { fi case "${prev}" in --from) - COMPREPLY=($(compgen -W "autojump z" -- "${cur}")) + COMPREPLY=($(compgen -W "" -- "${cur}")) return 0 ;; *) @@ -91,7 +91,7 @@ _zoxide() { return 0 ;; --hook) - COMPREPLY=($(compgen -W "none prompt pwd" -- "${cur}")) + COMPREPLY=($(compgen -W "" -- "${cur}")) return 0 ;; *) diff --git a/src/main.rs b/src/main.rs index 4bebcaf..8062110 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,9 @@ +#![allow(clippy::single_component_path_imports)] + +// rstest_reuse must be imported at the top of the crate. +#[cfg(test)] +use rstest_reuse; + mod cmd; mod config; mod db; diff --git a/src/shell.rs b/src/shell.rs index 877b632..09418c6 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -38,16 +38,22 @@ mod tests { use askama::Template; use assert_cmd::Command; use rstest::rstest; + use rstest_reuse::{apply, template}; use super::*; + #[template] #[rstest] - fn bash_bash( + fn opts( #[values(None, Some("z"))] cmd: Option<&str>, #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, #[values(false, true)] echo: bool, #[values(false, true)] resolve_symlinks: bool, ) { + } + + #[apply(opts)] + fn bash_bash(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Bash(&opts).render().unwrap(); Command::new("bash") @@ -58,13 +64,8 @@ mod tests { .stderr(""); } - #[rstest] - fn bash_shellcheck( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn bash_shellcheck(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Bash(&opts).render().unwrap(); @@ -77,13 +78,8 @@ mod tests { .stderr(""); } - #[rstest] - fn bash_shfmt( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn bash_shfmt(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let mut source = Bash(&opts).render().unwrap(); source.push('\n'); @@ -97,13 +93,8 @@ mod tests { .stderr(""); } - #[rstest] - fn elvish_elvish( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn elvish_elvish(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let mut source = String::new(); @@ -117,13 +108,8 @@ mod tests { Command::new("elvish").args(&["-c", &source, "-norc"]).assert().success().stdout("").stderr(""); } - #[rstest] - fn fish_fish( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn fish_fish(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Fish(&opts).render().unwrap(); @@ -139,13 +125,8 @@ mod tests { .stderr(""); } - #[rstest] - fn fish_fishindent( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn fish_fishindent(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let mut source = Fish(&opts).render().unwrap(); source.push('\n'); @@ -162,13 +143,8 @@ mod tests { .stderr(""); } - #[rstest] - fn nushell_nushell( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn nushell_nushell(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Nushell(&opts).render().unwrap(); @@ -183,13 +159,8 @@ mod tests { } } - #[rstest] - fn posix_bash( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn posix_bash(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Posix(&opts).render().unwrap(); @@ -203,13 +174,8 @@ mod tests { } } - #[rstest] - fn posix_dash( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn posix_dash(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Posix(&opts).render().unwrap(); @@ -219,13 +185,8 @@ mod tests { } } - #[rstest] - fn posix_shellcheck_( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn posix_shellcheck_(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Posix(&opts).render().unwrap(); @@ -238,13 +199,8 @@ mod tests { .stderr(""); } - #[rstest] - fn posix_shfmt( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn posix_shfmt(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let mut source = Posix(&opts).render().unwrap(); source.push('\n'); @@ -258,13 +214,8 @@ mod tests { .stderr(""); } - #[rstest] - fn powershell_pwsh( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn powershell_pwsh(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let mut source = "Set-StrictMode -Version latest\n".to_string(); Powershell(&opts).render_into(&mut source).unwrap(); @@ -277,13 +228,8 @@ mod tests { .stderr(""); } - #[rstest] - fn xonsh_black( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn xonsh_black(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let mut source = Xonsh(&opts).render().unwrap(); source.push('\n'); @@ -291,26 +237,16 @@ mod tests { Command::new("black").args(&["--check", "--diff", "-"]).write_stdin(source).assert().success().stdout(""); } - #[rstest] - fn xonsh_mypy( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn xonsh_mypy(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Xonsh(&opts).render().unwrap(); Command::new("mypy").args(&["--command", &source, "--strict"]).assert().success().stderr(""); } - #[rstest] - fn xonsh_pylint( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn xonsh_pylint(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let mut source = Xonsh(&opts).render().unwrap(); source.push('\n'); @@ -323,13 +259,8 @@ mod tests { .stderr(""); } - #[rstest] - fn xonsh_xonsh( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn xonsh_xonsh(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Xonsh(&opts).render().unwrap(); @@ -345,13 +276,8 @@ mod tests { .stderr(""); } - #[rstest] - fn zsh_shellcheck( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn zsh_shellcheck(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Zsh(&opts).render().unwrap(); @@ -365,13 +291,8 @@ mod tests { .stderr(""); } - #[rstest] - fn zsh_zsh( - #[values(None, Some("z"))] cmd: Option<&str>, - #[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook, - #[values(false, true)] echo: bool, - #[values(false, true)] resolve_symlinks: bool, - ) { + #[apply(opts)] + fn zsh_zsh(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; let source = Zsh(&opts).render().unwrap(); From 90e781a1928eae96554ba9c62dde0b8657b4d148 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Tue, 8 Mar 2022 10:53:30 +0530 Subject: [PATCH 08/13] Bypass alias in fzf preview (#329) --- CHANGELOG.md | 7 ++++--- src/fzf.rs | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a904bc1..edaf23d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,13 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Manpages: moved to `man/man1/*.1`. -- Fzf: added `--keep-right` option by default, upgraded minimum version to v0.21.0. -- Bash: only enable completions on 4.4+. ### Fixed -- Rename `_z` completion function to avoid conflicts with other shell plugins. +- Bash/Zsh: rename `_z` completion function to avoid conflicts with other shell plugins. - Elvish: upgrade to new lambda syntax. +- Fzf: added `--keep-right` option by default, upgraded minimum version to v0.21.0. +- Bash: only enable completions on 4.4+. +- Fzf: bypass `ls` alias in preview window. ## [0.8.0] - 2021-12-25 diff --git a/src/fzf.rs b/src/fzf.rs index 6521d7d..ac4b30b 100644 --- a/src/fzf.rs +++ b/src/fzf.rs @@ -38,7 +38,8 @@ impl Fzf { "--bind=ctrl-z:ignore", ]); if cfg!(unix) { - command.arg("--preview=ls -p {2..}"); + command.env("SHELL", "sh"); + command.arg(r"--preview=\command -p ls -p {2..}"); } } From 17365710af7cfb9cfe0030756ac31ffae4286436 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Wed, 9 Mar 2022 13:43:52 +0530 Subject: [PATCH 09/13] Copy owner from previous database file (#364) --- CHANGELOG.md | 19 +++-- Cargo.lock | 82 ++++++++++------------ Cargo.toml | 13 ++-- src/cmd/query.rs | 4 +- src/cmd/remove.rs | 4 +- src/db/mod.rs | 55 ++------------- src/error.rs | 15 ++-- src/fzf.rs | 77 -------------------- src/main.rs | 1 - src/shell.rs | 2 +- src/util.rs | 163 ++++++++++++++++++++++++++++++++++++++++++- templates/fish.txt | 4 +- tests/completions.rs | 2 +- 13 files changed, 236 insertions(+), 205 deletions(-) delete mode 100644 src/fzf.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index edaf23d..097eb03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,11 +15,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Bash/Zsh: rename `_z` completion function to avoid conflicts with other shell plugins. +- Bash/Zsh: rename `_z` completion function to avoid conflicts with other shell + plugins. - Elvish: upgrade to new lambda syntax. -- Fzf: added `--keep-right` option by default, upgraded minimum version to v0.21.0. +- Fzf: added `--keep-right` option by default, upgraded minimum version to + v0.21.0. - Bash: only enable completions on 4.4+. - Fzf: bypass `ls` alias in preview window. +- Retain ownership of database file. ## [0.8.0] - 2021-12-25 @@ -30,7 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Fzf: better default options. -- Fish: interactive completions are only triggered when the last argument is empty. +- Fish: interactive completions are only triggered when the last argument is + empty. - PowerShell: installation instructions. ### Fixed @@ -131,7 +135,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Auto-generated shell completions. - `zoxide query --all` for listing deleted directories. -- Lazy deletion for removed directories that have not been accessed in > 90 days. +- Lazy deletion for removed directories that have not been accessed in > 90 + days. - Nushell: support for 0.32.0+. ### Fixed @@ -158,7 +163,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - `cd -` on fish shells. -- `__zoxide_hook` no longer changes value of `$?` within `$PROMPT_COMMAND` on bash. +- `__zoxide_hook` no longer changes value of `$?` within `$PROMPT_COMMAND` on + bash. ### Removed @@ -195,7 +201,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `$_ZO_EXCLUDE_DIRS` now supports globs. -- `zoxide init` now defines `__zoxide_z*` functions that can be aliased as needed. +- `zoxide init` now defines `__zoxide_z*` functions that can be aliased as + needed. - Support for the [xonsh](https://xon.sh/) shell. - `zoxide import` can now import from Autojump. diff --git a/Cargo.lock b/Cargo.lock index 9f91ed4..b125936 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.55" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" +checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" [[package]] name = "askama" @@ -117,6 +117,12 @@ dependencies = [ "regex-automata", ] +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + [[package]] name = "cfg-if" version = "1.0.0" @@ -125,9 +131,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.1.5" +version = "3.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ced1892c55c910c1219e98d6fc8d71f6bddba7905866ce740066d8bfea859312" +checksum = "d8c93436c21e4698bacadf42917db28b23017027a4deccb35dbe47a7e7840123" dependencies = [ "atty", "bitflags", @@ -365,6 +371,15 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.16" @@ -387,6 +402,19 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "nix" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f866317acbd3a240710c63f065ffb1e4fd466259045ccb504130b7f668f35c6" +dependencies = [ + "bitflags", + "cc", + "cfg-if", + "libc", + "memoffset", +] + [[package]] name = "nom" version = "7.1.0" @@ -500,24 +528,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" -dependencies = [ - "getrandom", -] - [[package]] name = "redox_syscall" version = "0.2.11" @@ -539,9 +549,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.4" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" dependencies = [ "aho-corasick", "memchr", @@ -695,26 +705,6 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" -[[package]] -name = "thiserror" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "thread_local" version = "1.1.4" @@ -825,12 +815,12 @@ dependencies = [ "clap_complete_fig", "dirs", "dunce", + "fastrand", "glob", + "nix", "ordered-float", - "rand", "rstest", "rstest_reuse", "serde", "tempfile", - "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index e67083b..27d729f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,17 +23,13 @@ bincode = "1.3.1" clap = { version = "3.1.0", features = ["derive"] } dirs = "4.0.0" dunce = "1.0.1" +fastrand = "1.7.0" glob = "0.3.0" ordered-float = "2.0.0" serde = { version = "1.0.116", features = ["derive"] } -tempfile = "3.1.0" -thiserror = "1.0.30" -[target.'cfg(windows)'.dependencies] -rand = { version = "0.8.4", features = [ - "getrandom", - "small_rng", -], default-features = false } +[target.'cfg(unix)'.dependencies] +nix = "0.23.1" [build-dependencies] clap = { version = "3.1.0", features = ["derive"] } @@ -44,10 +40,11 @@ clap_complete_fig = "3.1.0" assert_cmd = "2.0.0" rstest = "0.12.0" rstest_reuse = "0.3.0" +tempfile = "3.1.0" [features] default = [] -nix = [] +nix-dev = [] [profile.release] codegen-units = 1 diff --git a/src/cmd/query.rs b/src/cmd/query.rs index 109640b..f095501 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -3,10 +3,10 @@ use std::io::{self, Write}; use anyhow::{Context, Result}; use crate::cmd::{Query, Run}; +use crate::config; use crate::db::{Database, DatabaseFile}; use crate::error::BrokenPipeHandler; -use crate::fzf::Fzf; -use crate::{config, util}; +use crate::util::{self, Fzf}; impl Run for Query { fn run(&self) -> Result<()> { diff --git a/src/cmd/remove.rs b/src/cmd/remove.rs index 7cf5212..7805517 100644 --- a/src/cmd/remove.rs +++ b/src/cmd/remove.rs @@ -3,9 +3,9 @@ use std::io::{self, Write}; use anyhow::{bail, Context, Result}; use crate::cmd::{Remove, Run}; +use crate::config; use crate::db::DatabaseFile; -use crate::fzf::Fzf; -use crate::{config, util}; +use crate::util::{self, Fzf}; impl Run for Remove { fn run(&self) -> Result<()> { diff --git a/src/db/mod.rs b/src/db/mod.rs index 08062f8..f0e99d1 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,13 +2,14 @@ mod dir; mod stream; use std::fs; -use std::io::{self, Write}; +use std::io; use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; pub use dir::{Dir, DirList, Epoch, Rank}; pub use stream::Stream; -use tempfile::{NamedTempFile, PersistError}; + +use crate::util; #[derive(Debug)] pub struct Database<'file> { @@ -24,18 +25,8 @@ impl<'file> Database<'file> { } let buffer = self.dirs.to_bytes()?; - let mut file = NamedTempFile::new_in(self.data_dir) - .with_context(|| format!("could not create temporary database in: {}", self.data_dir.display()))?; - - // Preallocate enough space on the file, preventing copying later on. This optimization may - // fail on some filesystems, but it is safe to ignore it and proceed. - let _ = file.as_file().set_len(buffer.len() as _); - file.write_all(&buffer) - .with_context(|| format!("could not write to temporary database: {}", file.path().display()))?; - let path = db_path(&self.data_dir); - persist(file, &path).with_context(|| format!("could not replace database: {}", path.display()))?; - + util::write(&path, &buffer).context("could not write to database")?; self.modified = false; Ok(()) } @@ -120,44 +111,6 @@ impl<'file> Database<'file> { } } -#[cfg(unix)] -fn persist>(file: NamedTempFile, path: P) -> Result<(), PersistError> { - file.persist(path)?; - Ok(()) -} - -#[cfg(windows)] -fn persist>(mut file: NamedTempFile, path: P) -> Result<(), PersistError> { - use std::thread; - use std::time::Duration; - - use rand::distributions::{Distribution, Uniform}; - use rand::rngs::SmallRng; - use rand::SeedableRng; - - // File renames on Windows are not atomic and sometimes fail with `PermissionDenied`. This is - // extremely unlikely unless it's running in a loop on multiple threads. Nevertheless, we guard - // against it by retrying the rename a fixed number of times. - const MAX_TRIES: usize = 10; - let mut rng = None; - - for _ in 0..MAX_TRIES { - match file.persist(&path) { - Ok(_) => break, - Err(e) if e.error.kind() == io::ErrorKind::PermissionDenied => { - let mut rng = rng.get_or_insert_with(SmallRng::from_entropy); - let between = Uniform::from(50..150); - let duration = Duration::from_millis(between.sample(&mut rng)); - thread::sleep(duration); - file = e.file; - } - Err(e) => return Err(e), - } - } - - Ok(()) -} - pub struct DatabaseFile { buffer: Vec, data_dir: PathBuf, diff --git a/src/error.rs b/src/error.rs index 782e59f..4a482b8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,19 +1,20 @@ +use std::fmt::{self, Display, Formatter}; use std::io; use anyhow::{bail, Context, Result}; -use thiserror::Error; - -#[derive(Debug, Error)] -#[error("could not find fzf, is it installed?")] -pub struct FzfNotFound; /// Custom error type for early exit. -#[derive(Debug, Error)] -#[error("")] +#[derive(Debug)] pub struct SilentExit { pub code: i32, } +impl Display for SilentExit { + fn fmt(&self, _: &mut Formatter<'_>) -> fmt::Result { + Ok(()) + } +} + pub trait BrokenPipeHandler { fn pipe_exit(self, device: &str) -> Result<()>; } diff --git a/src/fzf.rs b/src/fzf.rs deleted file mode 100644 index ac4b30b..0000000 --- a/src/fzf.rs +++ /dev/null @@ -1,77 +0,0 @@ -use std::io::{self, Read}; -use std::mem; -use std::process::{Child, ChildStdin, Stdio}; - -use anyhow::{bail, Context, Result}; - -use crate::error::{FzfNotFound, SilentExit}; -use crate::{config, util}; - -pub struct Fzf { - child: Child, -} - -impl Fzf { - pub fn new(multiple: bool) -> Result { - let bin = if cfg!(windows) { "fzf.exe" } else { "fzf" }; - let mut command = util::get_command(bin).map_err(|_| FzfNotFound)?; - if multiple { - command.arg("-m"); - } - command.arg("-n2..").stdin(Stdio::piped()).stdout(Stdio::piped()); - if let Some(fzf_opts) = config::fzf_opts() { - command.env("FZF_DEFAULT_OPTS", fzf_opts); - } else { - command.args(&[ - // Search result - "--no-sort", - // Interface - "--keep-right", - // Layout - "--height=40%", - "--info=inline", - "--layout=reverse", - // Scripting - "--exit-0", - "--select-1", - // Key/Event bindings - "--bind=ctrl-z:ignore", - ]); - if cfg!(unix) { - command.env("SHELL", "sh"); - command.arg(r"--preview=\command -p ls -p {2..}"); - } - } - - let child = match command.spawn() { - Ok(child) => child, - Err(e) if e.kind() == io::ErrorKind::NotFound => bail!(FzfNotFound), - Err(e) => Err(e).context("could not launch fzf")?, - }; - - Ok(Fzf { child }) - } - - pub fn stdin(&mut self) -> &mut ChildStdin { - self.child.stdin.as_mut().unwrap() - } - - pub fn select(mut self) -> Result { - // Drop stdin to prevent deadlock. - mem::drop(self.child.stdin.take()); - - let mut stdout = self.child.stdout.take().unwrap(); - let mut output = String::new(); - stdout.read_to_string(&mut output).context("failed to read from fzf")?; - - let status = self.child.wait().context("wait failed on fzf")?; - match status.code() { - Some(0) => Ok(output), - Some(1) => bail!("no match found"), - Some(2) => bail!("fzf returned an error"), - Some(code @ 130) => bail!(SilentExit { code }), - Some(128..=254) | None => bail!("fzf was terminated"), - _ => bail!("fzf returned an unknown error"), - } - } -} diff --git a/src/main.rs b/src/main.rs index 8062110..b22b216 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ mod cmd; mod config; mod db; mod error; -mod fzf; mod shell; mod util; diff --git a/src/shell.rs b/src/shell.rs index 09418c6..79554f4 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -32,7 +32,7 @@ make_template!(Powershell, "powershell.txt"); make_template!(Xonsh, "xonsh.txt"); make_template!(Zsh, "zsh.txt"); -#[cfg(feature = "nix")] +#[cfg(feature = "nix-dev")] #[cfg(test)] mod tests { use askama::Template; diff --git a/src/util.rs b/src/util.rs index 16025c7..2ad0b16 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,172 @@ use std::env; +use std::fs::{self, File, OpenOptions}; +use std::io::{self, Read, Write}; +use std::mem; use std::path::{Component, Path, PathBuf}; use std::process::Command; +use std::process::{Child, ChildStdin, Stdio}; use std::time::SystemTime; -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; +use crate::config; use crate::db::Epoch; +use crate::error::SilentExit; + +pub struct Fzf { + child: Child, +} + +impl Fzf { + const ERR_NOT_FOUND: &'static str = "could not find fzf, is it installed?"; + + pub fn new(multiple: bool) -> Result { + let bin = if cfg!(windows) { "fzf.exe" } else { "fzf" }; + let mut command = get_command(bin).map_err(|_| anyhow!(Self::ERR_NOT_FOUND))?; + if multiple { + command.arg("-m"); + } + command.arg("-n2..").stdin(Stdio::piped()).stdout(Stdio::piped()); + if let Some(fzf_opts) = config::fzf_opts() { + command.env("FZF_DEFAULT_OPTS", fzf_opts); + } else { + command.args(&[ + // Search result + "--no-sort", + // Interface + "--keep-right", + // Layout + "--height=40%", + "--info=inline", + "--layout=reverse", + // Scripting + "--exit-0", + "--select-1", + // Key/Event bindings + "--bind=ctrl-z:ignore", + ]); + if cfg!(unix) { + command.env("SHELL", "sh"); + command.arg(r"--preview=\command -p ls -p {2..}"); + } + } + + let child = match command.spawn() { + Ok(child) => child, + Err(e) if e.kind() == io::ErrorKind::NotFound => bail!(Self::ERR_NOT_FOUND), + Err(e) => Err(e).context("could not launch fzf")?, + }; + + Ok(Fzf { child }) + } + + pub fn stdin(&mut self) -> &mut ChildStdin { + self.child.stdin.as_mut().unwrap() + } + + pub fn select(mut self) -> Result { + // Drop stdin to prevent deadlock. + mem::drop(self.child.stdin.take()); + + let mut stdout = self.child.stdout.take().unwrap(); + let mut output = String::new(); + stdout.read_to_string(&mut output).context("failed to read from fzf")?; + + let status = self.child.wait().context("wait failed on fzf")?; + match status.code() { + Some(0) => Ok(output), + Some(1) => bail!("no match found"), + Some(2) => bail!("fzf returned an error"), + Some(code @ 130) => bail!(SilentExit { code }), + Some(128..=254) | None => bail!("fzf was terminated"), + _ => bail!("fzf returned an unknown error"), + } + } +} + +/// Similar to [`fs::write`], but atomic (best effort on Windows). +pub fn write, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> { + let path = path.as_ref(); + let contents = contents.as_ref(); + let dir = path.parent().unwrap(); + + // Create a tmpfile. + let (mut tmp_file, tmp_path) = tmpfile(dir)?; + let result = (|| { + // Write to the tmpfile. + let _ = tmp_file.set_len(contents.len() as u64); + tmp_file.write_all(contents).with_context(|| format!("could not write to file: {}", tmp_path.display()))?; + + // Set the owner of the tmpfile (UNIX only). + #[cfg(unix)] + if let Ok(metadata) = path.metadata() { + use nix::unistd::{self, Gid, Uid}; + use std::os::unix::fs::MetadataExt; + use std::os::unix::io::AsRawFd; + + let uid = Uid::from_raw(metadata.uid()); + let gid = Gid::from_raw(metadata.gid()); + let _ = unistd::fchown(tmp_file.as_raw_fd(), Some(uid), Some(gid)); + } + + // Close and rename the tmpfile. + mem::drop(tmp_file); + rename(&tmp_path, path) + })(); + // In case of an error, delete the tmpfile. + if result.is_err() { + let _ = fs::remove_file(&tmp_path); + } + result +} + +/// Atomically create a tmpfile in the given directory. +fn tmpfile>(dir: P) -> Result<(File, PathBuf)> { + const MAX_ATTEMPTS: usize = 5; + const TMP_NAME_LEN: usize = 16; + let dir = dir.as_ref(); + + let mut attempts = 0; + loop { + attempts += 1; + + // Generate a random name for the tmpfile. + let mut name = String::with_capacity(TMP_NAME_LEN); + name.push_str("tmp_"); + while name.len() < TMP_NAME_LEN { + name.push(fastrand::alphanumeric()); + } + let path = dir.join(name); + + // Atomically create the tmpfile. + match OpenOptions::new().write(true).create_new(true).open(&path) { + Ok(file) => break Ok((file, path)), + Err(e) if e.kind() == io::ErrorKind::AlreadyExists && attempts < MAX_ATTEMPTS => (), + Err(e) => break Err(e).with_context(|| format!("could not create file: {}", path.display())), + } + } +} + +/// Similar to [`fs::rename`], but retries on Windows. +fn rename, Q: AsRef>(from: P, to: Q) -> Result<()> { + const MAX_ATTEMPTS: usize = 5; + let from = from.as_ref(); + let to = to.as_ref(); + + if cfg!(windows) { + let mut attempts = 0; + loop { + attempts += 1; + match fs::rename(from, to) { + Err(e) if e.kind() == io::ErrorKind::PermissionDenied && attempts < MAX_ATTEMPTS => (), + result => break result, + } + } + } else { + fs::rename(from, to) + } + .with_context(|| format!("could not rename file: {} -> {}", from.display(), to.display())) +} pub fn canonicalize>(path: &P) -> Result { dunce::canonicalize(path).with_context(|| format!("could not resolve path: {}", path.as_ref().display())) diff --git a/templates/fish.txt b/templates/fish.txt index 403622a..19cb234 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -84,8 +84,8 @@ function __zoxide_z_complete # If the last argument is empty, use interactive selection. set -l query $tokens[2..-1] set -l result (zoxide query -i -- $query) - and commandline -p "$tokens[1] "(string escape $result) - commandline -f repaint + commandline --current-process "$tokens[1] "(string escape $result) + commandline --function repaint end end diff --git a/tests/completions.rs b/tests/completions.rs index 56975d8..881242c 100644 --- a/tests/completions.rs +++ b/tests/completions.rs @@ -1,5 +1,5 @@ //! Test clap generated completions. -#![cfg(feature = "nix")] +#![cfg(feature = "nix-dev")] use assert_cmd::Command; From a0242d7c9a1e3c9f79605cace892b4e5ead9b182 Mon Sep 17 00:00:00 2001 From: skwerlman Date: Thu, 31 Mar 2022 07:20:29 -0400 Subject: [PATCH 10/13] Switch elvish template to new try/catch syntax (#371) --- .github/workflows/ci.yml | 3 ++- CHANGELOG.md | 6 ++++-- README.md | 7 ++++--- man/man1/zoxide-init.1 | 2 +- shell.nix | 2 +- templates/elvish.txt | 6 +++--- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8500f7c..b9221a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,8 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + # FIXME: Enable macos-latest when this is merged: https://nixpk.gs/pr-tracker.html?pr=163924 + os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 with: diff --git a/CHANGELOG.md b/CHANGELOG.md index 097eb03..41762c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,11 +18,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bash/Zsh: rename `_z` completion function to avoid conflicts with other shell plugins. - Elvish: upgrade to new lambda syntax. -- Fzf: added `--keep-right` option by default, upgraded minimum version to - v0.21.0. +- Fzf: added `--keep-right` option by default, upgrade minimum supported version + to v0.21.0. - Bash: only enable completions on 4.4+. - Fzf: bypass `ls` alias in preview window. - Retain ownership of database file. +- Elvish: upgrade to new try-catch syntax, upgrade minimum supported version to + v0.18.0. ## [0.8.0] - 2021-12-25 diff --git a/README.md b/README.md index 94e67e4..b366173 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ Add this to your configuration (usually `~/.elvish/rc.elv`): eval (zoxide init elvish | slurp) ``` -Note: zoxide only supports elvish v0.16.0 and above. +Note: zoxide only supports elvish v0.18.0 and above. @@ -241,8 +241,9 @@ Add this to your configuration (usually `~/.zshrc`): eval "$(zoxide init zsh)" ``` -For completions to work, the above line must be added _after_ `compinit` is -called. You may have to rebuild your cache by running `rm ~/.zcompdump*; compinit`. +For completions to work, the above line must be added *after* `compinit` is +called. You may have to rebuild your cache by running +`rm ~/.zcompdump*; compinit`. diff --git a/man/man1/zoxide-init.1 b/man/man1/zoxide-init.1 index 69a3a9e..470ddf7 100644 --- a/man/man1/zoxide-init.1 +++ b/man/man1/zoxide-init.1 @@ -20,7 +20,7 @@ Add this to your configuration (usually \fB~/.elvish/rc.elv\fR): \fBeval $(zoxide init elvish | slurp)\fR .fi .sp -Note: zoxide only supports elvish v0.16.0 and above. +Note: zoxide only supports elvish v0.18.0 and above. .TP .B fish Add this to your configuration (usually \fB~/.config/fish/config.fish\fR): diff --git a/shell.nix b/shell.nix index 1a4bd42..d2ec04a 100644 --- a/shell.nix +++ b/shell.nix @@ -2,7 +2,7 @@ let rust = import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/46d8d20fce510c6a25fa66f36e31f207f6ea49e4.tar.gz"); pkgs = import (builtins.fetchTarball - "https://github.com/NixOS/nixpkgs/archive/fae46e66a5df220327b45e0d7c27c6961cf922ce.tar.gz") { + "https://github.com/NixOS/nixpkgs/archive/d19a9162c848517cfc9437f10945b736d718b948.tar.gz") { overlays = [ rust ]; }; in pkgs.mkShell { diff --git a/templates/elvish.txt b/templates/elvish.txt index d507710..f95feed 100644 --- a/templates/elvish.txt +++ b/templates/elvish.txt @@ -57,7 +57,7 @@ fn __zoxide_z {|@rest| var path try { set path = (zoxide query --exclude $pwd -- $@rest) - } except { + } catch { } else { __zoxide_cd $path } @@ -70,7 +70,7 @@ fn __zoxide_zi {|@rest| var path try { set path = (zoxide query -i -- $@rest) - } except { + } catch { } else { __zoxide_cd $path } @@ -116,4 +116,4 @@ set edit:completion:arg-completer[{{cmd}}] = $__zoxide_z_complete~ # # eval (zoxide init elvish | slurp) # -# Note: zoxide only supports elvish v0.17.0 and above. +# Note: zoxide only supports elvish v0.18.0 and above. From b71d33b5c128744e331aa2dfd21dc39b8821a5f1 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Mon, 11 Apr 2022 03:41:51 +0530 Subject: [PATCH 11/13] Replace `--no-aliases` with `--no-cmd` --- CHANGELOG.md | 1 + Cargo.lock | 86 ++++++++++++++++++++------------- README.md | 8 +-- contrib/completions/_zoxide | 6 +-- contrib/completions/_zoxide.ps1 | 6 +-- contrib/completions/zoxide.bash | 2 +- contrib/completions/zoxide.elv | 6 +-- contrib/completions/zoxide.fish | 6 +-- contrib/completions/zoxide.ts | 78 ++++++++++++------------------ man/man1/zoxide-init.1 | 12 ++--- src/cmd/_cmd.rs | 10 ++-- src/cmd/init.rs | 2 +- templates/bash.txt | 4 +- templates/elvish.txt | 4 +- templates/fish.txt | 4 +- templates/nushell.txt | 4 +- templates/posix.txt | 4 +- templates/powershell.txt | 4 +- templates/xonsh.txt | 4 +- templates/zsh.txt | 4 +- 20 files changed, 131 insertions(+), 124 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41762c0..ba4f8f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Manpages: moved to `man/man1/*.1`. +- Replace `--no-aliases` with `--no-cmd`. ### Fixed diff --git a/Cargo.lock b/Cargo.lock index b125936..7d081ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,9 +131,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.1.6" +version = "3.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8c93436c21e4698bacadf42917db28b23017027a4deccb35dbe47a7e7840123" +checksum = "71c47df61d9e16dc010b55dba1952a57d8c215dbb533fd13cdd13369aac73b1c" dependencies = [ "atty", "bitflags", @@ -157,9 +157,9 @@ dependencies = [ [[package]] name = "clap_complete_fig" -version = "3.1.0" +version = "3.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c11f6f44afea4aee21bb57a5297879c88ac8dc97224fbbbe796edd60a098f0e" +checksum = "690eb5abb7a98df1a64a3028beaf95af7e0ceb13da3186e6d0a86161af76309e" dependencies = [ "clap", "clap_complete", @@ -167,9 +167,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.1.4" +version = "3.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da95d038ede1a964ce99f49cbe27a7fb538d1da595e4b4f70b8c8f338d17bf16" +checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1" dependencies = [ "heck", "proc-macro-error", @@ -180,9 +180,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.7" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e5bed1f1c269533fa816a0a5492b3545209a205ca1a54842be180eb63a16a6" +checksum = "0bf124c720b7686e3c2663cf54062ab0f68a88af2fb6a030e87e30bf721fcb38" dependencies = [ "cfg-if", "lazy_static", @@ -205,9 +205,9 @@ dependencies = [ [[package]] name = "dirs-sys" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" dependencies = [ "libc", "redox_users", @@ -249,9 +249,9 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "getrandom" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" +checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad" dependencies = [ "cfg-if", "libc", @@ -318,9 +318,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.8.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee" dependencies = [ "autocfg", "hashbrown", @@ -352,15 +352,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.119" +version = "0.2.122" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" +checksum = "ec647867e2bf0772e28c8bcde4f0d19a9216916e890543b5a03ed8ef27b8f259" [[package]] name = "log" -version = "0.4.14" +version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8" dependencies = [ "cfg-if", ] @@ -417,13 +417,12 @@ dependencies = [ [[package]] name = "nom" -version = "7.1.0" +version = "7.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" +checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" dependencies = [ "memchr", "minimal-lexical", - "version_check", ] [[package]] @@ -512,39 +511,40 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.15" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +checksum = "632d02bff7f874a36f33ea8bb416cd484b90cc66c1194b1a1110d067a7013f58" dependencies = [ "proc-macro2", ] [[package]] name = "redox_syscall" -version = "0.2.11" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8380fe0152551244f0747b1bf41737e0f8a74f97a14ccefd1148187271634f3c" +checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42" dependencies = [ "bitflags", ] [[package]] name = "redox_users" -version = "0.4.0" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" dependencies = [ "getrandom", "redox_syscall", + "thiserror", ] [[package]] @@ -623,9 +623,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a3381e03edd24287172047536f20cabde766e2cd3e65e6b00fb3af51c4f38d" +checksum = "d65bd28f48be7196d222d95b9243287f48d27aca604e08497513019ff0502cc4" [[package]] name = "serde" @@ -661,9 +661,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.86" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d" dependencies = [ "proc-macro2", "quote", @@ -705,6 +705,26 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" +[[package]] +name = "thiserror" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.1.4" diff --git a/README.md b/README.md index b366173..f2ccccf 100644 --- a/README.md +++ b/README.md @@ -294,8 +294,8 @@ zoxide import --from z path/to/db When calling `zoxide init`, the following flags are available: - `--cmd` - - Changes the prefix of predefined aliases (`z`, `zi`). - - `--cmd j` would change the aliases to (`j`, `ji`). + - Changes the prefix of the `z` and `zi` commands. + - `--cmd j` would change the commands to (`j`, `ji`). - `--cmd cd` would replace the `cd` command (doesn't work on Nushell / POSIX shells). - `--hook ` - Changes how often zoxide increments a directory's score: @@ -304,8 +304,8 @@ When calling `zoxide init`, the following flags are available: | `none` | Never | | `prompt` | At every shell prompt | | `pwd` | Whenever the directory is changed | -- `--no-aliases` - - Don't define aliases (`z`, `zi`). +- `--no-cmd` + - Prevents zoxide from defining the `z` and `zi` commands. - These functions will still be available in your shell as `__zoxide_z` and `__zoxide_zi`, should you choose to redefine them. diff --git a/contrib/completions/_zoxide b/contrib/completions/_zoxide index 9fb15d6..21c33ee 100644 --- a/contrib/completions/_zoxide +++ b/contrib/completions/_zoxide @@ -50,9 +50,9 @@ _arguments "${_arguments_options[@]}" \ ;; (init) _arguments "${_arguments_options[@]}" \ -'--cmd=[Renames the '\''z'\'' command and corresponding aliases]:CMD: ' \ -'--hook=[Chooses event upon which an entry is added to the database]:HOOK:(none prompt pwd)' \ -'--no-aliases[Prevents zoxide from defining any commands]' \ +'--cmd=[Changes the prefix of the `z` and `zi` commands]:CMD: ' \ +'--hook=[Changes how often zoxide increments a directory'\''s score]:HOOK:(none prompt pwd)' \ +'--no-cmd[Prevents zoxide from defining the `z` and `zi` commands]' \ '-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 ebe83de..f1e0571 100644 --- a/contrib/completions/_zoxide.ps1 +++ b/contrib/completions/_zoxide.ps1 @@ -49,9 +49,9 @@ Register-ArgumentCompleter -Native -CommandName 'zoxide' -ScriptBlock { break } 'zoxide;init' { - [CompletionResult]::new('--cmd', 'cmd', [CompletionResultType]::ParameterName, 'Renames the ''z'' command and corresponding aliases') - [CompletionResult]::new('--hook', 'hook', [CompletionResultType]::ParameterName, 'Chooses event upon which an entry is added to the database') - [CompletionResult]::new('--no-aliases', 'no-aliases', [CompletionResultType]::ParameterName, 'Prevents zoxide from defining any commands') + [CompletionResult]::new('--cmd', 'cmd', [CompletionResultType]::ParameterName, 'Changes the prefix of the `z` and `zi` commands') + [CompletionResult]::new('--hook', 'hook', [CompletionResultType]::ParameterName, 'Changes how often zoxide increments a directory''s score') + [CompletionResult]::new('--no-cmd', 'no-cmd', [CompletionResultType]::ParameterName, 'Prevents zoxide from defining the `z` and `zi` commands') [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 e4ca62d..47fbd19 100644 --- a/contrib/completions/zoxide.bash +++ b/contrib/completions/zoxide.bash @@ -80,7 +80,7 @@ _zoxide() { return 0 ;; zoxide__init) - opts="-h -V --no-aliases --cmd --hook --help --version bash elvish fish nushell posix powershell xonsh zsh" + opts="-h -V --no-cmd --cmd --hook --help --version bash elvish fish nushell posix powershell xonsh zsh" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/contrib/completions/zoxide.elv b/contrib/completions/zoxide.elv index 607c208..2e98e78 100644 --- a/contrib/completions/zoxide.elv +++ b/contrib/completions/zoxide.elv @@ -43,9 +43,9 @@ set edit:completion:arg-completer[zoxide] = {|@words| cand --version 'Print version information' } &'zoxide;init'= { - cand --cmd 'Renames the ''z'' command and corresponding aliases' - cand --hook 'Chooses event upon which an entry is added to the database' - cand --no-aliases 'Prevents zoxide from defining any commands' + cand --cmd 'Changes the prefix of the `z` and `zi` commands' + cand --hook 'Changes how often zoxide increments a directory''s score' + cand --no-cmd 'Prevents zoxide from defining the `z` and `zi` commands' 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 9945b3a..16bf84a 100644 --- a/contrib/completions/zoxide.fish +++ b/contrib/completions/zoxide.fish @@ -11,9 +11,9 @@ complete -c zoxide -n "__fish_seen_subcommand_from import" -l from -d 'Applicati complete -c zoxide -n "__fish_seen_subcommand_from import" -l merge -d 'Merge into existing database' complete -c zoxide -n "__fish_seen_subcommand_from import" -s h -l help -d 'Print help information' complete -c zoxide -n "__fish_seen_subcommand_from import" -s V -l version -d 'Print version information' -complete -c zoxide -n "__fish_seen_subcommand_from init" -l cmd -d 'Renames the \'z\' command and corresponding aliases' -r -complete -c zoxide -n "__fish_seen_subcommand_from init" -l hook -d 'Chooses event upon which an entry is added to the database' -r -f -a "{none ,prompt ,pwd }" -complete -c zoxide -n "__fish_seen_subcommand_from init" -l no-aliases -d 'Prevents zoxide from defining any commands' +complete -c zoxide -n "__fish_seen_subcommand_from init" -l cmd -d 'Changes the prefix of the `z` and `zi` commands' -r +complete -c zoxide -n "__fish_seen_subcommand_from init" -l hook -d 'Changes how often zoxide increments a directory\'s score' -r -f -a "{none ,prompt ,pwd }" +complete -c zoxide -n "__fish_seen_subcommand_from init" -l no-cmd -d 'Prevents zoxide from defining the `z` and `zi` commands' complete -c zoxide -n "__fish_seen_subcommand_from init" -s h -l help -d 'Print help information' complete -c zoxide -n "__fish_seen_subcommand_from init" -s V -l version -d 'Print version information' complete -c zoxide -n "__fish_seen_subcommand_from query" -l exclude -d 'Exclude a path from results' -r -f -a "(__fish_complete_directories)" diff --git a/contrib/completions/zoxide.ts b/contrib/completions/zoxide.ts index c3c6fb7..d665f8c 100644 --- a/contrib/completions/zoxide.ts +++ b/contrib/completions/zoxide.ts @@ -31,13 +31,9 @@ const completion: Fig.Spec = { args: { name: "from", suggestions: [ - { - name: "autojump", - }, - { - name: "z", - }, - ] + "autojump", + "z", + ], }, }, { @@ -64,7 +60,7 @@ const completion: Fig.Spec = { options: [ { name: "--cmd", - description: "Renames the 'z' command and corresponding aliases", + description: "Changes the prefix of the `z` and `zi` commands", args: { name: "cmd", isOptional: true, @@ -72,26 +68,20 @@ const completion: Fig.Spec = { }, { name: "--hook", - description: "Chooses event upon which an entry is added to the database", + description: "Changes how often zoxide increments a directory's score", args: { name: "hook", isOptional: true, suggestions: [ - { - name: "none", - }, - { - name: "prompt", - }, - { - name: "pwd", - }, - ] + "none", + "prompt", + "pwd", + ], }, }, { - name: "--no-aliases", - description: "Prevents zoxide from defining any commands", + name: "--no-cmd", + description: "Prevents zoxide from defining the `z` and `zi` commands", }, { name: ["-h", "--help"], @@ -105,31 +95,15 @@ const completion: Fig.Spec = { args: { name: "shell", suggestions: [ - { - name: "bash", - }, - { - name: "elvish", - }, - { - name: "fish", - }, - { - name: "nushell", - }, - { - name: "posix", - }, - { - name: "powershell", - }, - { - name: "xonsh", - }, - { - name: "zsh", - }, - ] + "bash", + "elvish", + "fish", + "nushell", + "posix", + "powershell", + "xonsh", + "zsh", + ], }, }, { @@ -152,14 +126,26 @@ const completion: Fig.Spec = { { name: ["-i", "--interactive"], description: "Use interactive selection", + exclusiveOn: [ + "-l", + "--list", + ], }, { name: ["-l", "--list"], description: "List all matching directories", + exclusiveOn: [ + "-i", + "--interactive", + ], }, { name: ["-s", "--score"], description: "Print score with results", + exclusiveOn: [ + "-i", + "--interactive", + ], }, { name: ["-h", "--help"], diff --git a/man/man1/zoxide-init.1 b/man/man1/zoxide-init.1 index 470ddf7..62c0bba 100644 --- a/man/man1/zoxide-init.1 +++ b/man/man1/zoxide-init.1 @@ -74,9 +74,9 @@ Add this to your configuration: .SH OPTIONS .TP .B --cmd -Changes the prefix of predefined aliases (\fBz\fR, \fBzi\fR). +Changes the prefix of the \fBz\fR and \fBzi\fR commands. .br -\fB--cmd j\fR would change the aliases to (\fBj\fR, \fBji\fR). +\fB--cmd j\fR would change the commands to (\fBj\fR, \fBji\fR). .br \fB--cmd cd\fR would replace the \fBcd\fR command (doesn't work on Nushell / POSIX shells). @@ -94,10 +94,10 @@ l l. \fBpwd\fR|Whenever the directory is changed .TE .TP -.B --no-aliases -Don't define extra aliases (\fBz\fR, \fBzi\fR). These functions will still be -available in your shell as \fB__zoxide_z\fR and \fB__zoxide_zi\fR, should you -choose to redefine them. +.B --no-cmd +Prevents zoxide from defining the \fBz\fR and \fBzi\fR commands. These functions +will still be available in your shell as \fB__zoxide_z\fR and \fB__zoxide_zi\fR, +should you choose to redefine them. .SH REPORTING BUGS For any issues, feature requests, or questions, please visit: .sp diff --git a/src/cmd/_cmd.rs b/src/cmd/_cmd.rs index d75a86f..4e1b41b 100644 --- a/src/cmd/_cmd.rs +++ b/src/cmd/_cmd.rs @@ -62,15 +62,15 @@ pub struct Init { #[clap(arg_enum)] pub shell: InitShell, - /// Prevents zoxide from defining any commands - #[clap(long)] - pub no_aliases: bool, + /// Prevents zoxide from defining the `z` and `zi` commands + #[clap(long, alias = "no-aliases")] + pub no_cmd: bool, - /// Renames the 'z' command and corresponding aliases + /// Changes the prefix of the `z` and `zi` commands #[clap(long, default_value = "z")] pub cmd: String, - /// Chooses event upon which an entry is added to the database + /// Changes how often zoxide increments a directory's score #[clap(arg_enum, long, default_value = "pwd")] pub hook: InitHook, } diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 639ec0f..30dc60c 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -10,7 +10,7 @@ use crate::shell::{self, Opts}; impl Run for Init { fn run(&self) -> Result<()> { - let cmd = if self.no_aliases { None } else { Some(self.cmd.as_str()) }; + let cmd = if self.no_cmd { None } else { Some(self.cmd.as_str()) }; let echo = config::echo(); let resolve_symlinks = config::resolve_symlinks(); diff --git a/templates/bash.txt b/templates/bash.txt index 0cd3baf..9ee60fa 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -58,7 +58,7 @@ fi {% endif -%} {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -91,7 +91,7 @@ function __zoxide_zi() { } {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} diff --git a/templates/elvish.txt b/templates/elvish.txt index f95feed..5256756 100644 --- a/templates/elvish.txt +++ b/templates/elvish.txt @@ -41,7 +41,7 @@ if (builtin:not (builtin:eq $E:__zoxide_shlvl $E:SHLVL)) { {%- endif %} {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -78,7 +78,7 @@ fn __zoxide_zi {|@rest| edit:add-var __zoxide_zi~ $__zoxide_zi~ {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} diff --git a/templates/fish.txt b/templates/fish.txt index 19cb234..c73a987 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -53,7 +53,7 @@ end {%- endif %} {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -96,7 +96,7 @@ function __zoxide_zi end {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} diff --git a/templates/nushell.txt b/templates/nushell.txt index 9874a31..0e91a42 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -45,7 +45,7 @@ $'zoxide: PWD hooks are not supported on Nushell.(char nl)Use (char sq)zoxide in {%- endmatch %} {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -86,7 +86,7 @@ def __zoxide_zi [...rest:string] { } {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} diff --git a/templates/posix.txt b/templates/posix.txt index 3ac9878..29989d3 100644 --- a/templates/posix.txt +++ b/templates/posix.txt @@ -47,7 +47,7 @@ fi {%- endmatch %} {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -77,7 +77,7 @@ __zoxide_zi() { } {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} diff --git a/templates/powershell.txt b/templates/powershell.txt index 4b7e51a..69e904c 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -67,7 +67,7 @@ if ($__zoxide_hooked -ne 1) { } {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -105,7 +105,7 @@ function __zoxide_zi { } {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} diff --git a/templates/xonsh.txt b/templates/xonsh.txt index 74a241a..f6f7802 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -108,7 +108,7 @@ if "__zoxide_hook" not in globals(): {% endif %} {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -157,7 +157,7 @@ def __zoxide_zi(args: typing.List[str]) -> None: {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} diff --git a/templates/zsh.txt b/templates/zsh.txt index bc585f4..545de74 100644 --- a/templates/zsh.txt +++ b/templates/zsh.txt @@ -46,7 +46,7 @@ fi {%- endif %} {{ section }} -# When using zoxide with --no-aliases, alias these internal functions as +# When using zoxide with --no-cmd, alias these internal functions as # desired. # @@ -85,7 +85,7 @@ function __zoxide_zi() { } {{ section }} -# Convenient aliases for zoxide. Disable these using --no-aliases. +# Commands for zoxide. Disable these using --no-cmd. # {%- match cmd %} From e6bc1576ca045f13b2133e4164237d1c2c647b41 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Mon, 11 Apr 2022 03:58:35 +0530 Subject: [PATCH 12/13] Use `target_nix` when running xtasks in Nix --- .gitignore | 1 + xtask/src/main.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 89b1201..887d2ae 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ # Compiled files and executables debug/ target/ +target_nix/ # Backup files generated by rustfmt **/*.rs.bk diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 63df538..69df33d 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -8,11 +8,10 @@ use std::path::PathBuf; use std::process::{self, Command}; fn main() -> Result<()> { - let nix_enabled = enable_nix(); - let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let dir = dir.parent().with_context(|| format!("could not find workspace root: {}", dir.display()))?; env::set_current_dir(dir).with_context(|| format!("could not set current directory: {}", dir.display()))?; + let nix_enabled = enable_nix(); let app = App::parse(); match app { @@ -147,8 +146,11 @@ fn enable_nix() -> bool { println!("Detected Nix in environment, re-running in Nix."); let args = env::args(); let cmd = shell_words::join(args); - let mut nix_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - nix_path.push("../shell.nix"); - let status = Command::new("nix-shell").args(&["--pure", "--run", &cmd, "--"]).arg(nix_path).status().unwrap(); + + let status = Command::new("nix-shell") + .args(&["--pure", "--run", &cmd, "--", "shell.nix"]) + .env("CARGO_TARGET_DIR", "target_nix") + .status() + .unwrap(); process::exit(status.code().unwrap_or(1)); } From 212585ce74e4c1b5475f7b62ebc610e6a5da56f7 Mon Sep 17 00:00:00 2001 From: Kyohei Uto Date: Wed, 13 Apr 2022 16:30:10 +0900 Subject: [PATCH 13/13] Add felix to 3rd party integrations (#379) --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f2ccccf..2c04a68 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,7 @@ They must be set before `zoxide init` is called. | ------------------ | -------------------------------------------- | -------------------------- | | [clink] | Improved cmd.exe for Windows | [clink-zoxide] | | [emacs] | Text editor | [zoxide.el] | +| [felix] | File manager | Natively supported | | [nnn] | File manager | [nnn-autojump] | | [ranger] | File manager | [ranger-zoxide] | | [telescope.nvim] | Fuzzy finder for Neovim | [telescope-zoxide] | @@ -379,6 +380,7 @@ They must be set before `zoxide init` is called. [dports]: https://github.com/DragonFlyBSD/DPorts/tree/master/sysutils/zoxide [emacs]: https://www.gnu.org/software/emacs/ [fedora packages]: https://src.fedoraproject.org/rpms/rust-zoxide +[felix]: https://github.com/kyoheiu/felix [freshports]: https://www.freshports.org/sysutils/zoxide/ [fzf-installation]: https://github.com/junegunn/fzf#installation [fzf-man]: https://manpages.ubuntu.com/manpages/en/man1/fzf.1.html