Skip to content

Commit

Permalink
Add z completions for zsh (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Dec 5, 2021
1 parent 72fd48e commit d365bfd
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, windows-latest] # FIXME: macos-latest
steps:
- uses: actions/checkout@v2

Expand All @@ -28,5 +28,5 @@ jobs:

- run: cargo xtask ci
if: ${{ matrix.os == 'windows-latest' }}
- run: nix-shell --cores 0 --pure --run 'cargo xtask ci'
- run: nix-shell --cores 0 --pure --run 'rm -rf ~/.cargo/bin; cargo xtask ci'
if: ${{ matrix.os != 'windows-latest' }}
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Zsh: completions for `z` command.

### Changed

- fzf: better default options.
- fish: interactive completions are only triggered when the last argument is empty.
- Fzf: better default options.
- Fish: interactive completions are only triggered when the last argument is empty.

### Fixed

- PowerShell: use global scope for aliases.
- Zsh: fix errors with `set -eu`.
- fzf: handle early selection.
- Fzf: handle early selection.

## [0.7.9] - 2021-11-02

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Ajeet D'Souza <[email protected]>"]
categories = ["command-line-utilities", "filesystem"]
description = "A smarter cd command for your terminal"
edition = "2018"
edition = "2021"
keywords = ["cli"]
license = "MIT"
name = "zoxide"
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ zi foo # cd with interactive selection (using fzf)

Read more about the matching algorithm [here][algorithm-matching].

Starting from v0.7.10, you can also use interactive completions on Bash/Fish/Zsh:

```sh
z foo bar<SPACE><TAB> # show interactive completions
```

## Installation

