From 87bb93199917e4711a7967860ddcf2723b999307 Mon Sep 17 00:00:00 2001 From: Azalea Colburn Date: Fri, 21 Mar 2025 15:04:26 -0700 Subject: [PATCH] feat: add _ZO_ZI_ONLY_RESULT env var --- README.md | 7 +++++++ src/cmd/cmd.rs | 3 ++- src/cmd/query.rs | 31 ++++++++++++++++++++++++++++++- src/config.rs | 4 ++++ src/util.rs | 2 +- 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3c7f043..ad93cd2 100644 --- a/README.md +++ b/README.md @@ -395,6 +395,7 @@ When calling `zoxide init`, the following flags are available: - `--cmd j` would change the commands to (`j`, `ji`). - `--cmd cd` would replace the `cd` command. - `--hook ` + - Changes how often zoxide increments a directory's score: | Hook | Description | @@ -414,6 +415,7 @@ Environment variables[^2] can be used for configuration. They must be set before `zoxide init` is called. - `_ZO_DATA_DIR` + - Specifies the directory in which the database is stored. - The default value varies across OSes: @@ -427,6 +429,7 @@ Environment variables[^2] can be used for configuration. They must be set before - When set to 1, `z` will print the matched directory before navigating to it. - `_ZO_EXCLUDE_DIRS` + - Excludes the specified directories from the database. - This is provided as a list of [globs][glob], separated by OS-specific characters: @@ -437,6 +440,7 @@ Environment variables[^2] can be used for configuration. They must be set before | Windows | `;` | `$HOME;$HOME/private/*` | - By default, this is set to `"$HOME"`. + - `_ZO_FZF_OPTS` - Custom options to pass to [fzf] during interactive selection. See [`man fzf`][fzf-man] for the list of options. @@ -447,6 +451,9 @@ Environment variables[^2] can be used for configuration. They must be set before - `_ZO_RESOLVE_SYMLINKS` - When set to 1, `z` will resolve symlinks before adding directories to the database. +- `_ZO_ZI_ONLY_RESULT` + - When set to 1, `zi foo` will automatically select the only result `fzf` returns. + - However, if only one result is available during selection in the `fzf` menu, it will not be automatically selected. ## Third-party integrations diff --git a/src/cmd/cmd.rs b/src/cmd/cmd.rs index cff7e79..f29a45e 100644 --- a/src/cmd/cmd.rs +++ b/src/cmd/cmd.rs @@ -27,7 +27,8 @@ https://github.com/ajeetdsouza/zoxide {tab}_ZO_EXCLUDE_DIRS {tab}List of directory globs to be excluded {tab}_ZO_FZF_OPTS {tab}Custom flags to pass to fzf {tab}_ZO_MAXAGE {tab}Maximum total age after which entries start getting deleted -{tab}_ZO_RESOLVE_SYMLINKS{tab}Resolve symlinks when storing paths").into_resettable() +{tab}_ZO_RESOLVE_SYMLINKS{tab}Resolve symlinks when storing paths +{tab}_ZO_ZI_ONLY_RESULT{tab}Automatically select only result after fzf initialization (but not while fuzzy finding)").into_resettable() } } diff --git a/src/cmd/query.rs b/src/cmd/query.rs index 04b1ff4..426915f 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -20,7 +20,9 @@ impl Query { let now = util::current_time()?; let mut stream = self.get_stream(db, now)?; - if self.interactive { + if self.interactive && config::zi_only_result() { + self.query_interactive_only_result(&mut stream, now) + } else if self.interactive { self.query_interactive(&mut stream, now) } else if self.list { self.query_list(&mut stream, now) @@ -30,6 +32,33 @@ impl Query { } fn query_interactive(&self, stream: &mut Stream, now: Epoch) -> Result<()> { + let mut fzf = Self::get_fzf()?; + let selection = loop { + match stream.next() { + Some(dir) if Some(dir.path.as_ref()) == self.exclude.as_deref() => { + continue; + } + Some(dir) => { + if let Some(selection) = fzf.write(dir, now)? { + break selection; + } + } + None => { + break fzf.wait()?; + } + } + }; + + if self.score { + print!("{selection}"); + } else { + let path = selection.get(7..).context("could not read selection from fzf")?; + print!("{path}"); + } + Ok(()) + } + + fn query_interactive_only_result(&self, stream: &mut Stream, now: Epoch) -> Result<()> { let mut fzf = Self::get_fzf()?; let mut stream_length = 0; let mut last_dir: Option = None; diff --git a/src/config.rs b/src/config.rs index 0aeda5c..c00ab92 100644 --- a/src/config.rs +++ b/src/config.rs @@ -60,3 +60,7 @@ pub fn maxage() -> Result { pub fn resolve_symlinks() -> bool { env::var_os("_ZO_RESOLVE_SYMLINKS").is_some_and(|var| var == "1") } + +pub fn zi_only_result() -> bool { + env::var_os("_ZO_ZI_ONLY_RESULT").is_some_and(|var| var == "1") +} diff --git a/src/util.rs b/src/util.rs index a2f25ab..6aefa79 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,7 +8,7 @@ use std::{env, mem}; #[cfg(windows)] use anyhow::anyhow; -use anyhow::{Context, Error, Result, bail}; +use anyhow::{Context, Result, bail}; use crate::db::{Dir, Epoch}; use crate::error::SilentExit;