Support for PWD hooks on all versions of PowerShell (#563)

This commit is contained in:
Ajeet D'Souza 2023-05-07 19:19:53 +05:30 committed by GitHub
parent 0ecfbf7e68
commit 74ccc0232f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 73 deletions

View File

@ -17,23 +17,23 @@ diverse, inclusive, and healthy community.
Examples of behavior that contributes to a positive environment for our Examples of behavior that contributes to a positive environment for our
community include: community include:
* Demonstrating empathy and kindness toward other people - Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences - Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback - Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes, - Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience and learning from the experience
* Focusing on what is best not just for us as individuals, but for the - Focusing on what is best not just for us as individuals, but for the
overall community overall community
Examples of unacceptable behavior include: Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or - The use of sexualized language or imagery, and sexual attention or
advances of any kind advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks - Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment - Public or private harassment
* Publishing others' private information, such as a physical or email - Publishing others' private information, such as a physical or email
address, without their explicit permission address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a - Other conduct which could reasonably be considered inappropriate in a
professional setting professional setting
## Enforcement Responsibilities ## Enforcement Responsibilities

View File

@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fish/Zsh: aliases on `__zoxide_z` will now use completions. - Fish/Zsh: aliases on `__zoxide_z` will now use completions.
- Nushell: add support for v0.78.0. - Nushell: add support for v0.78.0.
- Fish: plugin now works on older versions. - Fish: plugin now works on older versions.
- PowerShell: warn when PowerShell version is too old for `z -` and `z +`.
- PowerShell: support for PWD hooks on all versions.
### Fixed ### Fixed
@ -36,7 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Nushell: upgrade minimum supported version to v0.73.0. - Nushell: upgrade minimum supported version to v0.73.0.
- Zsh: fix extra space in interactive completions when no match is found. - Zsh: fix extra space in interactive completions when no match is found.
- Fzf: various improvements, upgrade minimum supported version to v0.33.0. - Fzf: various improvements, upgrade minimum supported version to v0.33.0.
- Nushell: Accidental redefinition of hooks when initialized twice. - Nushell: accidental redefinition of hooks when initialized twice.
### Removed ### Removed
@ -140,7 +142,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- PowerShell: Hook not initializing correctly. - PowerShell: hook not initializing correctly.
## [0.7.6] - 2021-10-13 ## [0.7.6] - 2021-10-13

View File

@ -71,10 +71,10 @@ zoxide can be installed in 4 easy steps:
> >
> | Distribution | Repository | Instructions | > | Distribution | Repository | Instructions |
> | ------------------- | ----------------------- | ----------------------------------------------------------------------------------------------------- | > | ------------------- | ----------------------- | ----------------------------------------------------------------------------------------------------- |
> | ***Any*** | **[crates.io]** | `cargo install zoxide --locked` | > | **_Any_** | **[crates.io]** | `cargo install zoxide --locked` |
> | *Any* | [asdf] | `asdf plugin add zoxide https://github.com/nyrst/asdf-zoxide.git` <br /> `asdf install zoxide latest` | > | _Any_ | [asdf] | `asdf plugin add zoxide https://github.com/nyrst/asdf-zoxide.git` <br /> `asdf install zoxide latest` |
> | *Any* | [conda-forge] | `conda install -c conda-forge zoxide` | > | _Any_ | [conda-forge] | `conda install -c conda-forge zoxide` |
> | *Any* | [Linuxbrew] | `brew install zoxide` | > | _Any_ | [Linuxbrew] | `brew install zoxide` |
> | Alpine Linux 3.13+ | [Alpine Linux Packages] | `apk add zoxide` | > | Alpine Linux 3.13+ | [Alpine Linux Packages] | `apk add zoxide` |
> | Arch Linux | [Arch Linux Community] | `pacman -S zoxide` | > | Arch Linux | [Arch Linux Community] | `pacman -S zoxide` |
> | CentOS 7+ | [Copr] | `dnf copr enable atim/zoxide` <br /> `dnf install zoxide` | > | CentOS 7+ | [Copr] | `dnf copr enable atim/zoxide` <br /> `dnf install zoxide` |
@ -147,7 +147,7 @@ zoxide can be installed in 4 easy steps:
> >
> | Distribution | Repository | Instructions | > | Distribution | Repository | Instructions |
> | ------------- | --------------- | ------------------------------- | > | ------------- | --------------- | ------------------------------- |
> | ***Any*** | **[crates.io]** | `cargo install zoxide --locked` | > | **_Any_** | **[crates.io]** | `cargo install zoxide --locked` |
> | DragonFly BSD | [DPorts] | `pkg install zoxide` | > | DragonFly BSD | [DPorts] | `pkg install zoxide` |
> | FreeBSD | [FreshPorts] | `pkg install zoxide` | > | FreeBSD | [FreshPorts] | `pkg install zoxide` |
> | NetBSD | [pkgsrc] | `pkgin install zoxide` | > | NetBSD | [pkgsrc] | `pkgin install zoxide` |
@ -232,22 +232,8 @@ zoxide can be installed in 4 easy steps:
> Add this to your configuration (find it by running `echo $profile` in > Add this to your configuration (find it by running `echo $profile` in
> PowerShell): > PowerShell):
> >
> - For zoxide v0.8.0+:
>
> ```powershell > ```powershell
> Invoke-Expression (& { > Invoke-Expression (& { (zoxide init powershell | Out-String) })
> $hook = if ($PSVersionTable.PSVersion.Major -lt 6) { 'prompt' } else { 'pwd' }
> (zoxide init --hook $hook powershell | Out-String)
> })
> ```
>
> - For older versions of zoxide:
>
> ```powershell
> Invoke-Expression (& {
> $hook = if ($PSVersionTable.PSVersion.Major -lt 6) { 'prompt' } else { 'pwd' }
> (zoxide init --hook $hook powershell) -join "`n"
> })
> ``` > ```
</details> </details>
@ -272,7 +258,7 @@ zoxide can be installed in 4 easy steps:
> eval "$(zoxide init zsh)" > eval "$(zoxide init zsh)"
> ``` > ```
> >
> For completions to work, the above line must be added *after* `compinit` is > For completions to work, the above line must be added _after_ `compinit` is
> called. You may have to rebuild your completions cache by running > called. You may have to rebuild your completions cache by running
> `rm ~/.zcompdump*; compinit`. > `rm ~/.zcompdump*; compinit`.
@ -434,10 +420,13 @@ Environment variables[^2] can be used for configuration. They must be set before
| [zabb] | Finds the shortest possible query for a path | Natively supported | | [zabb] | Finds the shortest possible query for a path | Natively supported |
| [zsh-autocomplete] | Realtime completions for zsh | Natively supported | | [zsh-autocomplete] | Realtime completions for zsh | Natively supported |
[^1]: Debian / Ubuntu derivatives update their packages very slowly. If you're [^1]:
using one of these distributions, consider using the install script instead. Debian / Ubuntu derivatives update their packages very slowly. If you're
[^2]: If you're not sure how to set an environment variable on your shell, check using one of these distributions, consider using the install script instead.
out the [wiki][wiki-env].
[^2]:
If you're not sure how to set an environment variable on your shell, check
out the [wiki][wiki-env].
[algorithm-aging]: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#aging [algorithm-aging]: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#aging
[algorithm-matching]: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#matching [algorithm-matching]: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#matching

View File

@ -50,10 +50,7 @@ Add this to your configuration (find it by running \fBecho $profile\fR in
PowerShell): PowerShell):
.sp .sp
.nf .nf
\fBInvoke-Expression (& { \fBInvoke-Expression (& { (zoxide init powershell | Out-String) })\fR
$hook = if ($PSVersionTable.PSVersion.Major -lt 6) { 'prompt' } else { 'pwd' }
(zoxide init --hook $hook powershell | Out-String)
})\fR
.fi .fi
.TP .TP
.B xonsh .B xonsh

