mirror of
https://github.com/kristoferssolo/solorice.git
synced 2025-10-21 20:10:34 +00:00
121 lines
3.5 KiB
Bash
121 lines
3.5 KiB
Bash
#!/bin/env zsh
|
|
# =============================================================================
|
|
#
|
|
# Utility functions for zoxide.
|
|
#
|
|
|
|
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
|
|
function __zoxide_pwd() {
|
|
\builtin pwd -L
|
|
}
|
|
|
|
# cd + custom logic based on the value of _ZO_ECHO.
|
|
function __zoxide_cd() {
|
|
# shellcheck disable=SC2164
|
|
\builtin cd -- "$@"
|
|
}
|
|
|
|
# =============================================================================
|
|
#
|
|
# Hook configuration for zoxide.
|
|
#
|
|
|
|
# Hook to add new entries to the database.
|
|
function __zoxide_hook() {
|
|
# shellcheck disable=SC2312
|
|
\command zoxide add -- "$(__zoxide_pwd)"
|
|
}
|
|
|
|
# Initialize hook.
|
|
# shellcheck disable=SC2154
|
|
if [[ ${precmd_functions[(Ie)__zoxide_hook]:-} -eq 0 ]] && [[ ${chpwd_functions[(Ie)__zoxide_hook]:-} -eq 0 ]]; then
|
|
chpwd_functions+=(__zoxide_hook)
|
|
fi
|
|
|
|
# =============================================================================
|
|
#
|
|
# When using zoxide with --no-cmd, alias these internal functions as desired.
|
|
#
|
|
|
|
# Jump to a directory using only keywords.
|
|
function __zoxide_z() {
|
|
# shellcheck disable=SC2199
|
|
if [[ "$#" -eq 0 ]]; then
|
|
__zoxide_cd ~
|
|
elif [[ "$#" -eq 1 ]] && { [[ -d "$1" ]] || [[ "$1" = '-' ]] || [[ "$1" =~ ^[-+][0-9]$ ]]; }; then
|
|
__zoxide_cd "$1"
|
|
else
|
|
\builtin local result
|
|
# shellcheck disable=SC2312
|
|
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" && __zoxide_cd "${result}"
|
|
fi
|
|
}
|
|
|
|
# Jump to a directory using interactive search.
|
|
function __zoxide_zi() {
|
|
\builtin local result
|
|
result="$(\command zoxide query --interactive -- "$@")" && __zoxide_cd "${result}"
|
|
}
|
|
|
|
# =============================================================================
|
|
#
|
|
# Commands for zoxide. Disable these using --no-cmd.
|
|
#
|
|
|
|
function z() {
|
|
__zoxide_z "$@"
|
|
}
|
|
|
|
function zi() {
|
|
__zoxide_zi "$@"
|
|
}
|
|
|
|
# Completions.
|
|
if [[ -o zle ]]; then
|
|
__zoxide_result=''
|
|
|
|
function __zoxide_z_complete() {
|
|
# Only show completions when the cursor is at the end of the line.
|
|
# shellcheck disable=SC2154
|
|
[[ "${#words[@]}" -eq "${CURRENT}" ]] || return 0
|
|
|
|
if [[ "${#words[@]}" -eq 2 ]]; then
|
|
# Show completions for local directories.
|
|
_files -/
|
|
elif [[ "${words[-1]}" == '' ]]; then
|
|
# Show completions for Space-Tab.
|
|
# shellcheck disable=SC2086
|
|
__zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" --interactive -- ${words[2,-1]})" || __zoxide_result=''
|
|
|
|
# Bind '\e[0n' to helper function.
|
|
\builtin bindkey '\e[0n' '__zoxide_z_complete_helper'
|
|
# Send '\e[0n' to console input.
|
|
\builtin printf '\e[5n'
|
|
fi
|
|
|
|
# Report that the completion was successful, so that we don't fall back
|
|
# to another completion function.
|
|
return 0
|
|
}
|
|
|
|
function __zoxide_z_complete_helper() {
|
|
if [[ -n "${__zoxide_result}" ]]; then
|
|
# shellcheck disable=SC2034,SC2296
|
|
BUFFER="z ${(q-)__zoxide_result}"
|
|
\builtin zle reset-prompt
|
|
\builtin zle accept-line
|
|
else
|
|
\builtin zle reset-prompt
|
|
fi
|
|
}
|
|
\builtin zle -N __zoxide_z_complete_helper
|
|
|
|
[[ "${+functions[compdef]}" -ne 0 ]] && \compdef __zoxide_z_complete z
|
|
fi
|
|
|
|
# =============================================================================
|
|
#
|
|
# To initialize zoxide, add this to your configuration (usually ~/.zshrc):
|
|
#
|
|
eval "$(zoxide init zsh)"
|