Skip to content

Commit 3898d99

Browse files
matan-hajeetdsouza
andauthored
Use global scope for PowerShell variables / functions (#597)
Co-authored-by: Ajeet D'Souza <[email protected]>
1 parent 42e1d5d commit 3898d99

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
- Short option `-a` for `zoxide query --all`.
1515

16+
### Fixed
17+
18+
- PowerShell: use `global` scope for variables / functions.
19+
1620
## [0.9.1] - 2023-05-07
1721

1822
### Added

templates/powershell.txt

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77

88
# Call zoxide binary, returning the output as UTF-8.
9-
function __zoxide_bin {
9+
function global:__zoxide_bin {
1010
$encoding = [Console]::OutputEncoding
1111
try {
1212
[Console]::OutputEncoding = [System.Text.Utf8Encoding]::new()
@@ -18,15 +18,15 @@ function __zoxide_bin {
1818
}
1919

2020
# pwd based on zoxide's format.
21-
function __zoxide_pwd {
21+
function global:__zoxide_pwd {
2222
$cwd = Get-Location
2323
if ($cwd.Provider.Name -eq "FileSystem") {
2424
$cwd.ProviderPath
2525
}
2626
}
2727

2828
# cd + custom logic based on the value of _ZO_ECHO.
29-
function __zoxide_cd($dir, $literal) {
29+
function global:__zoxide_cd($dir, $literal) {
3030
$dir = if ($literal) {
3131
Set-Location -LiteralPath $dir -Passthru -ErrorAction Stop
3232
} else {
@@ -59,7 +59,7 @@ function __zoxide_cd($dir, $literal) {
5959
-#}
6060
{%- if hook == InitHook::Prompt -%}
6161
# Hook to add new entries to the database.
62-
function __zoxide_hook {
62+
function global:__zoxide_hook {
6363
$result = __zoxide_pwd
6464
if ($null -ne $result) {
6565
zoxide add -- $result
@@ -68,7 +68,7 @@ function __zoxide_hook {
6868
{%- else if hook == InitHook::Pwd -%}
6969
# Hook to add new entries to the database.
7070
$global:__zoxide_oldpwd = __zoxide_pwd
71-
function __zoxide_hook {
71+
function global:__zoxide_hook {
7272
$result = __zoxide_pwd
7373
if ($result -ne $global:__zoxide_oldpwd) {
7474
if ($null -ne $result) {
@@ -80,12 +80,12 @@ function __zoxide_hook {
8080
{%- endif %}
8181

8282
# Initialize hook.
83-
$__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction SilentlyContinue -ValueOnly)
84-
if ($__zoxide_hooked -ne 1) {
85-
$__zoxide_hooked = 1
86-
$__zoxide_prompt_old = $function:prompt
83+
$global:__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction SilentlyContinue -ValueOnly)
84+
if ($global:__zoxide_hooked -ne 1) {
85+
$global:__zoxide_hooked = 1
86+
$global:__zoxide_prompt_old = $function:prompt
8787

88-
function prompt {
88+
function global:prompt {
8989
if ($null -ne $__zoxide_prompt_old) {
9090
& $__zoxide_prompt_old
9191
}
@@ -99,7 +99,7 @@ if ($__zoxide_hooked -ne 1) {
9999
#
100100

101101
# Jump to a directory using only keywords.
102-
function __zoxide_z {
102+
function global:__zoxide_z {
103103
if ($args.Length -eq 0) {
104104
__zoxide_cd ~ $true
105105
}
@@ -124,7 +124,7 @@ function __zoxide_z {
124124
}
125125

126126
# Jump to a directory using interactive search.
127-
function __zoxide_zi {
127+
function global:__zoxide_zi {
128128
$result = __zoxide_bin query -i -- @args
129129
if ($LASTEXITCODE -eq 0) {
130130
__zoxide_cd $result $true

0 commit comments

Comments
 (0)