View File

@ -30,8 +30,16 @@ function __zoxide_cd($dir, $literal) {
$dir = if ($literal) { $dir = if ($literal) {
Set-Location -LiteralPath $dir -Passthru -ErrorAction Stop Set-Location -LiteralPath $dir -Passthru -ErrorAction Stop
} else { } else {
if ($dir -eq '-' -and ($PSVersionTable.PSVersion -lt 6.1)) {
Write-Error "cd - is not supported below PowerShell 6.1. Please upgrade your version of PowerShell."
}
elseif ($dir -eq '+' -and ($PSVersionTable.PSVersion -lt 6.2)) {
Write-Error "cd + is not supported below PowerShell 6.2. Please upgrade your version of PowerShell."
}
else {
Set-Location -Path $dir -Passthru -ErrorAction Stop Set-Location -Path $dir -Passthru -ErrorAction Stop
} }
}
{%- if echo %} {%- if echo %}
Write-Output $dir.Path Write-Output $dir.Path
{%- endif %} {%- endif %}
@ -41,6 +49,15 @@ function __zoxide_cd($dir, $literal) {
# Hook configuration for zoxide. # Hook configuration for zoxide.
# #
{% if hook == InitHook::None -%}
{{ not_configured }}
{%- else -%}
{#-
Initialize $__zoxide_hooked if it does not exist. Removing this will cause an
unset variable error in StrictMode.
-#}
{%- if hook == InitHook::Prompt -%}
# Hook to add new entries to the database. # Hook to add new entries to the database.
function __zoxide_hook { function __zoxide_hook {
$result = __zoxide_pwd $result = __zoxide_pwd
@ -48,37 +65,34 @@ function __zoxide_hook {
zoxide add -- $result zoxide add -- $result
} }
} }
{%- else if hook == InitHook::Pwd -%}
# Hook to add new entries to the database.
$global:__zoxide_oldpwd = __zoxide_pwd
function __zoxide_hook {
$result = __zoxide_pwd
if ($result -ne $global:__zoxide_oldpwd) {
if ($null -ne $result) {
zoxide add -- $result
}
$global:__zoxide_oldpwd = $result
}
}
{%- endif %}
# Initialize hook. # Initialize hook.
{#- $__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction SilentlyContinue -ValueOnly)
Initialize $__zoxide_hooked if it does not exist. Removing this will cause an
unset variable error in StrictMode.
#}
$__zoxide_hooked = (Get-Variable __zoxide_hooked -ValueOnly -ErrorAction SilentlyContinue)
if ($__zoxide_hooked -ne 1) { if ($__zoxide_hooked -ne 1) {
$__zoxide_hooked = 1 $__zoxide_hooked = 1
{%- match hook %} $__zoxide_prompt_old = $function:prompt
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
$prompt_old = $function:prompt
function prompt { function prompt {
$null = __zoxide_hook if ($null -ne $__zoxide_prompt_old) {
& $prompt_old & $__zoxide_prompt_old
} }
{%- when InitHook::Pwd %}
if ($PSVersionTable.PSVersion.Major -ge 6) {
$ExecutionContext.InvokeCommand.LocationChangedAction = {
$null = __zoxide_hook $null = __zoxide_hook
} }
}
else {
Write-Error ("`n" +
"zoxide: PWD hooks are not supported below powershell 6.`n" +
" Use 'zoxide init powershell --hook prompt' instead.")
}
{%- endmatch %}
} }
{%- endif %}
{{ section }} {{ section }}
# When using zoxide with --no-cmd, alias these internal functions as desired. # When using zoxide with --no-cmd, alias these internal functions as desired.
@ -89,12 +103,12 @@ function __zoxide_z {
if ($args.Length -eq 0) { if ($args.Length -eq 0) {
__zoxide_cd ~ $true __zoxide_cd ~ $true
} }
elseif ( elseif ($args.Length -eq 1 -and ($args[0] -eq '-' -or $args[0] -eq '+')) {
$args.Length -eq 1 -and
(($args[0] -eq '-' -or $args[0] -eq '+') -or (Test-Path $args[0] -PathType Container))
) {
__zoxide_cd $args[0] $false __zoxide_cd $args[0] $false
} }
elseif ($args.Length -eq 1 -and (Test-Path $args[0] -PathType Container)) {
__zoxide_cd $args[0] $true
}
else { else {
$result = __zoxide_pwd $result = __zoxide_pwd
if ($null -ne $result) { if ($null -ne $result) {
@ -137,4 +151,4 @@ Set-Alias -Name {{cmd}}i -Value __zoxide_zi -Option AllScope -Scope Global -Forc
# To initialize zoxide, add this to your configuration (find it by running # To initialize zoxide, add this to your configuration (find it by running
# `echo $profile` in PowerShell): # `echo $profile` in PowerShell):
# #
# Invoke-Expression (& { $hook = if ($PSVersionTable.PSVersion.Major -ge 6) { 'pwd' } else { 'prompt' } (zoxide init powershell --hook $hook | Out-String) }) # Invoke-Expression (& { (zoxide init powershell | Out-String) })