2021-04-08 18:35:42 +00:00
|
|
|
{%- let section = "# =============================================================================\n#" -%}
|
|
|
|
{%- let not_configured = "# -- not configured --" -%}
|
2020-10-18 09:22:13 +00:00
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2020-10-18 09:22:13 +00:00
|
|
|
# Utility functions for zoxide.
|
|
|
|
#
|
|
|
|
|
2021-05-14 04:34:13 +00:00
|
|
|
# pwd based on zoxide's format.
|
2020-10-18 09:22:13 +00:00
|
|
|
function __zoxide_pwd {
|
2021-05-14 04:34:13 +00:00
|
|
|
$__zoxide_pwd = Get-Location
|
|
|
|
if ($__zoxide_pwd.Provider.Name -eq "FileSystem") {
|
|
|
|
$__zoxide_pwd.ProviderPath
|
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# cd + custom logic based on the value of _ZO_ECHO.
|
|
|
|
function __zoxide_cd($dir) {
|
|
|
|
Set-Location $dir -ea Stop
|
2021-04-08 18:35:42 +00:00
|
|
|
{%- if echo %}
|
2021-05-14 04:34:13 +00:00
|
|
|
$(Get-Location).Path
|
2021-04-08 18:35:42 +00:00
|
|
|
{%- endif %}
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2020-10-18 09:22:13 +00:00
|
|
|
# Hook configuration for zoxide.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Hook to add new entries to the database.
|
|
|
|
function __zoxide_hook {
|
2021-05-14 04:34:13 +00:00
|
|
|
$__zoxide_result = __zoxide_pwd
|
|
|
|
if ($__zoxide_result -ne $null) {
|
|
|
|
zoxide add -- $__zoxide_result
|
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Initialize hook.
|
2021-07-17 12:27:04 +00:00
|
|
|
{# 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) {
|
|
|
|
$__zoxide_hooked = 1
|
2021-04-08 18:35:42 +00:00
|
|
|
{%- match hook %}
|
2021-05-03 21:12:43 +00:00
|
|
|
{%- when InitHook::None %}
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ not_configured }}
|
2021-05-03 21:12:43 +00:00
|
|
|
{%- when InitHook::Prompt %}
|
2021-03-06 12:55:13 +00:00
|
|
|
$__zoxide_prompt_old = $function:prompt
|
|
|
|
function prompt {
|
2020-10-18 09:22:13 +00:00
|
|
|
$null = __zoxide_hook
|
2021-03-06 12:55:13 +00:00
|
|
|
& $__zoxide_prompt_old
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
2021-05-03 21:12:43 +00:00
|
|
|
{%- when InitHook::Pwd %}
|
2021-03-06 12:55:13 +00:00
|
|
|
if ($PSVersionTable.PSVersion.Major -ge 6) {
|
|
|
|
$ExecutionContext.InvokeCommand.LocationChangedAction = {
|
|
|
|
$null = __zoxide_hook
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Write-Error ("`n" +
|
2021-03-31 16:45:43 +00:00
|
|
|
"zoxide: PWD hooks are not supported below powershell 6.`n" +
|
2021-03-13 21:34:30 +00:00
|
|
|
" Use 'zoxide init powershell --hook prompt' instead.")
|
2021-03-06 12:55:13 +00:00
|
|
|
}
|
2021-04-08 18:35:42 +00:00
|
|
|
{%- endmatch %}
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2020-10-18 09:22:13 +00:00
|
|
|
# When using zoxide with --no-aliases, alias these internal functions as
|
|
|
|
# desired.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Jump to a directory using only keywords.
|
|
|
|
function __zoxide_z {
|
|
|
|
if ($args.Length -eq 0) {
|
|
|
|
__zoxide_cd ~
|
|
|
|
}
|
|
|
|
elseif ($args.Length -eq 1 -and $args[0] -eq '-') {
|
|
|
|
__zoxide_cd -
|
|
|
|
}
|
2021-04-04 14:36:44 +00:00
|
|
|
elseif ($args.Length -eq 1 -and (Test-Path $args[0] -PathType Container)) {
|
2020-10-26 17:55:04 +00:00
|
|
|
__zoxide_cd $args[0]
|
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
else {
|
2021-05-14 04:34:13 +00:00
|
|
|
$__zoxide_result = __zoxide_pwd
|
|
|
|
if ($__zoxide_result -ne $null) {
|
|
|
|
$__zoxide_result = zoxide query --exclude $__zoxide_result -- @args
|
|
|
|
} else {
|
|
|
|
$__zoxide_result = zoxide query -- @args
|
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
|
|
__zoxide_cd $__zoxide_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Jump to a directory using interactive search.
|
2020-10-26 17:55:04 +00:00
|
|
|
function __zoxide_zi {
|
2020-10-18 09:22:13 +00:00
|
|
|
$__zoxide_result = zoxide query -i -- @args
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
|
|
__zoxide_cd $__zoxide_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2020-10-18 09:22:13 +00:00
|
|
|
# Convenient aliases for zoxide. Disable these using --no-aliases.
|
|
|
|
#
|
|
|
|
|
|
|
|
{%- match cmd %}
|
|
|
|
{%- when Some with (cmd) %}
|
|
|
|
|
|
|
|
Set-Alias {{cmd}} __zoxide_z
|
|
|
|
Set-Alias {{cmd}}i __zoxide_zi
|
|
|
|
|
|
|
|
{%- when None %}
|
2021-04-08 18:35:42 +00:00
|
|
|
|
|
|
|
{{ not_configured }}
|
2020-10-18 09:22:13 +00:00
|
|
|
|
|
|
|
{%- endmatch %}
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2021-04-27 12:07:38 +00:00
|
|
|
# To initialize zoxide, add this to your configuration (the location is stored
|
|
|
|
# in $profile):
|
2020-10-18 09:22:13 +00:00
|
|
|
#
|
2020-10-26 17:55:04 +00:00
|
|
|
# Invoke-Expression (& { $hook = if ($PSVersionTable.PSVersion.Major -ge 6) { 'pwd' } else { 'prompt' } (zoxide init powershell --hook $hook) -join "`n" })
|