Last active
February 8, 2024 00:14
-
-
Save trackd/d6d2bcae1fb0e7c8ba01e51153649ce5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function Enable-DebugSpectre { | |
[cmdletbinding()] | |
param() | |
$moduleName = 'PwshSpectreConsole' | |
$module = Get-Module -Name $ModuleName | |
if (-Not $Module) { | |
$Module = Import-Module $ModuleName -PassThru | |
} | |
$new = { | |
param( | |
[Parameter(Mandatory)] | |
[Spectre.Console.Rendering.Renderable]$RenderableObject | |
) | |
try { | |
$writer = [System.IO.StringWriter]::new() | |
$output = [Spectre.Console.AnsiConsoleOutput]::new($writer) | |
$settings = [Spectre.Console.AnsiConsoleSettings]::new() | |
$settings.Out = $output | |
$console = [Spectre.Console.AnsiConsole]::Create($settings) | |
$console.Write($RenderableObject) | |
$writer.ToString() | |
} | |
finally { | |
$writer.Dispose() | |
} | |
} | |
# if you want to get an already defined function. | |
# $new = $ExecutionContext.InvokeCommand.GetCommand('Debug-WriteAnsiConsole', 'function') | |
$sb = { | |
param($func) | |
# to override the function | |
# Set-Content -Path Function:\Write-AnsiConsole -Value $func -Force | |
# using an alias is easier to revert. | |
Set-Content -Path Function:\Debug-WriteAnsiConsole -Value $func -Force | |
Set-Alias -Name Write-AnsiConsole -Value Debug-WriteAnsiConsole -Force -Option AllScope -Scope Global | |
# this is only needed for alias to work | |
Export-ModuleMember -Alias Write-AnsiConsole -Function Debug-WriteAnsiConsole | |
} | |
. $module $sb $new | |
} | |
function Disable-DebugSpectre { | |
# Remove-Module -Name PwshSpectreConsole -Force | |
# Import-Module -name PwshSpectreConsole | |
Remove-Alias -Name Write-AnsiConsole -Force -Scope Global | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment