Write-HostPlus.psm1
function Write-HostPlus { <# .SYNOPSIS Wrapper around the Write-Host Cmdlet. Adds extended functionality. .DESCRIPTION Wrapper around the Write-Host Cmdlet. Added support for text coloring/formatting via ANSI Escape Sequences, and text alignment/justification. .PARAMETER InputObject Object to be written to the Console. .PARAMETER ForegroundColor Integer value associated with an ANSI Escape Sequence Color Code (0-255). .PARAMETER BackgroundColor Integer value associated with an ANSI Escape Sequence Color Code (0-255). .PARAMETER ForegroundConsoleColor ; .PARAMETER BackgroundConsoleColor ; .PARAMETER Formatting Integer value associated with an ANSI Escape Sequence Formatting Code (1-9) .PARAMETER Justification Specifies the alignment/justification for the output text. (LeftAligned, RightAligned, CenterAligned) .PARAMETER NoNewLine ; .PARAMETER Separator ; . #> [CmdletBinding()] param( [Parameter(Mandatory)] [object]$InputObject, [Parameter()] [int32]$ForegroundColor = 255, [Parameter()] [int32]$BackgroundColor = 0, #[Parameter(ParameterSetName='ConsoleColor')] #[ConsoleColor]$ForegroundConsoleColor, #[Parameter(ParameterSetName='ConsoleColor')] #[ConsoleColor]$BackgroundConsoleColor, [Parameter()] #[ValidateSet('Blink','Bold','Italic','Dim','Hide','Reverse','Underline','Strikethrough')] [int[]]$Formatting, [Parameter()] [ValidateSet('LeftAligned','RightAligned','CenterAligned')] [string]$Justification = 'LeftAligned', [switch]$NoNewLine ) begin{ $Reset = "`e[0m" $Output = $InputObject <# ADD CONSOLECOLOR FUNCTIONALITY HERE #> if ($ForegroundColor -gt $null) { $Output = "`e[38;5;$ForegroundColor" + "m" + "$Output" } if ($BackgroundColor -gt $null) { $Output = "`e[48;5;$BackgroundColor" + "m" + "$Output" } if ($Formatting -gt $null) { $FormatStyle = @() ForEach ($Code in $Formatting) { $Sequence = "`e[" + $Code + "m" $FormatStyle += $Sequence } $Output = "$FormatStyle" + "$Output" } $Output = "$Output" + "$Reset" } process{ if ($Justification -eq 'LeftAligned') { if ($NoNewLine) { Write-Host $Output -NoNewLine } else { Write-Host $Output } } elseif ($Justification -eq 'RightAligned') { if ($NoNewLine) { $ConsoleWidth = $Host.UI.RawUI.BufferSize.Width $TextLength = $Output.Length $Padding = ($ConsoleWidth - $TextLength) $Spacing = " " * $ConsoleWidth Write-Host "$($Spacing)$Output" -NoNewLine } else { $ConsoleWidth = $Host.UI.RawUI.BufferSize.Width $TextLength = $Output.Length $Padding = ($ConsoleWidth - $TextLength) $Spacing = " " * $Padding Write-Host "$($Spacing)$Output" } } elseif ($Justification -eq 'CenterAligned') { if ($NoNewLine) { $ConsoleWidth = $Host.UI.RawUI.BufferSize.Width $Padding = [Math]::Max(0, ($ConsoleWidth - $Output.Length) / 2) $CenteredOutput = $Output.PadLeft($Output.Length + [Math]::Floor($Padding), ' ').PadRight($ConsoleWidth, ' ') Write-Host $CenteredOutput -NoNewLine } else { $ConsoleWidth = $Host.UI.RawUI.BufferSize.Width $Padding = [Math]::Max(0, ($ConsoleWidth - $Output.Length) / 2) $CenteredOutput = $Output.PadLeft($Output.Length + [Math]::Floor($Padding), ' ').PadRight($ConsoleWidth, ' ') Write-Host $CenteredOutput } } } end{ } } |