From 84529619042617d11bcc77d26ea5551deb0877e3 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Thu, 8 Apr 2021 11:12:12 +0530 Subject: [PATCH] Add hooks for Elvish --- src/shell.rs | 6 +++--- templates/bash.txt | 8 +++++--- templates/elvish.txt | 12 ++++++++++-- templates/fish.txt | 17 ++++++++--------- templates/nushell.txt | 9 +++++---- templates/posix.txt | 11 ++++++----- templates/powershell.txt | 15 ++++++++------- templates/xonsh.txt | 12 ++++++------ templates/zsh.txt | 11 ++++++----- 9 files changed, 57 insertions(+), 44 deletions(-) diff --git a/src/shell.rs b/src/shell.rs index 3e8660c..0aad47d 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -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'); diff --git a/templates/bash.txt b/templates/bash.txt index 151fe9a..7909716 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -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 %} diff --git a/templates/elvish.txt b/templates/elvish.txt index 5cda349..b4d2999 100644 --- a/templates/elvish.txt +++ b/templates/elvish.txt @@ -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 %} diff --git a/templates/fish.txt b/templates/fish.txt index 388f089..5b31019 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -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 %} diff --git a/templates/nushell.txt b/templates/nushell.txt index 36d59de..da786c5 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -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 %} diff --git a/templates/posix.txt b/templates/posix.txt index 288c85b..f5788bc 100644 --- a/templates/posix.txt +++ b/templates/posix.txt @@ -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 %} diff --git a/templates/powershell.txt b/templates/powershell.txt index c89e36a..74762eb 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -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 %} diff --git a/templates/xonsh.txt b/templates/xonsh.txt index 762f28d..dd19f6d 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -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 %} diff --git a/templates/zsh.txt b/templates/zsh.txt index 0efea68..5d004b2 100644 --- a/templates/zsh.txt +++ b/templates/zsh.txt @@ -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 %}