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

View File

@ -44,16 +44,17 @@ function __zoxide_hook() {
zoxide add -- "${__zoxide_pwd_old}" zoxide add -- "${__zoxide_pwd_old}"
fi fi
} }
{%- endmatch %} {%- endmatch %}
# Initialize hook. # Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1' __zoxide_hooked='1'
{%- if hook == Hook::None %} {%- if hook == Hook::None %}
{{ not_configured }} {{ not_configured }}
{%- else %} {%- else %}
PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND:+${PROMPT_COMMAND}}" PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND:+${PROMPT_COMMAND}}"
{%- endif %} {%- endif %}
fi fi
{{ section }} {{ section }}
@ -113,6 +114,7 @@ function {{cmd}}i() {
} }
{%- when None %} {%- when None %}
{{ not_configured }} {{ not_configured }}
{%- endmatch %} {%- 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 builtin:set-env __zoxide_oldpwd $pwd
before-chdir = [$@before-chdir [_]{ builtin:set-env __zoxide_oldpwd $pwd }] before-chdir = [$@before-chdir [_]{ builtin:set-env __zoxide_oldpwd $pwd }]
# edit:before-readline = [{ echo 'going to read' }] # Initialize hook to add directories to zoxide.
# after-chdir = [[dir]{ zoxide add $pwd }] {%- 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 }} {{ section }}
@ -67,6 +74,7 @@ edit:add-var z~ $__zoxide_z~
edit:add-var zi~ $__zoxide_zi~ edit:add-var zi~ $__zoxide_zi~
{%- when None %} {%- when None %}
{{ not_configured }} {{ not_configured }}
{%- endmatch %} {%- endmatch %}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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