173 lines
4.8 KiB
Plaintext
173 lines
4.8 KiB
Plaintext
{%- let section = "# =============================================================================\n#" -%}
|
|
{%- let not_configured = "# -- not configured --" -%}
|
|
|
|
{{ section }}
|
|
# Utility functions for zoxide.
|
|
#
|
|
|
|
# pwd based on zoxide's format.
|
|
function __zoxide_pwd {
|
|
$cwd = Get-Location
|
|
if ($cwd.Provider.Name -eq "FileSystem") {
|
|
$cwd.ProviderPath
|
|
}
|
|
}
|
|
|
|
# cd + custom logic based on the value of _ZO_ECHO.
|
|
function __zoxide_cd($dir, $literal) {
|
|
$dir = if ($literal) {
|
|
Set-Location -LiteralPath $dir -Passthru -ErrorAction Stop
|
|
} else {
|
|
Set-Location -Path $dir -Passthru -ErrorAction Stop
|
|
}
|
|
{%- if echo %}
|
|
Write-Output $dir.Path
|
|
{%- endif %}
|
|
}
|
|
|
|
{{ section }}
|
|
# Hook configuration for zoxide.
|
|
#
|
|
|
|
# Hook to add new entries to the database.
|
|
function __zoxide_hook {
|
|
$result = __zoxide_pwd
|
|
if ($null -ne $result) {
|
|
zoxide add -- $result
|
|
}
|
|
}
|
|
|
|
# Initialize hook.
|
|
{# 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
|
|
{%- match hook %}
|
|
{%- when InitHook::None %}
|
|
{{ not_configured }}
|
|
{%- when InitHook::Prompt %}
|
|
$prompt_old = $function:prompt
|
|
function prompt {
|
|
$null = __zoxide_hook
|
|
& $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-cmd, alias these internal functions as desired.
|
|
#
|
|
|
|
# Jump to a directory using only keywords.
|
|
function __zoxide_z {
|
|
if ($args.Length -eq 0) {
|
|
__zoxide_cd ~ $true
|
|
}
|
|
elseif (
|
|
$args.Length -eq 1 -and
|
|
(($args[0] -eq '-' -or $args[0] -eq '+') -or (Test-Path $args[0] -PathType Container))
|
|
) {
|
|
__zoxide_cd $args[0] $false
|
|
}
|
|
else {
|
|
$result = __zoxide_pwd
|
|
if ($null -ne $result) {
|
|
$result = zoxide query --exclude $result -- @args
|
|
}
|
|
else {
|
|
$result = zoxide query -- @args
|
|
}
|
|
if ($LASTEXITCODE -eq 0) {
|
|
__zoxide_cd $result $true
|
|
}
|
|
}
|
|
}
|
|
|
|
# Jump to a directory using interactive search.
|
|
function __zoxide_zi {
|
|
$result = zoxide 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 (& { $hook = if ($PSVersionTable.PSVersion.Major -ge 6) { 'pwd' } else { 'prompt' } (zoxide init powershell --hook $hook | Out-String) })
|