Public/Formatting/Write-Emphatic.ps1
Set-StrictMode -Version 2.0 Function Write-Emphatic { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost','')] param( [Parameter(Mandatory=$true)] [string]$TaskName, [Parameter(Mandatory=$true)] [string]$TaskType, [Parameter(Mandatory=$true)] [string]$Info, [ConsoleColor]$ForegroundColor = 'Magenta' ) function StringFormat { param( [int]$length, [string]$value, [string]$prefix = '', [string]$postfix = '', [switch]$padright ) # wraps string in spaces so we reduce length by two $length = $length - 2 #- $postfix.Length - $prefix.Length if($value.Length -gt $length){ # Reduce to length - 4 for elipsis $value = $value.Substring(0, $length - 4) + '...' } $value = " $value " if($padright){ $value = $value.PadRight($length, '-') } else { $value = $value.PadLeft($length, '-') } return $prefix + $value + $postfix } $actualWidth = (Get-Host).UI.RawUI.BufferSize.Width $width = $actualWidth - ($actualWidth % 2) $half = $width / 2 $leftString = StringFormat -length $half -value "$TaskName [$TaskType]" -prefix '[' -postfix ':' $rightString = StringFormat -length $half -value $Info -postfix ']' -padright $message = ($leftString + $rightString) Write-Host $message -ForegroundColor 'Magenta' } |