Use global scope for PowerShell variables / functions (#597)

Co-authored-by: Ajeet D'Souza <98ajeet@gmail.com>
This commit is contained in:
matan h 2023-08-04 01:55:48 +00:00 committed by GitHub
parent 42e1d5dd3b
commit 3898d99f0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View File

@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Short option `-a` for `zoxide query --all`. - Short option `-a` for `zoxide query --all`.
### Fixed
- PowerShell: use `global` scope for variables / functions.
## [0.9.1] - 2023-05-07 ## [0.9.1] - 2023-05-07
### Added ### Added

View File

@ -6,7 +6,7 @@
# #
# Call zoxide binary, returning the output as UTF-8. # Call zoxide binary, returning the output as UTF-8.
function __zoxide_bin { function global:__zoxide_bin {
$encoding = [Console]::OutputEncoding $encoding = [Console]::OutputEncoding
try { try {
[Console]::OutputEncoding = [System.Text.Utf8Encoding]::new() [Console]::OutputEncoding = [System.Text.Utf8Encoding]::new()
@ -18,7 +18,7 @@ function __zoxide_bin {
} }
# pwd based on zoxide's format. # pwd based on zoxide's format.
function __zoxide_pwd { function global:__zoxide_pwd {
$cwd = Get-Location $cwd = Get-Location
if ($cwd.Provider.Name -eq "FileSystem") { if ($cwd.Provider.Name -eq "FileSystem") {
$cwd.ProviderPath $cwd.ProviderPath
@ -26,7 +26,7 @@ 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, $literal) { function global:__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 {
@ -59,7 +59,7 @@ function __zoxide_cd($dir, $literal) {
-#} -#}
{%- if hook == InitHook::Prompt -%} {%- if hook == InitHook::Prompt -%}
# Hook to add new entries to the database. # Hook to add new entries to the database.
function __zoxide_hook { function global:__zoxide_hook {
$result = __zoxide_pwd $result = __zoxide_pwd
if ($null -ne $result) { if ($null -ne $result) {
zoxide add -- $result zoxide add -- $result
@ -68,7 +68,7 @@ function __zoxide_hook {
{%- else if hook == InitHook::Pwd -%} {%- else if hook == InitHook::Pwd -%}
# Hook to add new entries to the database. # Hook to add new entries to the database.
$global:__zoxide_oldpwd = __zoxide_pwd $global:__zoxide_oldpwd = __zoxide_pwd
function __zoxide_hook { function global:__zoxide_hook {
$result = __zoxide_pwd $result = __zoxide_pwd
if ($result -ne $global:__zoxide_oldpwd) { if ($result -ne $global:__zoxide_oldpwd) {
if ($null -ne $result) { if ($null -ne $result) {
@ -80,12 +80,12 @@ function __zoxide_hook {
{%- endif %} {%- endif %}
# Initialize hook. # Initialize hook.
$__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction SilentlyContinue -ValueOnly) $global:__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction SilentlyContinue -ValueOnly)
if ($__zoxide_hooked -ne 1) { if ($global:__zoxide_hooked -ne 1) {
$__zoxide_hooked = 1 $global:__zoxide_hooked = 1
$__zoxide_prompt_old = $function:prompt $global:__zoxide_prompt_old = $function:prompt
function prompt { function global:prompt {
if ($null -ne $__zoxide_prompt_old) { if ($null -ne $__zoxide_prompt_old) {
& $__zoxide_prompt_old & $__zoxide_prompt_old
} }
@ -99,7 +99,7 @@ if ($__zoxide_hooked -ne 1) {
# #
# Jump to a directory using only keywords. # Jump to a directory using only keywords.
function __zoxide_z { function global:__zoxide_z {
if ($args.Length -eq 0) { if ($args.Length -eq 0) {
__zoxide_cd ~ $true __zoxide_cd ~ $true
} }
@ -124,7 +124,7 @@ function __zoxide_z {
} }
# Jump to a directory using interactive search. # Jump to a directory using interactive search.
function __zoxide_zi { function global:__zoxide_zi {
$result = __zoxide_bin query -i -- @args $result = __zoxide_bin query -i -- @args
if ($LASTEXITCODE -eq 0) { if ($LASTEXITCODE -eq 0) {
__zoxide_cd $result $true __zoxide_cd $result $true