zoxide/templates/powershell.txt

119 lines
2.9 KiB
Plaintext

{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}
{{ section }}
# Utility functions for zoxide.
#
# pwd based on zoxide's format.
function __zoxide_pwd {
$__zoxide_pwd = Get-Location
if ($__zoxide_pwd.Provider.Name -eq "FileSystem") {
$__zoxide_pwd.ProviderPath
}
}
# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd($dir) {
Set-Location $dir -ea Stop
{%- if echo %}
$(Get-Location).Path
{%- endif %}
}
{{ section }}
# Hook configuration for zoxide.
#
# Hook to add new entries to the database.
function __zoxide_hook {
$__zoxide_result = __zoxide_pwd
if ($__zoxide_result -ne $null) {
zoxide add -- $__zoxide_result
}
}
# Initialize hook.
if ($__zoxide_hooked -ne '1') {
$__zoxide_hooked = '1'
{%- match hook %}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
$__zoxide_prompt_old = $function:prompt
function prompt {
$null = __zoxide_hook
& $__zoxide_prompt_old
}
{%- when InitHook::Pwd %}
if ($PSVersionTable.PSVersion.Major -ge 6) {
$ExecutionContext.InvokeCommand.LocationChangedAction = {
$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 %}
}
{{ section }}
# 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 -
}
elseif ($args.Length -eq 1 -and (Test-Path $args[0] -PathType Container)) {
__zoxide_cd $args[0]
}
else {
$__zoxide_result = __zoxide_pwd
if ($__zoxide_result -ne $null) {
$__zoxide_result = zoxide query --exclude $__zoxide_result -- @args
} else {
$__zoxide_result = zoxide query -- @args
}
if ($LASTEXITCODE -eq 0) {
__zoxide_cd $__zoxide_result
}
}
}
# Jump to a directory using interactive search.
function __zoxide_zi {
$__zoxide_result = zoxide query -i -- @args
if ($LASTEXITCODE -eq 0) {
__zoxide_cd $__zoxide_result
}
}
{{ section }}
# 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 %}
{{ not_configured }}
{%- endmatch %}
{{ section }}
# To initialize zoxide, add this to your configuration (the location is stored
# in $profile):
#
# Invoke-Expression (& { $hook = if ($PSVersionTable.PSVersion.Major -ge 6) { 'pwd' } else { 'prompt' } (zoxide init powershell --hook $hook) -join "`n" })