From 7d19bbeb858d3939d4dd05aa7fb17b842ea7f8c5 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:48:56 +0100 Subject: [PATCH 01/14] Update powershell plugins metadata Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin/navi.plugin.psd1 | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/navi.plugin/navi.plugin.psd1 b/shell/navi.plugin/navi.plugin.psd1 index 86e8da3..2e1f49f 100644 --- a/shell/navi.plugin/navi.plugin.psd1 +++ b/shell/navi.plugin/navi.plugin.psd1 @@ -9,6 +9,7 @@ RootModule = './navi.plugin.psm1' ModuleVersion = '1.0' GUID = '59287935-a9b6-4a7f-a1f5-bd6180d9056f' Author = 'Alexis-Opolka' +Description = 'A powershell module that integrates with navi. It makes you able to access navi with the Ctrl+G keybinding.' CompanyName = 'None' Copyright = '(c) Alexis Opolka & Navi contributors. All rights reserved.' FunctionsToExport = @('Invoke-NaviWidget') From 4300b7a6d657b11c940f9941aaa9add0dbcf39c3 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:53:25 +0100 Subject: [PATCH 02/14] Tries to implement the call to Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index 0cf73da..5be331d 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -10,25 +10,23 @@ param ( function InstallNaviWidgetModule(){ - [String]$PwshModulePath = $env:PSModulePath.Split(";")[0]; [String]$NAVI_PLUGIN = "navi.plugin"; Write-Debug "Are we updating the PowerShell module: $Update" - Write-Debug "Current Root Powershell Module path is: $PwshModulePath" ### If we're updating the module, Copy the newly updated contents to the currently installed module ### then quit with a successful exit code. ### We're not supposed to update the shortcut binding. if ($Update){ Write-Debug "Updating Navi-Widget PowerShell module" - Copy-Item -Path .\navi.plugin\ -Destination $PwshModulePath -Force -Recurse + Install-Module -Name navi.plugin exit 0 } ### If we're not updating, check if we don't have the module already installed if (-Not (Test-Path -Path $PwshModulePath\$NAVI_PLUGIN)) { Write-Debug "Copying Item to the path" - Copy-Item -Path .\navi.plugin\ -Destination $PwshModulePath -Recurse + Install-Module -Name navi.plugin } else { Write-Error "Navi-Widget is already installed for PowerShell!" exit 1 From 2bb57a23b89e1a08e0fc14a183177c2a7de9fb69 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:56:05 +0100 Subject: [PATCH 03/14] Moves Navi plugin to its own repository https://github.com/alexis-opolka/navi.plugin.pwsh Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin/navi.plugin.psd1 | 26 -------------- shell/navi.plugin/navi.plugin.psm1 | 56 ------------------------------ 2 files changed, 82 deletions(-) delete mode 100644 shell/navi.plugin/navi.plugin.psd1 delete mode 100644 shell/navi.plugin/navi.plugin.psm1 diff --git a/shell/navi.plugin/navi.plugin.psd1 b/shell/navi.plugin/navi.plugin.psd1 deleted file mode 100644 index 2e1f49f..0000000 --- a/shell/navi.plugin/navi.plugin.psd1 +++ /dev/null @@ -1,26 +0,0 @@ -# -# Module manifest for module 'navi.plugin' -# -# Generated by: Alexis Opolka (@alexis-opolka) - - -@{ -RootModule = './navi.plugin.psm1' -ModuleVersion = '1.0' -GUID = '59287935-a9b6-4a7f-a1f5-bd6180d9056f' -Author = 'Alexis-Opolka' -Description = 'A powershell module that integrates with navi. It makes you able to access navi with the Ctrl+G keybinding.' -CompanyName = 'None' -Copyright = '(c) Alexis Opolka & Navi contributors. All rights reserved.' -FunctionsToExport = @('Invoke-NaviWidget') -CmdletsToExport = @() -VariablesToExport = '*' -AliasesToExport = @() -PrivateData = @{ - PSData = @{ - } -} -### Here should go the HelpInfo XML file if it's supported in -### a later version. -HelpInfoURI = '' -} diff --git a/shell/navi.plugin/navi.plugin.psm1 b/shell/navi.plugin/navi.plugin.psm1 deleted file mode 100644 index 7088afe..0000000 --- a/shell/navi.plugin/navi.plugin.psm1 +++ /dev/null @@ -1,56 +0,0 @@ - -[CmdletBinding()] -param ( - ### Should we debug this PS Script ? - ### Caution: Set-PSDebug is not limited to this script but enables session-wide calls - ### Be sure to disable Verbose output before calling any other shell modules or scripts. - [Parameter()] - [bool] - $VerboseOutput -) - -if ($VerboseOutput) { - ### Outputs the running code if required - ### - ### For more details on how it works, please see: - ### - https://stackoverflow.com/a/41334715/13025136 - ### An answer and explaination from @michael-sorens (https://stackoverflow.com/users/115690/michael-sorens) - ### on how Set-PSDebug relates to set-x in LINUX/UNIX environments. - ### - ### - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-7.4 - ### Microsoft's Reference and documentation for the `Set-PSDebug` function. - Set-PSDebug -Trace 1 -} else { - Set-PSDebug -Trace 0 -} - - -### Initial code from @lurebat (https://github.com/lurebat/) -### See #570 (https://github.com/denisidoro/navi/issues/570) for its original contribution -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() - - ### Handling the case when the user escapes without selecting any entry - if (-Not([String]::IsNullOrEmpty($output))) { - [Microsoft.PowerShell.PSConsoleReadLine]::Insert([String]$output) - } -} \ No newline at end of file From 6f82c186a7a5cd61daf50602b57c897049095900 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 28 Feb 2025 01:02:32 +0100 Subject: [PATCH 04/14] Adds a check to create the profile file if it doesn't exist Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index 5be331d..4c99f98 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -32,6 +32,12 @@ function InstallNaviWidgetModule(){ exit 1 } + ### We first need to be sure the profile has been created + if (-Not(Test-Path -Path $PROFILE)) { + ### The Profile file hasn't been created yet + New-Item -ItemType "file" -Path $PROFILE -Force + } + Write-Debug "Registering the navi shortcut inside the current shell session" Set-PSReadlineKeyHandler -BriefDescription "A keybinding to open Navi Widget" -Chord Ctrl+g -ScriptBlock { Invoke-NaviWidget } From 54b707c201b33108b5c79b489e3aeba00ef96b65 Mon Sep 17 00:00:00 2001 From: Alexis Opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 7 Mar 2025 18:37:15 +0000 Subject: [PATCH 05/14] Revert "Moves Navi plugin to its own repository" This reverts commit 2bb57a23b89e1a08e0fc14a183177c2a7de9fb69. --- shell/navi.plugin/navi.plugin.psd1 | 26 ++++++++++++++ shell/navi.plugin/navi.plugin.psm1 | 56 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 shell/navi.plugin/navi.plugin.psd1 create mode 100644 shell/navi.plugin/navi.plugin.psm1 diff --git a/shell/navi.plugin/navi.plugin.psd1 b/shell/navi.plugin/navi.plugin.psd1 new file mode 100644 index 0000000..2e1f49f --- /dev/null +++ b/shell/navi.plugin/navi.plugin.psd1 @@ -0,0 +1,26 @@ +# +# Module manifest for module 'navi.plugin' +# +# Generated by: Alexis Opolka (@alexis-opolka) + + +@{ +RootModule = './navi.plugin.psm1' +ModuleVersion = '1.0' +GUID = '59287935-a9b6-4a7f-a1f5-bd6180d9056f' +Author = 'Alexis-Opolka' +Description = 'A powershell module that integrates with navi. It makes you able to access navi with the Ctrl+G keybinding.' +CompanyName = 'None' +Copyright = '(c) Alexis Opolka & Navi contributors. All rights reserved.' +FunctionsToExport = @('Invoke-NaviWidget') +CmdletsToExport = @() +VariablesToExport = '*' +AliasesToExport = @() +PrivateData = @{ + PSData = @{ + } +} +### Here should go the HelpInfo XML file if it's supported in +### a later version. +HelpInfoURI = '' +} diff --git a/shell/navi.plugin/navi.plugin.psm1 b/shell/navi.plugin/navi.plugin.psm1 new file mode 100644 index 0000000..7088afe --- /dev/null +++ b/shell/navi.plugin/navi.plugin.psm1 @@ -0,0 +1,56 @@ + +[CmdletBinding()] +param ( + ### Should we debug this PS Script ? + ### Caution: Set-PSDebug is not limited to this script but enables session-wide calls + ### Be sure to disable Verbose output before calling any other shell modules or scripts. + [Parameter()] + [bool] + $VerboseOutput +) + +if ($VerboseOutput) { + ### Outputs the running code if required + ### + ### For more details on how it works, please see: + ### - https://stackoverflow.com/a/41334715/13025136 + ### An answer and explaination from @michael-sorens (https://stackoverflow.com/users/115690/michael-sorens) + ### on how Set-PSDebug relates to set-x in LINUX/UNIX environments. + ### + ### - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-7.4 + ### Microsoft's Reference and documentation for the `Set-PSDebug` function. + Set-PSDebug -Trace 1 +} else { + Set-PSDebug -Trace 0 +} + + +### Initial code from @lurebat (https://github.com/lurebat/) +### See #570 (https://github.com/denisidoro/navi/issues/570) for its original contribution +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() + + ### Handling the case when the user escapes without selecting any entry + if (-Not([String]::IsNullOrEmpty($output))) { + [Microsoft.PowerShell.PSConsoleReadLine]::Insert([String]$output) + } +} \ No newline at end of file From 684e450ef5ca258618fc9bec47e1a0aba971cadd Mon Sep 17 00:00:00 2001 From: Alexis Opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 7 Mar 2025 19:40:11 +0100 Subject: [PATCH 06/14] Update shell.rs It should remove the need to put a "-" in the middle of the name. It's a bit impractical. --- src/common/shell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/shell.rs b/src/common/shell.rs index 13f5bbf..c9d86a6 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -12,7 +12,7 @@ pub enum Shell { Fish, Elvish, Nushell, - PowerShell, + Powershell, } #[derive(Error, Debug)] From cc1187e25949f1cb50d79aac33d6a8e2885b281a Mon Sep 17 00:00:00 2001 From: Alexis Opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 7 Mar 2025 19:41:21 +0100 Subject: [PATCH 07/14] Update shell.rs Updated the link to the module. --- src/commands/shell.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/shell.rs b/src/commands/shell.rs index 992b92b..a9641b0 100644 --- a/src/commands/shell.rs +++ b/src/commands/shell.rs @@ -14,7 +14,7 @@ impl Display for Shell { Self::Fish => "fish", Self::Elvish => "elvish", Self::Nushell => "nushell", - Self::PowerShell => "powershell", + Self::Powershell => "powershell", }; write!(f, "{s}") @@ -37,7 +37,7 @@ impl Runnable for Input { Shell::Fish => include_str!("../../shell/navi.plugin.fish"), Shell::Elvish => include_str!("../../shell/navi.plugin.elv"), Shell::Nushell => include_str!("../../shell/navi.plugin.nu"), - Shell::PowerShell => include_str!("../../shell/navi.plugin.ps1"), + Shell::PowerShell => include_str!("../../shell/navi.plugin.psm1"), }; println!("{content}"); From 7a2108cb2029da97c4841e285020a364976fd682 Mon Sep 17 00:00:00 2001 From: Alexis Opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 7 Mar 2025 19:44:12 +0100 Subject: [PATCH 08/14] Create navi.plugin.psm1 Moves the file to the base folder, removed cmdlets handlers and related options. Still needs modifications. --- shell/navi.plugin.psm1 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 shell/navi.plugin.psm1 diff --git a/shell/navi.plugin.psm1 b/shell/navi.plugin.psm1 new file mode 100644 index 0000000..dbe619a --- /dev/null +++ b/shell/navi.plugin.psm1 @@ -0,0 +1,30 @@ + +### Initial code from @lurebat (https://github.com/lurebat/) +### See #570 (https://github.com/denisidoro/navi/issues/570) for its original contribution +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() + + ### Handling the case when the user escapes without selecting any entry + if (-Not([String]::IsNullOrEmpty($output))) { + [Microsoft.PowerShell.PSConsoleReadLine]::Insert([String]$output) + } +} \ No newline at end of file From 89e71794c608b34c2fec4adfcbb24e6a54de8875 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 14 Mar 2025 23:41:47 +0100 Subject: [PATCH 09/14] Update navi pwsh plugin Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 85 ++++++++++-------------------- shell/navi.plugin.psm1 | 30 ----------- shell/navi.plugin/navi.plugin.psd1 | 26 --------- shell/navi.plugin/navi.plugin.psm1 | 56 -------------------- src/commands/shell.rs | 2 +- 5 files changed, 28 insertions(+), 171 deletions(-) delete mode 100644 shell/navi.plugin.psm1 delete mode 100644 shell/navi.plugin/navi.plugin.psd1 delete mode 100644 shell/navi.plugin/navi.plugin.psm1 diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index 4c99f98..ffc4c93 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -1,64 +1,33 @@ -### This script installs the Powershell module under the user's Powershell modules path -### For modifications of the Powershell Navi plugin, see /shell/navi.plugin/navi.plugin.psm1. - -param ( - ### Controls if we update the current module (otherwise we don't rewrite the already existing module, if present) - [Switch]$Update, - ### Should the verbosity be enabled? - [Switch]$Verbose -) -function InstallNaviWidgetModule(){ - [String]$NAVI_PLUGIN = "navi.plugin"; +$null = New-Module { + ### Initial code from @lurebat (https://github.com/lurebat/) + ### See #570 (https://github.com/denisidoro/navi/issues/570) for its original contribution + function Invoke-NaviWidget { + $ast = $tokens = $errors = $cursor = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $ast, [ref] $tokens, [ref] $errors, [ref] $cursor) - Write-Debug "Are we updating the PowerShell module: $Update" + $line = $ast.ToString().Trim() + $output = $null - ### If we're updating the module, Copy the newly updated contents to the currently installed module - ### then quit with a successful exit code. - ### We're not supposed to update the shortcut binding. - if ($Update){ - Write-Debug "Updating Navi-Widget PowerShell module" - Install-Module -Name navi.plugin - exit 0 + 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() + + ### Handling the case when the user escapes without selecting any entry + if (-Not([String]::IsNullOrEmpty($output))) { + [Microsoft.PowerShell.PSConsoleReadLine]::Insert([String]$output) + } } - - ### If we're not updating, check if we don't have the module already installed - if (-Not (Test-Path -Path $PwshModulePath\$NAVI_PLUGIN)) { - Write-Debug "Copying Item to the path" - Install-Module -Name navi.plugin - } else { - Write-Error "Navi-Widget is already installed for PowerShell!" - exit 1 - } - - ### We first need to be sure the profile has been created - if (-Not(Test-Path -Path $PROFILE)) { - ### The Profile file hasn't been created yet - New-Item -ItemType "file" -Path $PROFILE -Force - } - - Write-Debug "Registering the navi shortcut inside the current shell session" - Set-PSReadlineKeyHandler -BriefDescription "A keybinding to open Navi Widget" -Chord Ctrl+g -ScriptBlock { Invoke-NaviWidget } - - Write-Debug "Appending the navi shortcut inside the current user's profile" - ### Adding a new line - Write-Output "Import-Module navi.plugin" >> $PROFILE - Write-Output "" >> $PROFILE - Write-Output 'Set-PSReadlineKeyHandler -BriefDescription "A keybinding to open Navi Widget" -Chord Ctrl+g -ScriptBlock { Invoke-NaviWidget }' >> $PROFILE - - - Write-Output "Navi plugin has been installed!" - exit 0 ### Succesful installation -} - -if ($Verbose) { - ### Enabling verbose/debug output at the start of the script - $DebugPreference = 'Continue' - InstallNaviWidgetModule -Update $Update - ### Disabling verbose/debug output at the end of the script - ### in order to not modify the current user's shell environment - $DebugPreference = 'SilentlyContinue' -} else { - InstallNaviWidgetModule -Update $Update } diff --git a/shell/navi.plugin.psm1 b/shell/navi.plugin.psm1 deleted file mode 100644 index dbe619a..0000000 --- a/shell/navi.plugin.psm1 +++ /dev/null @@ -1,30 +0,0 @@ - -### Initial code from @lurebat (https://github.com/lurebat/) -### See #570 (https://github.com/denisidoro/navi/issues/570) for its original contribution -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() - - ### Handling the case when the user escapes without selecting any entry - if (-Not([String]::IsNullOrEmpty($output))) { - [Microsoft.PowerShell.PSConsoleReadLine]::Insert([String]$output) - } -} \ No newline at end of file diff --git a/shell/navi.plugin/navi.plugin.psd1 b/shell/navi.plugin/navi.plugin.psd1 deleted file mode 100644 index 2e1f49f..0000000 --- a/shell/navi.plugin/navi.plugin.psd1 +++ /dev/null @@ -1,26 +0,0 @@ -# -# Module manifest for module 'navi.plugin' -# -# Generated by: Alexis Opolka (@alexis-opolka) - - -@{ -RootModule = './navi.plugin.psm1' -ModuleVersion = '1.0' -GUID = '59287935-a9b6-4a7f-a1f5-bd6180d9056f' -Author = 'Alexis-Opolka' -Description = 'A powershell module that integrates with navi. It makes you able to access navi with the Ctrl+G keybinding.' -CompanyName = 'None' -Copyright = '(c) Alexis Opolka & Navi contributors. All rights reserved.' -FunctionsToExport = @('Invoke-NaviWidget') -CmdletsToExport = @() -VariablesToExport = '*' -AliasesToExport = @() -PrivateData = @{ - PSData = @{ - } -} -### Here should go the HelpInfo XML file if it's supported in -### a later version. -HelpInfoURI = '' -} diff --git a/shell/navi.plugin/navi.plugin.psm1 b/shell/navi.plugin/navi.plugin.psm1 deleted file mode 100644 index 7088afe..0000000 --- a/shell/navi.plugin/navi.plugin.psm1 +++ /dev/null @@ -1,56 +0,0 @@ - -[CmdletBinding()] -param ( - ### Should we debug this PS Script ? - ### Caution: Set-PSDebug is not limited to this script but enables session-wide calls - ### Be sure to disable Verbose output before calling any other shell modules or scripts. - [Parameter()] - [bool] - $VerboseOutput -) - -if ($VerboseOutput) { - ### Outputs the running code if required - ### - ### For more details on how it works, please see: - ### - https://stackoverflow.com/a/41334715/13025136 - ### An answer and explaination from @michael-sorens (https://stackoverflow.com/users/115690/michael-sorens) - ### on how Set-PSDebug relates to set-x in LINUX/UNIX environments. - ### - ### - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-7.4 - ### Microsoft's Reference and documentation for the `Set-PSDebug` function. - Set-PSDebug -Trace 1 -} else { - Set-PSDebug -Trace 0 -} - - -### Initial code from @lurebat (https://github.com/lurebat/) -### See #570 (https://github.com/denisidoro/navi/issues/570) for its original contribution -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() - - ### Handling the case when the user escapes without selecting any entry - if (-Not([String]::IsNullOrEmpty($output))) { - [Microsoft.PowerShell.PSConsoleReadLine]::Insert([String]$output) - } -} \ No newline at end of file diff --git a/src/commands/shell.rs b/src/commands/shell.rs index a9641b0..3d932bb 100644 --- a/src/commands/shell.rs +++ b/src/commands/shell.rs @@ -37,7 +37,7 @@ impl Runnable for Input { Shell::Fish => include_str!("../../shell/navi.plugin.fish"), Shell::Elvish => include_str!("../../shell/navi.plugin.elv"), Shell::Nushell => include_str!("../../shell/navi.plugin.nu"), - Shell::PowerShell => include_str!("../../shell/navi.plugin.psm1"), + Shell::Powershell => include_str!("../../shell/navi.plugin.ps1"), }; println!("{content}"); From 0531554816d1d6c65626b9cbfddceb832ac09cf6 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Fri, 14 Mar 2025 23:59:12 +0100 Subject: [PATCH 10/14] Integrates the shortcut key in the module definition Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index ffc4c93..f7a4159 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -30,4 +30,6 @@ $null = New-Module { [Microsoft.PowerShell.PSConsoleReadLine]::Insert([String]$output) } } + + Set-PSReadlineKeyHandler -BriefDescription "A keybinding to open Navi Widget" -Chord Ctrl+g -ScriptBlock { Invoke-NaviWidget } } From fbc94b0a3e1c0f433cdc9518883d123c148f78a2 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Sun, 16 Mar 2025 07:44:49 +0100 Subject: [PATCH 11/14] Update powershell plugin definition and exports Co-authored-by: KITAGAWA Yasutaka Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index f7a4159..3983898 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -1,6 +1,24 @@ $null = New-Module { + + function Invoke-Navi { + $startArgs = @{ + FileName = "navi"; + Arguments = $args; + RedirectStandardOutput = $true; + WorkingDirectory = $PWD; + } + $p = [System.Diagnostics.Process]@{StartInfo = $startArgs} + + [void]$p.Start() + $result = $p.StandardOutput.ReadToEnd() + $p.WaitForExit() + + $result + } + + ### Initial code from @lurebat (https://github.com/lurebat/) ### See #570 (https://github.com/denisidoro/navi/issues/570) for its original contribution function Invoke-NaviWidget { @@ -11,19 +29,19 @@ $null = New-Module { $output = $null if ([String]::IsNullOrEmpty($line)) { - $output = navi --print + $output = (Invoke-Navi "--print" | Out-String).Trim() } else { - $best_match = (navi --print --best-match --query $line | Out-String).Trim() + $best_match = (Invoke-Navi "--print --best-match --query `"$line`"" | Out-String).Trim() if ([String]::IsNullOrEmpty($best_match)) { - $output = (navi --print --query "$line" | Out-String).Trim() + $output = (Invoke-Navi "--print --query `"$line`"" | Out-String).Trim() } else { $output = $best_match } } - [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() + [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() ### Handling the case when the user escapes without selecting any entry if (-Not([String]::IsNullOrEmpty($output))) { @@ -32,4 +50,5 @@ $null = New-Module { } Set-PSReadlineKeyHandler -BriefDescription "A keybinding to open Navi Widget" -Chord Ctrl+g -ScriptBlock { Invoke-NaviWidget } + Export-ModuleMember -Function @() } From 838527dbbb0cf3edf7b68742410d921e32eb5a9e Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Sun, 16 Mar 2025 10:04:46 +0100 Subject: [PATCH 12/14] Update Module exports Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index 3983898..dd63c64 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -50,5 +50,5 @@ $null = New-Module { } Set-PSReadlineKeyHandler -BriefDescription "A keybinding to open Navi Widget" -Chord Ctrl+g -ScriptBlock { Invoke-NaviWidget } - Export-ModuleMember -Function @() + Export-ModuleMember -Function @( Navi-Widget ) } From 259114daa0d5b8ff4333b9fc8a079153a2ffdae0 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Sun, 16 Mar 2025 10:39:35 +0100 Subject: [PATCH 13/14] Fixes powershell export Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index dd63c64..3983898 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -50,5 +50,5 @@ $null = New-Module { } Set-PSReadlineKeyHandler -BriefDescription "A keybinding to open Navi Widget" -Chord Ctrl+g -ScriptBlock { Invoke-NaviWidget } - Export-ModuleMember -Function @( Navi-Widget ) + Export-ModuleMember -Function @() } From 211795f8d503d73d3ae8d644baae1b4eab0a9328 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Sun, 16 Mar 2025 11:28:26 +0100 Subject: [PATCH 14/14] Fixes the issue with unauthorized I/O redirection Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- shell/navi.plugin.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/navi.plugin.ps1 b/shell/navi.plugin.ps1 index 3983898..f31d74a 100644 --- a/shell/navi.plugin.ps1 +++ b/shell/navi.plugin.ps1 @@ -8,6 +8,7 @@ $null = New-Module { Arguments = $args; RedirectStandardOutput = $true; WorkingDirectory = $PWD; + UseShellExecute = $false; } $p = [System.Diagnostics.Process]@{StartInfo = $startArgs}