204 lines
5.8 KiB
Plaintext
204 lines
5.8 KiB
Plaintext
{%- let section = "# =============================================================================\n#" -%}
|
|
{%- let not_configured = "# -- not configured --" -%}
|
|
|
|
{{ section }}
|
|
# Utility functions for zoxide.
|
|
#
|
|
|
|
# Call zoxide binary, returning the output as UTF-8.
|
|
function global:__zoxide_bin {
|
|
$encoding = [Console]::OutputEncoding
|
|
try {
|
|
[Console]::OutputEncoding = [System.Text.Utf8Encoding]::new()
|
|
$result = zoxide @args
|
|
return $result
|
|
} finally {
|
|
[Console]::OutputEncoding = $encoding
|
|
}
|
|
}
|
|
|
|
# pwd based on zoxide's format.
|
|
function global:__zoxide_pwd {
|
|
$cwd = Get-Location
|
|
if ($cwd.Provider.Name -eq "FileSystem") {
|
|
$cwd.ProviderPath
|
|
}
|
|
}
|
|
|
|
# cd + custom logic based on the value of _ZO_ECHO.
|
|
function global:__zoxide_cd($dir, $literal) {
|
|
$dir = if ($literal) {
|
|
Set-Location -LiteralPath $dir -Passthru -ErrorAction Stop
|
|
} else {
|
|
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
|
|
}
|
|
}
|
|
{%- if echo %}
|
|
Write-Output $dir.Path
|
|
{%- endif %}
|
|
}
|
|
|
|
{{ section }}
|
|
# Hook configuration for zoxide.
|
|
#
|
|
|
|
{% 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 -%}
|
|
# Hook to add new entries to the database.
|
|
function global:__zoxide_hook {
|
|
$result = __zoxide_pwd
|
|
if ($null -ne $result) {
|
|
zoxide add "--" $result
|
|
}
|
|
}
|
|
{%- else if hook == InitHook::Pwd -%}
|
|
# Hook to add new entries to the database.
|
|
$global:__zoxide_oldpwd = __zoxide_pwd
|
|
function global:__zoxide_hook {
|
|
$result = __zoxide_pwd
|
|
if ($result -ne $global:__zoxide_oldpwd) {
|
|
if ($null -ne $result) {
|
|
zoxide add "--" $result
|
|
}
|
|
$global:__zoxide_oldpwd = $result
|
|
}
|
|
}
|
|
{%- endif %}
|
|
|
|
# Initialize hook.
|
|
$global:__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction Ignore -ValueOnly)
|
|
if ($global:__zoxide_hooked -ne 1) {
|
|
$global:__zoxide_hooked = 1
|
|
$global:__zoxide_prompt_old = $function:prompt
|
|
|
|
function global:prompt {
|
|
if ($null -ne $__zoxide_prompt_old) {
|
|
& $__zoxide_prompt_old
|
|
}
|
|
$null = __zoxide_hook
|
|
}
|
|
}
|
|
{%- endif %}
|
|
|
|
{{ section }}
|
|
# When using zoxide with --no-cmd, alias these internal functions as desired.
|
|
#
|
|
|
|
# Jump to a directory using only keywords.
|
|
function global:__zoxide_z {
|
|
if ($args.Length -eq 0) {
|
|
__zoxide_cd ~ $true
|
|
}
|
|
elseif ($args.Length -eq 1 -and ($args[0] -eq '-' -or $args[0] -eq '+')) {
|
|
__zoxide_cd $args[0] $false
|
|
}
|
|
elseif ($args.Length -eq 1 -and (Test-Path -PathType Container -LiteralPath $args[0])) {
|
|
__zoxide_cd $args[0] $true
|
|
}
|
|
elseif ($args.Length -eq 1 -and (Test-Path -PathType Container -Path $args[0] )) {
|
|
__zoxide_cd $args[0] $false
|
|
}
|
|
else {
|
|
$result = __zoxide_pwd
|
|
if ($null -ne $result) {
|
|
$result = __zoxide_bin query --exclude $result "--" @args
|
|
}
|
|
else {
|
|
$result = __zoxide_bin query "--" @args
|
|
}
|
|
if ($LASTEXITCODE -eq 0) {
|
|
__zoxide_cd $result $true
|
|
}
|
|
}
|
|
}
|
|
|
|
# Jump to a directory using interactive search.
|
|
function global:__zoxide_zi {
|
|
$result = __zoxide_bin query -i "--" @args
|
|
if ($LASTEXITCODE -eq 0) {
|
|
__zoxide_cd $result $true
|
|
}
|
|
}
|
|
|
|
{{ section }}
|
|
# Commands for zoxide. Disable these using --no-cmd.
|
|
#
|
|
|
|
{%- match cmd %}
|
|
{%- when Some with (cmd) %}
|
|
|
|
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
|
|
|
|
filter __zoxide_escapeStringWithSpecialChars {
|
|
$_ -replace '\s|#|@|\$|;|,|''|\{|\}|\(|\)|"|`|\||<|>|&','`$&'
|
|
}
|
|
|
|
Register-ArgumentCompleter -Native -CommandName "{{cmd}}" -ScriptBlock {
|
|
param(
|
|
$WordToComplete,
|
|
$CommandAst,
|
|
$CursorPosition
|
|
)
|
|
|
|
# Get the current command line and convert into a string
|
|
$Command = $CommandAst.CommandElements
|
|
$Command = "$Command"
|
|
|
|
# The user could have moved the cursor backwards on the command-line.
|
|
# We only show completions when the cursor is at the end of the line
|
|
if ($Command.Length -gt $CursorPosition) {
|
|
return
|
|
}
|
|
|
|
$Program,$Arguments = $Command.Split(" ",2)
|
|
|
|
# If we don't have any parameter, just use the default completion (Which is Set-Location)
|
|
if([string]::IsNullOrEmpty($Arguments)) {
|
|
return
|
|
}
|
|
|
|
$QueryArgs = $Arguments.Split(" ")
|
|
|
|
# If the last parameter is complete (there is a space following it)
|
|
if ($WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
|
|
# Normally, we would invoke the interactive query. Unfortunally it is not possible to invoke an
|
|
# interactive command in a powershell argument completer. Therefore, we just return in that case to the
|
|
# default completion strategy.
|
|
# zoxide query -i -- @QueryArgs
|
|
return
|
|
} else {
|
|
$result = zoxide query --exclude "$(__zoxide_pwd)" -l -- @QueryArgs
|
|
}
|
|
|
|
$result | ForEach-Object {
|
|
[System.Management.Automation.CompletionResult]::new($($_ | __zoxide_escapeStringWithSpecialChars), "$($_)", 'ParameterValue', "$($_)")
|
|
}
|
|
}
|
|
|
|
{%- when None %}
|
|
|
|
{{ not_configured }}
|
|
|
|
{%- endmatch %}
|
|
|
|
{{ section }}
|
|
# To initialize zoxide, add this to your configuration (find it by running
|
|
# `echo $profile` in PowerShell):
|
|
#
|
|
# Invoke-Expression (& { (zoxide init powershell | Out-String) })
|