Functions/Logging/Write-Title.ps1
<#
.SYNOPSIS Writes string as title to Host-Output. .DESCRIPTION Creates pretty format for a subject in the host output. .EXAMPLE Write-Title -Title 'My Title' OUTPUT: ------------------------------------------------------------- My Title ------------------------------------------------------------- .EXAMPLE Write-Title 'My Title' $false 40 '*' OUTPUT: **************************************** My Title **************************************** .EXAMPLE Write-Title 'end' -SingleLine OUTPUT: -----------------------------end----------------------------- #> function Write-Title { [CmdletBinding()] Param ( # Writes the title to the host with a multi-line pretty format. [Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $true)] [string] $Title, # Writes a single line in pretty format. [Parameter(Position=1, ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $true)] [switch] $SingleLine, # The character used for spacing [Parameter(Position=2, ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $true)] [string] $SpacerCharacter = '-', # The total length of the string to return [Parameter(Position=3, ValueFromPipeline=$true, ValueFromPipelineByPropertyName = $true)] [int] $DividerLength = 60 ) if($SingleLine){ Set-Spacer -title $Title -dividerLength $DividerLength -spacerCharacter $SpacerCharacter | Write-Output return } $prettyTitle = @() $prettyTitle += Set-Spacer -dividerLength $DividerLength -spacerCharacter $SpacerCharacter $prettyTitle += Set-Spacer -title $Title -dividerLength $DividerLength -spacerCharacter ' ' $prettyTitle += Set-Spacer -dividerLength $DividerLength -spacerCharacter $SpacerCharacter $prettyTitle | Write-Output return } Export-ModuleMember -Function Write-Title function Set-Spacer{ param( [string] $title = '', [int] $dividerLength = 60, [string] $spacerCharacter = ' ' ) if($title.Length -ge ($dividerLength - 2)){ return $title } $prettyTitle = '' for ($i = 1; $i -le ([math]::Truncate((($dividerLength - 2) - $title.Length) / 2)); $i++){ $prettyTitle += $spacerCharacter } $prettyTitle += $title $suffixLenght = $dividerLength - $prettyTitle.Length for ($i = 1; $i -le $suffixLenght; $i++){ $prettyTitle += $spacerCharacter } return $prettyTitle } |