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

Refactor history retrieval and handling #254

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Changes from all commits
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
63 changes: 40 additions & 23 deletions PSFzf.Base.ps1
Original file line number Diff line number Diff line change
@@ -768,50 +768,67 @@ function Invoke-FzfPsReadlineHandlerProvider {
$replaceLen = $rightCursor - $leftCursor
if ($rightCursor -eq 0 -and $leftCursor -eq 0) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($str)
} else {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($leftCursor,$replaceLen+1,$str)
}
else {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($leftCursor, $replaceLen + 1, $str)
}
}
}
function Invoke-FzfPsReadlineHandlerHistory {
$result = $null
try
{

function Get-PickedHistory($Query = '', [switch]$UsePSReadLineHistory) {
try {
$script:OverrideFzfDefaults = [FzfDefaultOpts]::new($env:FZF_CTRL_R_OPTS)

$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadline]::GetBufferState([ref]$line, [ref]$cursor)
$fileHist = @{}
if ($UsePSReadLineHistory) {
$reader = New-Object PSFzf.IO.ReverseLineReader -ArgumentList $((Get-PSReadlineOption).HistorySavePath)

$reader = New-Object PSFzf.IO.ReverseLineReader -ArgumentList $((Get-PSReadlineOption).HistorySavePath)
$result = $reader.GetEnumerator() | ForEach-Object {
if (-not $fileHist.ContainsKey($_)) {
$fileHist.Add($_, $true)
$_
}
} | Invoke-Fzf -Query "$Query" -Bind ctrl-r:toggle-sort, ctrl-z:ignore -Scheme history
}
else {
$result = Get-History | ForEach-Object { $_.CommandLine } | ForEach-Object {
if (-not $fileHist.ContainsKey($_)) {
$fileHist.Add($_, $true)
$_
}
} | Invoke-Fzf -Query "$Query" -Reverse -Scheme history
}

$fileHist = @{}
$reader.GetEnumerator() | ForEach-Object {
if (-not $fileHist.ContainsKey($_)) {
$fileHist.Add($_,$true)
$_
}
} | Invoke-Fzf -Query "$line" -Bind ctrl-r:toggle-sort, ctrl-z:ignore -Scheme history | ForEach-Object { $result = $_ }
}
catch
{
catch {
# catch custom exception
}
finally
{
finally {
if ($script:OverrideFzfDefaults) {
$script:OverrideFzfDefaults.Restore()
$script:OverrideFzfDefaults = $null
}

# ensure that stream is closed:
$reader.Dispose()
if ($reader) {
$reader.Dispose()
}
}

$result
}
function Invoke-FzfPsReadlineHandlerHistory {
$result = $null
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadline]::GetBufferState([ref]$line, [ref]$cursor)

$result = Get-PickedHistory -Query $line -UsePSReadLineHistory

InvokePromptHack

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

7 changes: 1 addition & 6 deletions PSFzf.Functions.ps1
Original file line number Diff line number Diff line change
@@ -169,12 +169,7 @@ function Invoke-FuzzyFasd() {

#.ExternalHelp PSFzf.psm1-help.xml
function Invoke-FuzzyHistory() {
if (Get-Command Get-PSReadLineOption -ErrorAction Ignore) {
$result = Get-Content (Get-PSReadLineOption).HistorySavePath | Invoke-Fzf -Reverse -Scheme history
}
else {
$result = Get-History | ForEach-Object { $_.CommandLine } | Invoke-Fzf -Reverse -Scheme history
}
$result = Get-PickedHistory -UsePSReadLineHistory:$($null -ne $(Get-Command Get-PSReadLineOption -ErrorAction Ignore))
if ($null -ne $result) {
Write-Output "Invoking '$result'`n"
Invoke-Expression "$result" -Verbose