Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append Specific function to handle host in history #141

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions PSFzf.Base.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,39 @@ function Invoke-FzfPsReadlineHandlerHistoryArgs {
}
}

function Invoke-FzfHandlerHistoryHost {
$Commands = @('icm','Invoke-Command','Enter-PSSession','etsn')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bit limiting, and seems case-sensitive? Should probably be using (Get-Command $Name -Type Alias).ResolvedCommandname (plus error checking) to get the command from an alias so that it also works for custom aliases, not just icm and etsn.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ! Maybe something like that :

	$Cmdlets = @('Invoke-Command','Enter-PSSession')
	$Alias = (get-alias |? { $Cmdlets -contains $_.ResolvedCommand }).Name
	$Commands = $Cmdlets + $Alias

And as in Powershell 5, Get-Service, Get-Process, Get-WmiObject used to have a "-ComputerName" parameters, this entries should be searched in history

$result = @()
try
{
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadline]::GetBufferState([ref]$line, [ref]$cursor)
$line = $line.Insert($cursor,"{}") # add marker for fzf

$contentTable = @{}
$reader = New-Object PSFzf.IO.ReverseLineReader -ArgumentList $((Get-PSReadlineOption).HistorySavePath)

$reader.GetEnumerator() | Where-Object { $_ -match $($Commands -join '|') } | ForEach-Object {
$tokens = ([System.Management.Automation.PsParser]::Tokenize($_, [ref] $null) | Where-Object { $_.Type -eq 'CommandArgument' })
if( $tokens.Count -gt 0){
$tokens[0].Content
}
} | Invoke-Fzf -NoSort -Bind ctrl-r:toggle-sort | ForEach-Object { $result = $_ }

}catch{

}finally{
$reader.Dispose()
}

InvokePromptHack

if (-not [string]::IsNullOrEmpty($result)) {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($cursor,0, ' ' + $result)
}
}

function Invoke-FzfPsReadlineHandlerSetLocation {
$result = $null
try
Expand Down