functions/message/Write-PSFHostColor.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
function Write-PSFHostColor
{
<#
 .SYNOPSIS
  Function that recognizes html-style tags to insert color into printed text.
  
 .DESCRIPTION
  Function that recognizes html-style tags to insert color into printed text.
   
  Color tags should be designed to look like this:
  <c="<console color>">Text</c>
  For example this would be a valid string:
  "This message should <c="red">partially be painted in red</c>!"
   
  This allows specifying color within strings and avoids having to piece together colored text in multiple calls to Write-Host.
  Only colors that are part of the ConsoleColor enumeration can be used. Bad colors will be ignored in favor of the default color.
  
 .PARAMETER String
  The message to write to host.
  
 .PARAMETER DefaultColor
  Default: (Get-DbaConfigValue -Name "message.infocolor")
  The color to write stuff to host in when no (or bad) color-code was specified.
  
 .PARAMETER NoNewLine
  Specifies that the content displayed in the console does not end with a newline character.
  
 .PARAMETER Level
  By default, all messages to Write-PSFHostColor will be printed to host.
  By specifying a level, it will only print the text if that level is within the range visible to the user.
  
  Visibility is controlled by the following two configuration settings:
    psframework.message.info.maximum
    psframework.message.info.minimum
  
 .EXAMPLE
  Write-PSFHostColor -String 'This is going to be <c="red">bloody red</c> text! And this is <c="green">green stuff</c> for extra color'
   
  Will print the specified line in multiple colors
  
 .EXAMPLE
  $string1 = 'This is going to be <c="red">bloody red</c> text! And this is <c="green">green stuff</c> for extra color'
  $string2 = '<c="red">bloody red</c> text! And this is <c="green">green stuff</c> for extra color'
  $string3 = 'This is going to be <c="red">bloody red</c> text! And this is <c="green">green stuff</c>'
  $string1, $string2, $string3 | Write-HostColor -DefaultColor "Magenta"
   
  Will print all three lines, respecting the color-codes, but use the color "Magenta" as default color.
  
 .EXAMPLE
  $stringLong = @"
  Dear <c="red">Sirs</c><c="green"> and</c> <c="blue">Madams</c>,
   
  it has come to our attention that you are not sufficiently <c="darkblue">awesome!</c>
  Kindly improve your <c="yellow">AP</c> (<c="magenta">awesome-ness points</c>) by at least 50% to maintain you membership in Awesome Inc!
   
  You have <c="green">27 3/4</c> days time to meet this deadline. <c="darkyellow">After this we will unfortunately be forced to rend you assunder and sacrifice your remains to the devil</c>.
   
  Best regards,
  <c="red">Luzifer</c>
  "@
  Write-PSFHostColor -String $stringLong
   
  Will print a long multiline text in its entirety while still respecting the colorcodes
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true)]
        [string[]]
        $String,
        
        [ConsoleColor]
        $DefaultColor = (Get-PSFConfigValue -FullName "psframework.message.info.color"),
        
        [switch]
        $NoNewLine,
        
        [PSFramework.Message.MessageLevel]
        $Level
    )
    begin
    {
        $em = [PSFramework.Message.MessageHost]::InfoColorEmphasis
        $sub = [PSFramework.Message.MessageHost]::InfoColorSubtle
        
        $max_info = [PSFramework.Message.MessageHost]::MaximumInformation
        $min_info = [PSFramework.Message.MessageHost]::MinimumInformation
    }
    process
    {
        if ($Level)
        {
            if (($max_info -lt $Level) -or ($min_info -gt $Level)) { return }
        }
        
        foreach ($line in $String)
        {
            foreach ($row in $line.Split("`n").Split([environment]::NewLine))
            {
                if ($row -notlike '*<c=["'']*["'']>*</c>*') { Microsoft.PowerShell.Utility\Write-Host -Object $row -ForegroundColor $DefaultColor -NoNewline:$NoNewLine }
                else
                {
                    $row = $row -replace '<c=["'']em["'']>', "<c='$em'>" -replace '<c=["'']sub["'']>', "<c='$sub'>"
                    $match = ($row | Select-String '<c=["''](.*?)["'']>(.*?)</c>' -AllMatches).Matches
                    $index = 0
                    $count = 0
                    
                    while ($count -le $match.Count)
                    {
                        if ($count -lt $Match.Count)
                        {
                            Microsoft.PowerShell.Utility\Write-Host -Object $row.SubString($index, ($match[$count].Index - $Index)) -ForegroundColor $DefaultColor -NoNewline
                            try { Microsoft.PowerShell.Utility\Write-Host -Object $match[$count].Groups[2].Value -ForegroundColor $match[$count].Groups[1].Value -NoNewline -ErrorAction Stop }
                            catch { Microsoft.PowerShell.Utility\Write-Host -Object $match[$count].Groups[2].Value -ForegroundColor $DefaultColor -NoNewline -ErrorAction Stop }
                            
                            $index = $match[$count].Index + $match[$count].Length
                            $count++
                        }
                        else
                        {
                            Microsoft.PowerShell.Utility\Write-Host -Object $row.SubString($index) -ForegroundColor $DefaultColor -NoNewline:$NoNewLine
                            $count++
                        }
                    }
                }
            }
        }
    }
}