From 4a8b4b707cc0b2cdb16ccfcfa6c85db23fdeb5e0 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 6 Dec 2024 17:46:13 +0100 Subject: [PATCH] Add initial powershell plugin by @lurebat Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 shell/navi.plugin.ps1 diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 new file mode 100644 index 0000000..3c16103 --- /dev/null +++ b/shell/navi.plugin.ps1 @@ -0,0 +1,26 @@ + +### Initial code from @lurebat (https://github.com/lurebat/) +function Invoke-NaviWidget { + $ast = $tokens = $errors = $cursor = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $ast, [ref] $tokens, [ref] $errors, [ref] $cursor) + + $line = $ast.ToString().Trim() + $output = $null + + if ([String]::IsNullOrEmpty($line)) { + $output = navi --print + } + else { + $best_match = (navi --print --best-match --query $line | Out-String).Trim() + if ([String]::IsNullOrEmpty($best_match)) { + $output = (navi --print --query "$line" | Out-String).Trim() + } else { + $output = $best_match + } + } + + [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() + [Microsoft.PowerShell.PSConsoleReadLine]::Insert($output) +} + +Set-PSReadlineKeyHandler -Key Ctrl+g -ScriptBlock { Invoke-NaviWidget }