Functions/Get-DryHeader.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Function Get-DryHeader {
    [CmdletBinding(DefaultParameterSetName="multilineheader")]
    [OutputType([Array],ParameterSetName="multilineheader")]
    [OutputType([String],ParameterSetName="singlelineheader")]
    Param (
        [Parameter(Mandatory)]
        [PSObject]$Message,

        [Parameter(HelpMessage="Number of whitespaces on the right side of screen, after each message line")]
        [Int]$PostBuffer = 3,

        [Parameter(HelpMessage="A sequence of characters of a specific length")]
        [String]$HeaderChars = '.',

        [Alias("sh")]
        [Parameter(Mandatory,ParameterSetName="singlelineheader",HelpMessage="Get a smaller, one-line header")]
        [Switch]$Small,

        [Parameter(HelpMessage="Converts 'Message' to 'M e s s a g e'")]
        [Switch]$Air,

        [Parameter(Mandatory,ParameterSetName="singlelineheader")]
        [Parameter(Mandatory,ParameterSetName="multilineheader")]
        [Int]$TargetMessageLength
    )

    # Make an array of the header chars
    For ($hca = 0; $hca -lt $HeaderChars.length; $hca++) {
        [Array]$HeaderCharsArray += $HeaderChars[$hca]
    }

    # Converts 'Message' to 'M e s s a g e'
    If ($Air) {
        $AiredMessage = ''
        For ($LetterCount = 0; $LetterCount -lt $Message.Length; $LetterCount++) {
            $AiredMessage += $Message.SubString($LetterCount,1)
            $AiredMessage = "$AiredMessage "
        }
        $Message = $AiredMessage
    }

    Switch ($PSCmdlet.ParameterSetName) {
        'singlelineheader' {
            # add $PostBuffer
            For ($b = 0; $b -lt $PostBuffer; $b++) {
                $Message = "$Message "
            }

            # Determine which of the elements in the array to start on - this is to make HeaderChars aligned
            $HeaderCharIndex = ($Message.length)%($HeaderCharsArray.length)

            While ($Message.Length -lt $TargetMessageLength) {
                $Message = $Message + $HeaderCharsArray[$HeaderCharIndex]
                $HeaderCharIndex++
                If ($HeaderCharIndex -gt ($HeaderCharsArray.length-1)) {
                    $HeaderCharIndex = 0
                }
            }
            $Message
         }
        'multilineheader' {
            # Just output the header line
            [String]$HeaderLine = ''
            $HeaderCharIndex = 0
            While ($HeaderLine.Length -lt $TargetMessageLength) {
                $HeaderLine = $HeaderLine + $HeaderCharsArray[$HeaderCharIndex]
                $HeaderCharIndex++
                If ($HeaderCharIndex -gt ($HeaderCharsArray.length-1)) {
                    $HeaderCharIndex = 0
                }
            }
            # Output the header line and message
            $HeaderLine,$Message
        }
    }
}