Skip to content

Commit

Permalink
Refactor history retrieval and handling
Browse files Browse the repository at this point in the history
- Invoke-FuzzyHistory() (alis: 'fh') now removes duplicates
- Resolves #219
  • Loading branch information
kelleyma49 committed Feb 18, 2024
1 parent fd1a428 commit 1c38824
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
63 changes: 40 additions & 23 deletions PSFzf.Base.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
7 changes: 1 addition & 6 deletions PSFzf.Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c38824

Please sign in to comment.