mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-08 20:38:47 +00:00
Replace vendor powershell.tmBundle by SublimeText powershell. Improve samples for PS.
This commit is contained in:
65
samples/PowerShell/history.ps1
Normal file
65
samples/PowerShell/history.ps1
Normal file
@@ -0,0 +1,65 @@
|
||||
function Save-HistoryAll() {
|
||||
$history = Get-History -Count $MaximumHistoryCount
|
||||
[array]::Reverse($history)
|
||||
$history = $history | Group CommandLine | Foreach {$_.Group[0]}
|
||||
[array]::Reverse($history)
|
||||
$history | Export-Csv $historyPath
|
||||
}
|
||||
|
||||
function Save-HistoryIncremental() {
|
||||
# Get-History -Count $MaximumHistoryCount | Group CommandLine | Foreach {$_.Group[0]} | Export-Csv $historyPath
|
||||
Get-History -Count 1 | Export-Csv -Append $historyPath
|
||||
}
|
||||
|
||||
# hook powershell's exiting event & hide the registration with -supportevent.
|
||||
#Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { Save-History }
|
||||
|
||||
$oldPrompt = Get-Content function:\prompt
|
||||
|
||||
if( $oldPrompt -notlike '*Save-HistoryIncremental*' )
|
||||
{
|
||||
$newPrompt = @'
|
||||
Save-HistoryIncremental
|
||||
|
||||
'@
|
||||
$newPrompt += $oldPrompt
|
||||
$function:prompt = [ScriptBlock]::Create($newPrompt)
|
||||
}
|
||||
|
||||
# load previous history, if it exists
|
||||
if ((Test-Path $historyPath)) {
|
||||
$loadTime =
|
||||
(
|
||||
Measure-Command {
|
||||
Import-Csv $historyPath | Add-History
|
||||
Save-HistoryAll
|
||||
Clear-History
|
||||
Import-Csv $historyPath | ? {$count++;$true} | Add-History
|
||||
}
|
||||
).totalseconds
|
||||
Write-Host -Fore Green "`nLoaded $count history item(s) in $loadTime seconds.`n"
|
||||
}
|
||||
|
||||
|
||||
function Search-History()
|
||||
{
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Retrive and filter history based on query
|
||||
.DESCRIPTION
|
||||
.PARAMETER Name
|
||||
.EXAMPLE
|
||||
.LINK
|
||||
#>
|
||||
|
||||
param(
|
||||
[string[]] $query
|
||||
)
|
||||
|
||||
$history = Get-History -Count $MaximumHistoryCount
|
||||
foreach ($item in $query){
|
||||
$item = $item.ToLower()
|
||||
$history = $history | where {$_.CommandLine.ToLower().Contains($item)}
|
||||
}
|
||||
$history
|
||||
}
|
||||
Reference in New Issue
Block a user