ps/Modules/Cole.PowerShell.Developer/Public/Show-Line.ps1
2023-05-30 22:51:22 -07:00

131 lines
5.0 KiB
PowerShell

function Show-Line {
param (
[Parameter(Mandatory = $false)]
[string]$Width = '100%',
[Parameter(Mandatory = $false)]
[ValidateSet('Line','Bold','Dashed','BoldDashed','DashedNarrow','BoldDashedNarrow','DashedWide','BoldDashedWide','LeftHalf','RightHalf','BoldLeftHalf','BoldRightHalf','BoldRightThinLeft','BoldLeftThinRight','Double')]
[string]$Style = 'Line',
[Parameter(Mandatory = $false)]
[ValidateSet('Black','Blue','Cyan','DarkGray','Green','LightBlue','LightCyan','LightGray','LightGreen','LightMagenta','LightRed','LightYellow','Magenta','Red','White','Yellow','Default')]
[string]$Color = 'Default',
[int]$Padding,
[int]$PaddingStart,
[int]$PaddingEnd,
[switch]$WithEndcaps,
[Parameter(Mandatory = $false)]
[ValidateSet('Line','Dashed','DashedNarrow','DashedWide','HalfTop','HalfBottom','Bold','BoldDashed','BoldDashedNarrow','BoldDashedWide','BoldTopHalf','BoldTopThinBottom','BoldBottomHalf','BoldBottomThinTop','Double')]
[string]$EndcapStyle,
[switch]$WithBoldEndcaps, # This provided for simplicity and ease of use
[switch]$WithLeftEndcap,
[Parameter(Mandatory = $false)]
[ValidateSet('Line','Dashed','DashedNarrow','DashedWide','HalfTop','HalfBottom','Bold','BoldDashed','BoldDashedNarrow','BoldDashedWide','BoldTopHalf','BoldTopThinBottom','BoldBottomHalf','BoldBottomThinTop','Double')]
[string]$LeftEndcapStyle,
[switch]$WithRightEndcap,
[Parameter(Mandatory = $false)]
[ValidateSet('Line','Dashed','DashedNarrow','DashedWide','HalfTop','HalfBottom','Bold','BoldDashed','BoldDashedNarrow','BoldDashedWide','BoldTopHalf','BoldTopThinBottom','BoldBottomHalf','BoldBottomThinTop','Double')]
[string]$RightEndcapStyle
)
$logLead = Get-LogLeadName
$isNumberRegex = "^\d+$"
$isPercentRegex = "^\d+%$"
$ResetColor = $PSStyle.Reset
$_style = $PSBox.Horizontal.$Style
if ($WithBoldEndcaps) {
$WithEndcaps = $true
}
if ($WithEndcaps) {
$WithLeftEndcap = $true
$WithRightEndcap = $true
}
if (![string]::IsNullOrWhiteSpace($EndcapStyle)) {
if ([string]::IsNullOrWhiteSpace($LeftEndcapStyle)) {
$LeftEndcapStyle = $EndcapStyle
}
if ([string]::IsNullOrWhiteSpace($RightEndcapStyle)) {
$RightEndcapStyle = $EndcapStyle
}
}
$useBoldHorizontal = $Style -contains 'Bold'
$useDoubleHorizontal = $Style -contains 'Double'
if (![string]::IsNullOrWhiteSpace($LeftEndcapStyle)) {
$useBoldVertical = $LeftEndcapStyle -eq 'Bold'
}
if ($Padding -gt 0 -and $PaddingStart -eq 0) {
$PaddingStart = $Padding
}
if ($Padding -gt 0 -and $PaddingEnd -eq 0) {
$PaddingEnd = $Padding
}
$prefix = "".PadRight($PaddingStart)
$suffix = "".PadRight($PaddingEnd)
$screenWidth = Get-ConsoleDisplayWidth
$boxWidth = $screenWidth - $PaddingStart -$PaddingEnd
$widthIsNumber = $Width -match $isNumberRegex
$widthIsPercent = $Width -match $isPercentRegex
if ($widthIsNumber) {
$boxWidth = [int]$Width
if ($boxWidth -gt $screenWidth) {
$boxWidth = $screenWidth
}
}
if ($widthIsPercent) {
$boxWidth = [System.Math]::Round((([int]($Width.Substring(0,$Width.Length - 1)) * $boxWidth )/ 100.0))
}
if (!$widthIsNumber -and !$widthIsPercent) {
throw "$logLead : Width parameter must be an int or a percent expressed as an integer and percent sign. Examples: 10, 25%, 100"
}
if ($Color -eq 'Default') {
$_color = $ResetColor
} else {
$_color = $PSStyle.ForegroundColor.$Color
}
$endcapStart = ""
$endcapEnd = ""
if ($WithEndcaps) {
if ($useBoldHorizontal) {
if ($useBoldVertical) {
$endcapStart = $PSBox.Tee.Vertical.Left.Bold
$endcapend = $PSBox.Tee.Vertical.Right.Bold
} else {
$endcapStart = $PSBox.Tee.Vertical.Left.BoldHorizontal
$endcapend = $PSBox.Tee.Vertical.Right.BoldHorizontal
}
} elseif ($useDoubleHorizontal) {
if ($LeftEndcapStyle -eq "Double") {
$endcapStart = $PSBox.Tee.Vertical.Left.Double
$endcapStart = $PSBox.Tee.Vertical.Right.Double
} else {
$endcapStart = $PSBox.Tee.Vertical.Left.DoubleHorizontal
$endcapStart = $PSBox.Tee.Vertical.Right.DoubleHorizontal
}
$endcapend = $PSBox.Tee.Vertical.Right.Double
} else {
if ($useBoldVertical) {
$endcapStart = $PSBox.Tee.Vertical.Left.BoldVertical
$endcapend = $PSBox.Tee.Vertical.Right.BoldVertical
} else {
$endcapStart = $PSBox.Tee.Vertical.Left.Line
$endcapend = $PSBox.Tee.Vertical.Right.Line
}
}
$boxWidth = $boxWidth - 2
}
$body = $_style * $boxWidth
return "$prefix$_color$endcapStart$body$endcapEnd$ResetColor$suffix"
}