diff --git a/Build.ps1 b/Build.ps1 new file mode 100644 index 0000000..050484e --- /dev/null +++ b/Build.ps1 @@ -0,0 +1,2 @@ +rm ./Output -Recurse -Force +Build-Module diff --git a/Source/Public/f7_history.ps1 b/Source/Public/f7_history.ps1 index 76b8b14..31364e4 100644 --- a/Source/Public/f7_history.ps1 +++ b/Source/Public/f7_history.ps1 @@ -14,18 +14,44 @@ function f7_history { if ($global) { # Global history - $history = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems().CommandLine - # reverse the items so most recent is on top - [array]::Reverse($history) - $selection = $history | Select-Object -Unique | Out-ConsoleGridView -OutputMode Single -Filter $line -Title "Command History for All Powershell Instances" + $historyItems = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() + [array]::Reverse($historyItems) + + $seen = @{} + $history = $historyItems | Where-Object { + $key = $_.CommandLine + if (-not $seen.ContainsKey($key)) { + $seen[$key] = $true + return $true + } + return $false + } | ForEach-Object { + $startTime = if ($_.StartTime -ne $null -and $_.StartTime -ne [datetime]::MinValue) { $_.StartTime.ToLocalTime() } else { $null } + [PSCustomObject]@{ 'CommandLine' = $_.CommandLine; 'When' = $startTime } + } + + if ($historyItems -eq $null -or $historyItems.Count -eq 0) { + Write-Host "The global (PSReadLine) history is empty." + return + } + + $selection = $history | Out-ConsoleGridView -OutputMode Single -Filter $line -Title "Command History for All Powershell Instances" + } else { # Local history - $history = Get-History | Sort-Object -Descending -Property Id -Unique | Select-Object CommandLine -ExpandProperty CommandLine + $history = Get-History | Sort-Object -Descending -Property Id | Select-Object @{Name = 'CommandLine'; Expression = { $_.CommandLine } } -Unique + + if ($history -eq $null -or $history.Count -eq 0) { + Write-Host "The PowerShell history is empty." + return + } + $selection = $history | Out-ConsoleGridView -OutputMode Single -Filter $line -Title "Command History" } - if ($selection) { + if ($selection.Count -gt 0) { + $selection = $selection.'CommandLine' [Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine() [Microsoft.PowerShell.PSConsoleReadLine]::Insert($selection) if ($selection.StartsWith($line)) { @@ -39,15 +65,16 @@ function f7_history { if ($args.Count -eq 0 -or $null -eq $args[0]["Key"]) { $key = "F7" -} else { +} +else { $key = $args[0]["Key"] } # When F7 is pressed, show the local command line history in OCGV $parameters = @{ - Key = $key + Chord = $key BriefDescription = 'Show Matching Command History' - LongDescription = 'Show Matching Command History using Out-ConsoleGridView' + Description = 'Show Matching Command History using Out-ConsoleGridView' ScriptBlock = { f7_history -Global $false } @@ -56,15 +83,16 @@ Set-PSReadLineKeyHandler @parameters if ($args.Count -eq 0 -or $null -eq $args[0]["AllKey"]) { $allkey = "Shift-F7" -} else { +} +else { $allkey = $args[0]["AllKey"] } # When Shift-F7 is pressed, show the local command line history in OCGV $parameters = @{ - Key = $allkey + Chord = $allkey BriefDescription = 'Show Matching Command History for All' - LongDescription = 'Show Matching Command History for all PowerShell instances using Out-ConsoleGridView' + Description = 'Show Matching Command History for all PowerShell instances using Out-ConsoleGridView' ScriptBlock = { f7_history -Global $true }