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

Removes dupes from global history. Fixes column header. #11

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rm ./Output -Recurse -Force
Build-Module
52 changes: 40 additions & 12 deletions Source/Public/f7_history.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down