mirror of
https://github.com/max-sixty/worktrunk.git
synced 2026-09-14 20:00:38 +08:00
92 lines
4.0 KiB
Bash
92 lines
4.0 KiB
Bash
# worktrunk shell integration for zsh
|
|
#
|
|
# Completions require zsh's completion system (compinit). If completions don't work:
|
|
# autoload -Uz compinit && compinit # add before this line in .zshrc
|
|
|
|
# Only initialize if {{ cmd }} is available (in PATH or via WORKTRUNK_BIN)
|
|
if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
|
|
|
|
# Override {{ cmd }} command with split directive passing.
|
|
# Creates two temp files: one for cd (raw path) and one for exec (shell).
|
|
# WORKTRUNK_BIN can override the binary path (for testing dev builds).
|
|
{{ cmd }}() {
|
|
local use_source=false
|
|
local -a args
|
|
|
|
for arg in "$@"; do
|
|
if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
|
|
done
|
|
|
|
# Completion mode: call binary directly, no directive files needed.
|
|
# This check MUST be here (not in the binary) because clap's completion
|
|
# handler runs before argument parsing.
|
|
if [[ -n "${COMPLETE:-}" ]]; then
|
|
command "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}"
|
|
return
|
|
fi
|
|
|
|
local cd_file exec_file exit_code=0
|
|
cd_file="$(mktemp)"
|
|
exec_file="$(mktemp)"
|
|
|
|
# --source: use cargo run (builds from source)
|
|
if [[ "$use_source" == true ]]; then
|
|
WORKTRUNK_DIRECTIVE_CD_FILE="$cd_file" WORKTRUNK_DIRECTIVE_EXEC_FILE="$exec_file" \
|
|
cargo run --bin {{ cmd }} --quiet -- "${args[@]}" || exit_code=$?
|
|
else
|
|
WORKTRUNK_DIRECTIVE_CD_FILE="$cd_file" WORKTRUNK_DIRECTIVE_EXEC_FILE="$exec_file" \
|
|
command "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}" || exit_code=$?
|
|
fi
|
|
|
|
# cd file holds a raw path (no shell escaping needed).
|
|
# `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
|
|
# `alias cd=__zoxide_z`) that would otherwise be substituted into this
|
|
# function body when it's defined. See #2643.
|
|
if [[ -s "$cd_file" ]]; then
|
|
builtin cd -- "$(<"$cd_file")"
|
|
local cd_exit=$?
|
|
if [[ $exit_code -eq 0 ]]; then
|
|
exit_code=$cd_exit
|
|
fi
|
|
fi
|
|
|
|
# exec file holds arbitrary shell (e.g. from --execute)
|
|
if [[ -s "$exec_file" ]]; then
|
|
source "$exec_file"
|
|
local src_exit=$?
|
|
if [[ $exit_code -eq 0 ]]; then
|
|
exit_code=$src_exit
|
|
fi
|
|
fi
|
|
|
|
command rm -f "$cd_file" "$exec_file"
|
|
return "$exit_code"
|
|
}
|
|
|
|
# Lazy completions - generate on first TAB, then delegate to clap's completer
|
|
_{{ cmd }}_lazy_complete() {
|
|
# Generate completions function once (check if clap's function exists)
|
|
if ! (( $+functions[_clap_dynamic_completer_{{ cmd }}] )); then
|
|
# Use `command` to bypass the shell function and call the binary directly.
|
|
# Without this, `{{ cmd }}` would call the shell function which evals
|
|
# the completion script internally but doesn't re-emit it.
|
|
eval "$(COMPLETE=zsh command "${WORKTRUNK_BIN:-{{ cmd }}}" 2>/dev/null)" || return
|
|
fi
|
|
_clap_dynamic_completer_{{ cmd }} "$@"
|
|
}
|
|
|
|
# Register completion (silently skip if compinit hasn't run yet).
|
|
# We don't warn here because this script runs on every shell startup - users
|
|
# shouldn't see warnings every time they open a terminal. Instead, `wt config
|
|
# shell install` detects missing compinit and shows a one-time advisory.
|
|
if (( $+functions[compdef] )); then
|
|
compdef _{{ cmd }}_lazy_complete {{ cmd }}
|
|
# Single-column display keeps descriptions visually associated with each branch.
|
|
# Users can override: zstyle ':completion:*:{{ cmd }}:*' list-max ''
|
|
zstyle ':completion:*:{{ cmd }}:*' list-max 1
|
|
# Prevent grouping branches with identical descriptions (same timestamp) on one line.
|
|
# Without this, "release main -- + 12m" instead of separate lines per branch.
|
|
zstyle ':completion:*:*:{{ cmd }}:*' list-grouped false
|
|
fi
|
|
fi
|