-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #924 from alexis-opolka/master
Add support for powershell on Windows + Powershell Plugin
- Loading branch information
Showing
7 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,4 @@ path = "src/lib.rs" | |
[[bin]] | ||
name = "navi" | ||
path = "src/bin/main.rs" | ||
bench = false | ||
bench = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
### 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]$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 | ||
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 | ||
} else { | ||
Write-Error "Navi-Widget is already installed for PowerShell!" | ||
exit 1 | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# | ||
# 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' | ||
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 = '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ pub enum Shell { | |
Fish, | ||
Elvish, | ||
Nushell, | ||
PowerShell, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
|