feat: add _ZO_ZI_ONLY_RESULT env var

This commit is contained in:
Azalea Colburn 2025-03-21 15:04:26 -07:00
parent bea2225b68
commit 87bb931999
No known key found for this signature in database
5 changed files with 44 additions and 3 deletions

View File

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

View File

@ -27,7 +27,8 @@ https://github.com/ajeetdsouza/zoxide
{tab}<bold>_ZO_EXCLUDE_DIRS</bold> {tab}List of directory globs to be excluded
{tab}<bold>_ZO_FZF_OPTS</bold> {tab}Custom flags to pass to fzf
{tab}<bold>_ZO_MAXAGE</bold> {tab}Maximum total age after which entries start getting deleted
{tab}<bold>_ZO_RESOLVE_SYMLINKS</bold>{tab}Resolve symlinks when storing paths").into_resettable()
{tab}<bold>_ZO_RESOLVE_SYMLINKS</bold>{tab}Resolve symlinks when storing paths
{tab}<bold>_ZO_ZI_ONLY_RESULT</bold>{tab}Automatically select only result after fzf initialization (but not while fuzzy finding)").into_resettable()
}
}

View File

@ -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<String> = None;

View File

@ -60,3 +60,7 @@ pub fn maxage() -> Result<Rank> {
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")
}

View File

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