Last active
January 29, 2016 10:46
-
-
Save aetos382/904de73e793e5f1699a5 to your computer and use it in GitHub Desktop.
https://gist.github.com/aetos382/6128df251617ac5bea99 が変な風に進化したらこうなった。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$OutputHistoryContext = [PSCustomObject] @{ | |
Enabled = $true | |
DisabledTemporary = $false | |
Count = -1 | |
Queue = New-Object 'System.Collections.Queue' | |
LastOutputVariableName = '' | |
} | |
function Out-Default { | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline)] | |
[PSObject] $InputObject, | |
[Switch] $Transcript) | |
begin { | |
$context = $OutputHistoryContext | |
$wrappedCmdlet = $ExecutionContext.InvokeCommand.GetCmdlet('Microsoft.PowerShell.Core\Out-Default') | |
$sb = { & $wrappedCmdlet @PSBoundParameters } | |
$p = $sb.GetSteppablePipeline() | |
$p.Begin($PSCmdlet) | |
$outputs = @() | |
} | |
process { | |
$p.Process($_) | |
<# | |
Get-OutputHistory 内で DisabledTemporary を設定するより先に begin ブロックが実行されてしまうので | |
$enabled を begin ブロックで初期化してはいけない | |
#> | |
$enabled = $context.Enabled -and !$context.DisabledTemporary | |
if ($_ -and $enabled) { | |
$outputs += $_ | |
} | |
} | |
end { | |
$p.End() | |
if ($outputs -and $enabled) { | |
$q = $context.Queue | |
$q.Enqueue($outputs) | |
if ($context.Count -ge 0) { | |
while ($q.Count -gt $context.Count) { | |
[void] $q.Dequeue() | |
} | |
} | |
if ($context.LastOutputVariableName) { | |
New-Variable -Name $context.LastOutputVariableName -Value $outputs -Scope Global -Option ReadOnly -Force | |
} | |
} | |
$context.DisabledTemporary = $false | |
} | |
} | |
function Get-OutputHistory { | |
[OutputType([object[]])] | |
param([switch] $LogHistory) | |
$array = $OutputHistoryContext.Queue.ToArray() | |
if (!$LogHistory) { | |
$OutputHistoryContext.DisabledTemporary = $true | |
} | |
return $array | |
} | |
function Clear-OutputHistory { | |
$OutputHistoryContext.Queue.Clear() | |
} | |
function Enable-OutputHistory { | |
$OutputHistoryContext.Enabled = $true | |
} | |
function Disable-OutputHistory { | |
$OutputHistoryContext.Enabled = $false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment