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.
|
|
|
|
#
|
|
|
|
|
2022-09-18 06:03:41 +00:00
|
|
|
# Call zoxide binary, returning the output as UTF-8.
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:__zoxide_bin {
|
2022-09-18 06:03:41 +00:00
|
|
|
$encoding = [Console]::OutputEncoding
|
|
|
|
try {
|
|
|
|
[Console]::OutputEncoding = [System.Text.Utf8Encoding]::new()
|
|
|
|
$result = zoxide @args
|
|
|
|
return $result
|
|
|
|
} finally {
|
|
|
|
[Console]::OutputEncoding = $encoding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 04:34:13 +00:00
|
|
|
# pwd based on zoxide's format.
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:__zoxide_pwd {
|
2021-12-13 06:46:12 +00:00
|
|
|
$cwd = Get-Location
|
|
|
|
if ($cwd.Provider.Name -eq "FileSystem") {
|
|
|
|
$cwd.ProviderPath
|
2021-05-14 04:34:13 +00:00
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# cd + custom logic based on the value of _ZO_ECHO.
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:__zoxide_cd($dir, $literal) {
|
2021-12-14 19:43:45 +00:00
|
|
|
$dir = if ($literal) {
|
|
|
|
Set-Location -LiteralPath $dir -Passthru -ErrorAction Stop
|
|
|
|
} else {
|
2023-05-07 13:49:53 +00:00
|
|
|
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
|
|
|
|
}
|
2021-12-14 19:43:45 +00:00
|
|
|
}
|
2021-04-08 18:35:42 +00:00
|
|
|
{%- if echo %}
|
2021-12-13 06:46:12 +00:00
|
|
|
Write-Output $dir.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.
|
|
|
|
#
|
|
|
|
|
2023-05-07 13:49:53 +00:00
|
|
|
{% 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 -%}
|
2020-10-18 09:22:13 +00:00
|
|
|
# Hook to add new entries to the database.
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:__zoxide_hook {
|
2021-09-04 10:31:50 +00:00
|
|
|
$result = __zoxide_pwd
|
2021-09-17 19:59:46 +00:00
|
|
|
if ($null -ne $result) {
|
2024-03-16 07:00:19 +00:00
|
|
|
zoxide add "--" $result
|
2021-05-14 04:34:13 +00:00
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
2023-05-07 13:49:53 +00:00
|
|
|
{%- else if hook == InitHook::Pwd -%}
|
|
|
|
# Hook to add new entries to the database.
|
|
|
|
$global:__zoxide_oldpwd = __zoxide_pwd
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:__zoxide_hook {
|
2023-05-07 13:49:53 +00:00
|
|
|
$result = __zoxide_pwd
|
|
|
|
if ($result -ne $global:__zoxide_oldpwd) {
|
|
|
|
if ($null -ne $result) {
|
2024-03-16 07:00:19 +00:00
|
|
|
zoxide add "--" $result
|
2023-05-07 13:49:53 +00:00
|
|
|
}
|
|
|
|
$global:__zoxide_oldpwd = $result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{%- endif %}
|
2020-10-18 09:22:13 +00:00
|
|
|
|
|
|
|
# Initialize hook.
|
2023-08-04 01:55:48 +00:00
|
|
|
$global:__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction SilentlyContinue -ValueOnly)
|
|
|
|
if ($global:__zoxide_hooked -ne 1) {
|
|
|
|
$global:__zoxide_hooked = 1
|
|
|
|
$global:__zoxide_prompt_old = $function:prompt
|
2023-05-07 13:49:53 +00:00
|
|
|
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:prompt {
|
2023-05-07 13:49:53 +00:00
|
|
|
if ($null -ne $__zoxide_prompt_old) {
|
|
|
|
& $__zoxide_prompt_old
|
2021-03-06 12:55:13 +00:00
|
|
|
}
|
2023-05-07 13:49:53 +00:00
|
|
|
$null = __zoxide_hook
|
2021-09-17 19:59:46 +00:00
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
2023-05-07 13:49:53 +00:00
|
|
|
{%- endif %}
|
2020-10-18 09:22:13 +00:00
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2022-04-22 07:41:11 +00:00
|
|
|
# When using zoxide with --no-cmd, alias these internal functions as desired.
|
2020-10-18 09:22:13 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
# Jump to a directory using only keywords.
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:__zoxide_z {
|
2020-10-18 09:22:13 +00:00
|
|
|
if ($args.Length -eq 0) {
|
2021-12-14 19:43:45 +00:00
|
|
|
__zoxide_cd ~ $true
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
2023-05-07 13:49:53 +00:00
|
|
|
elseif ($args.Length -eq 1 -and ($args[0] -eq '-' -or $args[0] -eq '+')) {
|
2021-12-14 19:43:45 +00:00
|
|
|
__zoxide_cd $args[0] $false
|
2020-10-26 17:55:04 +00:00
|
|
|
}
|
2023-05-07 13:49:53 +00:00
|
|
|
elseif ($args.Length -eq 1 -and (Test-Path $args[0] -PathType Container)) {
|
|
|
|
__zoxide_cd $args[0] $true
|
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
else {
|
2021-09-04 10:31:50 +00:00
|
|
|
$result = __zoxide_pwd
|
2021-09-17 19:59:46 +00:00
|
|
|
if ($null -ne $result) {
|
2024-03-16 07:00:19 +00:00
|
|
|
$result = __zoxide_bin query --exclude $result "--" @args
|
2021-09-17 19:59:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-03-16 07:00:19 +00:00
|
|
|
$result = __zoxide_bin query "--" @args
|
2021-05-14 04:34:13 +00:00
|
|
|
}
|
2020-10-18 09:22:13 +00:00
|
|
|
if ($LASTEXITCODE -eq 0) {
|
2021-12-14 19:43:45 +00:00
|
|
|
__zoxide_cd $result $true
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Jump to a directory using interactive search.
|
2023-08-04 01:55:48 +00:00
|
|
|
function global:__zoxide_zi {
|
2024-03-16 07:00:19 +00:00
|
|
|
$result = __zoxide_bin query -i "--" @args
|
2020-10-18 09:22:13 +00:00
|
|
|
if ($LASTEXITCODE -eq 0) {
|
2021-12-14 19:43:45 +00:00
|
|
|
__zoxide_cd $result $true
|
2020-10-18 09:22:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2022-04-10 22:11:51 +00:00
|
|
|
# Commands for zoxide. Disable these using --no-cmd.
|
2020-10-18 09:22:13 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
{%- match cmd %}
|
|
|
|
{%- when Some with (cmd) %}
|
|
|
|
|
2021-11-10 20:52:23 +00:00
|
|
|
Set-Alias -Name {{cmd}} -Value __zoxide_z -Option AllScope -Scope Global -Force
|
|
|
|
Set-Alias -Name {{cmd}}i -Value __zoxide_zi -Option AllScope -Scope Global -Force
|
2020-10-18 09:22:13 +00:00
|
|
|
|
|
|
|
{%- 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-08-05 08:52:12 +00:00
|
|
|
# To initialize zoxide, add this to your configuration (find it by running
|
|
|
|
# `echo $profile` in PowerShell):
|
2020-10-18 09:22:13 +00:00
|
|
|
#
|
2023-05-07 13:49:53 +00:00
|
|
|
# Invoke-Expression (& { (zoxide init powershell | Out-String) })
|