Add hooks for Elvish

This commit is contained in:
Ajeet D'Souza 2021-04-08 11:12:12 +05:30
parent 638d804613
commit 8452961904
9 changed files with 57 additions and 44 deletions

View File

@ -139,13 +139,13 @@ mod tests {
let opts = dbg!(&opts()[i]);
let mut source = String::new();
// Filter out lines using edit:add-var, since that function
// is only available in the interactive editor.
// Filter out lines using edit:*, since those functions
// are only available in the interactive editor.
for line in Elvish(opts)
.render()
.unwrap()
.split('\n')
.filter(|line| !line.starts_with("edit:add-var"))
.filter(|line| !line.contains("edit:"))
{
source.push_str(line);
source.push('\n');

View File

@ -44,16 +44,17 @@ function __zoxide_hook() {
zoxide add -- "${__zoxide_pwd_old}"
fi
}
{%- endmatch %}
# Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- if hook == Hook::None %}
{%- if hook == Hook::None %}
{{ not_configured }}
{%- else %}
{%- else %}
PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND:+${PROMPT_COMMAND}}"
{%- endif %}
{%- endif %}
fi
{{ section }}
@ -113,6 +114,7 @@ function {{cmd}}i() {
}
{%- when None %}
{{ not_configured }}
{%- endmatch %}

View File

@ -27,8 +27,15 @@ if (not (and (builtin:has-env __zoxide_hooked) (builtin:eq (builtin:get-env __zo
builtin:set-env __zoxide_oldpwd $pwd
before-chdir = [$@before-chdir [_]{ builtin:set-env __zoxide_oldpwd $pwd }]
# edit:before-readline = [{ echo 'going to read' }]
# after-chdir = [[dir]{ zoxide add $pwd }]
# Initialize hook to add directories to zoxide.
{%- match hook %}
{%- when Hook::None %}
{{ not_configured }}
{%- when Hook::Prompt %}
edit:before-readline = [$@edit:before-readline []{ zoxide add $pwd }]
{%- when Hook::Pwd %}
after-chdir = [$@after-chdir [_]{ zoxide add $pwd }]
{%- endmatch %}
}
{{ section }}
@ -67,6 +74,7 @@ edit:add-var z~ $__zoxide_z~
edit:add-var zi~ $__zoxide_zi~
{%- when None %}
{{ not_configured }}
{%- endmatch %}

View File

@ -5,8 +5,6 @@
# Utility functions for zoxide.
#
# Remove definitions.
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd
{%- if resolve_symlinks %}
@ -19,9 +17,9 @@ end
# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd
builtin cd $argv
{%- if echo %}
{%- if echo %}
and __zoxide_pwd
{%- endif %}
{%- endif %}
and builtin commandline -f repaint
end
@ -32,14 +30,14 @@ end
# Initialize hook to add new entries to the database.
if test "$__zoxide_hooked" != 1
set __zoxide_hooked 1
{%- match hook %}
{%- when Hook::None %}
{%- match hook %}
{%- when Hook::None %}
function __zoxide_hook
{%- when Hook::Prompt %}
{%- when Hook::Prompt %}
function __zoxide_hook --on-event fish_prompt
{%- when Hook::Pwd %}
{%- when Hook::Pwd %}
function __zoxide_hook --on-variable PWD
{%- endmatch %}
{%- endmatch %}
command zoxide add -- (__zoxide_pwd)
end
end
@ -99,6 +97,7 @@ function {{cmd}}i
end
{%- when None %}
{{ not_configured }}
{%- endmatch %}

View File

@ -48,18 +48,18 @@ def __zoxide_z [...rest:string] {
cd $(zoxide query --exclude $(pwd) -- $args | str trim)
}
}
{%- if echo %}
{%- if echo %}
echo $(pwd)
{%- endif %}
{%- endif %}
}
# Jump to a directory using interactive search.
def __zoxide_zi [...rest:string] {
let args = $(echo $rest | skip 1)
cd $(zoxide query -i -- $args | str trim)
{%- if echo %}
{%- if echo %}
echo $(pwd)
{%- endif %}
{%- endif %}
}
{{ section }}
@ -73,6 +73,7 @@ alias {{cmd}} = __zoxide_z ''
alias {{cmd}}i = __zoxide_zi ''
{%- when None %}
{{ not_configured }}
{%- endmatch %}

View File

@ -42,16 +42,16 @@ __zoxide_hook() {
# Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- match hook %}
{%- when Hook::None %}
{%- match hook %}
{%- when Hook::None %}
{{ not_configured }}
{%- when Hook::Prompt %}
{%- when Hook::Prompt %}
PS1="${PS1}\$(__zoxide_hook)"
{%- when Hook::Pwd %}
{%- when Hook::Pwd %}
\printf "%s\n%s\n" \
"zoxide: PWD hooks are not supported on POSIX shells." \
" Use 'zoxide init posix --hook prompt' instead."
{%- endmatch %}
{%- endmatch %}
fi
{{ section }}
@ -109,6 +109,7 @@ __zoxide_unset '{{cmd}}i'
}
{%- when None %}
{{ not_configured }}
{%- endmatch %}

View File

@ -13,9 +13,9 @@ function __zoxide_pwd {
# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd($dir) {
Set-Location $dir -ea Stop
{%- if echo %}
{%- if echo %}
__zoxide_pwd
{%- endif %}
{%- endif %}
}
{{ section }}
@ -30,16 +30,16 @@ function __zoxide_hook {
# Initialize hook.
if ($__zoxide_hooked -ne '1') {
$__zoxide_hooked = '1'
{%- match hook %}
{%- when Hook::None %}
{%- match hook %}
{%- when Hook::None %}
{{ not_configured }}
{%- when Hook::Prompt %}
{%- when Hook::Prompt %}
$__zoxide_prompt_old = $function:prompt
function prompt {
$null = __zoxide_hook
& $__zoxide_prompt_old
}
{%- when Hook::Pwd %}
{%- when Hook::Pwd %}
if ($PSVersionTable.PSVersion.Major -ge 6) {
$ExecutionContext.InvokeCommand.LocationChangedAction = {
$null = __zoxide_hook
@ -49,7 +49,7 @@ if ($__zoxide_hooked -ne '1') {
"zoxide: PWD hooks are not supported below powershell 6.`n" +
" Use 'zoxide init powershell --hook prompt' instead.")
}
{%- endmatch %}
{%- endmatch %}
}
{{ section }}
@ -95,6 +95,7 @@ Set-Alias {{cmd}} __zoxide_z
Set-Alias {{cmd}}i __zoxide_zi
{%- when None %}
{{ not_configured }}
{%- endmatch %}

View File

@ -87,15 +87,14 @@ def __zoxide_errhandler(func):
# Initialize hook to add new entries to the database.
if globals().get("__zoxide_hooked") is not True:
globals()["__zoxide_hooked"] = True
{% match hook -%}
{%- when Hook::None -%}
{% match hook -%}
{%- when Hook::None %}
{{ not_configured }}
{%- when Hook::Prompt -%}
{%- when Hook::Prompt %}
@events.on_post_prompt # type: ignore # pylint:disable=undefined-variable
{%- when Hook::Pwd -%}
{%- when Hook::Pwd %}
@events.on_chdir # type: ignore # pylint:disable=undefined-variable
{%- endmatch %}
{%- endmatch %}
def __zoxide_hook(**_kwargs):
"""Hook to add new entries to the database."""
pwd = __zoxide_pwd()
@ -158,6 +157,7 @@ aliases["{{cmd}}"] = __zoxide_z
aliases["{{cmd}}i"] = __zoxide_zi
{%- when None %}
{{ not_configured }}
{%- endmatch %}

View File

@ -32,14 +32,14 @@ function __zoxide_hook() {
# Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- match hook %}
{%- when Hook::None %}
{%- match hook %}
{%- when Hook::None %}
{{ not_configured }}
{%- when Hook::Prompt %}
{%- when Hook::Prompt %}
precmd_functions+=(__zoxide_hook)
{%- when Hook::Pwd %}
{%- when Hook::Pwd %}
chpwd_functions=("${chpwd_functions[@]}" "__zoxide_hook")
{%- endmatch %}
{%- endmatch %}
fi
{{ section }}
@ -99,6 +99,7 @@ function {{cmd}}i() {
}
{%- when None %}
{{ not_configured }}
{%- endmatch %}