### *Step 1: Install zoxide*
Expand Down Expand Up @@ -231,6 +237,8 @@ Add this to your configuration (usually `~/.zshrc`):
eval "$(zoxide init zsh)"
```

For completions to work, this line must be added _after_ calling `compinit`.

</details>

<details>
Expand Down Expand Up @@ -347,16 +355,16 @@ They must be set before `zoxide init` is called.
[algorithm-matching]: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#matching
[alpine linux packages]: https://pkgs.alpinelinux.org/packages?name=zoxide
[arch linux community]: https://archlinux.org/packages/community/x86_64/zoxide/
[builtwithnix-badge]: https://img.shields.io/badge/builtwith-nix-7d81f7
[builtwithnix-badge]: https://img.shields.io/badge/builtwith-nix-7d81f7?style=flat-square
[builtwithnix]: https://builtwithnix.org/
[chocolatey]: https://community.chocolatey.org/packages/zoxide
[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
[crates.io-badge]: https://img.shields.io/crates/v/zoxide?style=flat-square
[crates.io]: https://crates.io/crates/zoxide
[debian packages]: https://packages.debian.org/stable/admin/zoxide
[devuan packages]: https://pkginfo.devuan.org/cgi-bin/package-query.html?c=package&q=zoxide
[downloads-badge]: https://img.shields.io/github/downloads/ajeetdsouza/zoxide/total
[downloads-badge]: https://img.shields.io/github/downloads/ajeetdsouza/zoxide/total?style=flat-square
[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
Expand Down
38 changes: 19 additions & 19 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
let
rust = import (builtins.fetchTarball
"https://github.com/oxalica/rust-overlay/archive/ad311f5bb5c5ef475985f1e0f264e831470a8510.tar.gz");
pkgs = import <nixpkgs> { overlays = [ rust ]; };
pkgs-latest = import (builtins.fetchTarball
"https://github.com/NixOS/nixpkgs/archive/3ef1d2a9602c18f8742e1fb63d5ae9867092e3d6.tar.gz")
{ };
"https://github.com/oxalica/rust-overlay/archive/783722a22ee5d762ac5c1c7b418b57b3010c827a.tar.gz");
pkgs = import (builtins.fetchTarball
"https://github.com/NixOS/nixpkgs/archive/58f87c20e1abbbe835f1f3106ecea10fd93c4a90.tar.gz") {
overlays = [ rust ];
};
in pkgs.mkShell {
buildInputs = [
# Rust
pkgs.rust-bin.stable.latest.default

# Shells
pkgs-latest.elvish
pkgs-latest.fish
pkgs-latest.nushell
pkgs-latest.xonsh
pkgs.bash
pkgs.dash
pkgs.elvish
pkgs.fish
pkgs.nushell
pkgs.powershell
pkgs.xonsh
pkgs.zsh

# Tools
pkgs-latest.cargo-audit
pkgs-latest.mandoc
pkgs-latest.nixfmt
pkgs-latest.nodePackages.markdownlint-cli
pkgs-latest.python3Packages.black
pkgs-latest.python3Packages.mypy
pkgs-latest.python3Packages.pylint
pkgs-latest.shellcheck
pkgs-latest.shfmt
pkgs.cargo-audit
pkgs.mandoc
pkgs.nixfmt
pkgs.nodePackages.markdownlint-cli
pkgs.python3Packages.black
pkgs.python3Packages.mypy
pkgs.python3Packages.pylint
pkgs.shellcheck
pkgs.shfmt

# Dependencies
pkgs.cacert
pkgs.libiconv
pkgs.fzf
pkgs.git
pkgs.libiconv
];

RUST_BACKTRACE = 1;
Expand Down
7 changes: 4 additions & 3 deletions src/app/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ impl Query {

if self.interactive {
let mut fzf = Fzf::new(false)?;
let stdin = fzf.stdin();

let selection = loop {
let dir = match stream.next() {
Some(dir) => dir,
None => break fzf.select()?,
};

match writeln!(fzf.stdin(), "{}", dir.display_score(now)) {
Ok(()) => (()),
match writeln!(stdin, "{}", dir.display_score(now)) {
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => break fzf.select()?,
Err(e) => Err(e).context("could not write to fzf")?,
result => result.context("could not write to fzf")?,
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/app/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ impl Run for Remove {
let mut stream = db.stream(now).with_keywords(keywords);

let mut fzf = Fzf::new(true)?;
let stdin = fzf.stdin();

let selection = loop {
let dir = match stream.next() {
Some(dir) => dir,
None => break fzf.select()?,
};

match writeln!(fzf.stdin(), "{}", dir.display_score(now)) {
Ok(()) => (()),
match writeln!(stdin, "{}", dir.display_score(now)) {
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => break fzf.select()?,
Err(e) => Err(e).context("could not write to fzf")?,
result => result.context("could not write to fzf")?,
}
};

Expand Down
1 change: 0 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::ffi::OsString;
use std::path::PathBuf;

use anyhow::{bail, Context, Result};
use dirs;
use glob::Pattern;

use crate::db::Rank;
Expand Down
2 changes: 1 addition & 1 deletion src/fzf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Fzf {
command.args(&[
"--bind=ctrl-z:ignore",
"--exit-0",
"--height=35%",
"--height=40%",
"--inline-info",
"--no-sort",
"--reverse",
Expand Down
19 changes: 11 additions & 8 deletions templates/bash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ function __zoxide_cd() {
{%- if hook == InitHook::Prompt %}
function __zoxide_hook() {
\builtin local -r retval="$?"
\builtin command zoxide add -- "$(__zoxide_pwd)"
\command zoxide add -- "$(__zoxide_pwd || \builtin true)"
return "${retval}"
}
{%- else if hook == InitHook::Pwd %}
__zoxide_oldpwd="$(__zoxide_pwd)"

function __zoxide_hook() {
\builtin local -r retval="$?"
\builtin local -r pwd_tmp="$(__zoxide_pwd)"
\builtin local pwd_tmp
pwd_tmp="$(__zoxide_pwd)"
if [[ ${__zoxide_oldpwd} != "${pwd_tmp}" ]]; then
__zoxide_oldpwd="${pwd_tmp}"
\builtin command zoxide add -- "${__zoxide_oldpwd}"
\command zoxide add -- "${__zoxide_oldpwd}"
fi
return "${retval}"
}
Expand Down Expand Up @@ -76,14 +77,15 @@ function __zoxide_z() {
__zoxide_cd "${result:2}"
else
\builtin local result
result="$(\builtin command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" && __zoxide_cd "${result}"
result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" -- "$@")" &&
__zoxide_cd "${result}"
fi
}

# Jump to a directory using interactive search.
function __zoxide_zi() {
\builtin local result
result="$(\builtin command zoxide query -i -- "$@")" && __zoxide_cd "${result}"
result="$(\command zoxide query -i -- "$@")" && __zoxide_cd "${result}"
}

{{ section }}
Expand Down Expand Up @@ -123,11 +125,12 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): && ${TERM} != 'dumb' ]]; then

# If there is only one argument, use `cd` completions.
if [[ {{ "${#COMP_WORDS[@]}" }} -eq 2 ]]; then
\builtin mapfile -t COMPREPLY < <(compgen -A directory -S / -- "${COMP_WORDS[-1]}")
\builtin mapfile -t COMPREPLY < \
<(\builtin compgen -A directory -S / -- "${COMP_WORDS[-1]}" || \builtin true)
# If there is a space after the last word, use interactive selection.
elif [[ -z ${COMP_WORDS[-1]} ]]; then
\local result
result="$(\builtin command zoxide query -i -- "${COMP_WORDS[@]:1}")" &&
\builtin local result
result="$(\command zoxide query -i -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" &&
COMPREPLY=("${__zoxide_z_prefix}${result}")
\builtin printf '\e[5n'
fi
Expand Down
23 changes: 12 additions & 11 deletions templates/posix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
__zoxide_pwd() {
{%- if resolve_symlinks %}
\pwd -P
\command pwd -P
{%- else %}
\pwd -L
\command pwd -L
{%- endif %}
}

# cd + custom logic based on the value of _ZO_ECHO.
__zoxide_cd() {
# shellcheck disable=SC2164
\cd "$@" {%- if echo %} && __zoxide_pwd {%- endif %}
\command cd "$@" {%- if echo %} && __zoxide_pwd {%- endif %}
}

{{ section }}
Expand All @@ -31,7 +31,7 @@ __zoxide_cd() {
{%- when InitHook::Prompt -%}
# Hook to add new entries to the database.
__zoxide_hook() {
zoxide add -- "$(__zoxide_pwd)"
\command zoxide add -- "$(__zoxide_pwd || \builtin true)"
}

# Initialize hook.
Expand All @@ -40,7 +40,7 @@ if [ "${PS1:=}" = "${PS1#*\$(__zoxide_hook)}" ]; then
fi

{%- when InitHook::Pwd -%}
\printf "%s\n%s\n" \
\command printf "%s\n%s\n" \
"zoxide: PWD hooks are not supported on POSIX shells." \
" Use 'zoxide init posix --hook prompt' instead."

Expand All @@ -60,19 +60,20 @@ __zoxide_z() {
__zoxide_cd "${OLDPWD}"
else
# shellcheck disable=SC2016
\printf 'zoxide: $OLDPWD is not set'
\command printf 'zoxide: $OLDPWD is not set'
return 1
fi
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then
__zoxide_cd "$1"
else
__zoxide_result="$(zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" && __zoxide_cd "${__zoxide_result}"
__zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" -- "$@")" &&
__zoxide_cd "${__zoxide_result}"
fi
}

# Jump to a directory using interactive search.
__zoxide_zi() {
__zoxide_result="$(zoxide query -i -- "$@")" && __zoxide_cd "${__zoxide_result}"
__zoxide_result="$(\command zoxide query -i -- "$@")" && __zoxide_cd "${__zoxide_result}"
}

{{ section }}
Expand All @@ -84,10 +85,10 @@ __zoxide_zi() {

# Remove definitions.
__zoxide_unset() {
\unset -f "$@" >/dev/null 2>&1
\unset -v "$@" >/dev/null 2>&1
\command unset -f "$@" >/dev/null 2>&1
\command unset -v "$@" >/dev/null 2>&1
# shellcheck disable=SC1001
\unalias "$@" >/dev/null 2>&1 || \:
\command unalias "$@" >/dev/null 2>&1 || \:
}

__zoxide_unset '{{cmd}}'
Expand Down
Loading

0 comments on commit d365bfd

Please sign in to comment.