ps/Modules/Cole.PowerShell.Developer/Public/Get-HistoryEntries.ps1

62 lines
1.6 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-HistoryEntries {
<#
.SYNOPSIS
Get the history according to what was saved by PSReadLine
#>
[CmdletBinding()]
[OutputType([System.Collections.ArrayList])]
param (
[switch]$SkipMultilines,
[switch]$OnlyMultilines,
[switch]$SkipLongLines,
[Parameter()]
[string[]]$AdditionalPath
)
$logLead = Get-LogLeadName
$longLineLength = 120 # 120 characters is a pretty long line
if ($SkipMultilines -and $OnlyMultilines) {
throw "$logLead : You can't specify both skip and only multilines"
}
try {
$path = (Get-PSReadlineOption).HistorySavePath
} catch {
Write-Warning "$logLead : Is PSReadLine installed? Can't find the history save path"
return
}
$lines = @()
foreach ($filePath in $AdditionalPath) {
$lines += Get-Content -Path $filePath
}
$lines += Get-Content -Path $path
$records = New-Object -TypeName "System.Collections.ArrayList"
$isMultiline = $false
$groupedLines = @()
foreach ($line in $lines) {
if ($line.EndsWith('`')) {
$line = $line.TrimEnd('`')
$groupedLines += $line
$isMultiline = $true
continue
}
$groupedLines += $line
$lineIsLong = ($groupedLines -join '').Length -gt $longLineLength
$skipAdd = ($SkipLongLines -and $lineIsLong) -or ($OnlyMultilines -and !$isMultiline) -or ($SkipMultilines -and $isMultiline)
if (!$skipAdd) {
$records.Add($groupedLines) | Out-Null
}
$groupedLines = @()
$isMultiline = $false
}
return $records
}