PoshColorFunctions.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function Pad
{
    param([System.String] $value, [System.Int32] $length)

    return $value.PadRight($length)
}
function New-ColorVTSequence
{
    param([System.String] $value)

    $color = [System.Drawing.ColorTranslator]::FromHtml($value)

    $r = $color.R;
    $g = $color.G;
    $b = $color.B;

    return "$r;$g;$b";
}

function New-ForegroundVTSequence
{
    param([System.String] $hexColor)

    $rgbSequence = New-ColorVTSequence -value $hexColor

    return "38;2;$rgbSequence"
}

function New-BackgroundVTSequence
{
    param([System.String] $hexColor)

    $rgbSequence = New-ColorVTSequence -value $hexColor

    return "48;2;$rgbSequence"
}

function New-TextColorVTSequence{
    param([System.String] $foregroundHexColor, [System.String] $backgroundHexColor)

    $result = New-ColorVTSequence -value $foregroundHexColor

    if ($backgroundHexColor)
    {
        $backgroundSequence = New-ColorVTSequence -value $backgroundHexColor

        $result = "$result;$backgroundSequence";
    }

    return "$result" + "m"
}

function Convert-ToRGB
{
    param([System.String]$color)

    $colorValue = [System.Drawing.Color]::FromName($color)

    # if the passed in color name is not a known name, just use white
    if (!$colorValue.IsKnownColor)
    {
        $colorValue = [System.Drawing.Color]::White
    }

    $hexValue = "#" + $colorValue.R.ToString() + $colorValue.G.ToString() + $colorValue.B.ToString()

    return $hexValue
}

function Write-HostColor
{
    param ([Parameter(Position=0)] [System.String] $text, [Parameter(Position=1)][System.String] $foreground,  [System.String] $background, [switch] $noNewLine = $false)

    if ($global:PoshColor.UseConsoleColors)
    {
        if ($background)
        {
            if ($noNewLine)
            {
                Write-Host $text -ForegroundColor $foreground -BackgroundColor $background -NoNewline
            }
            else {
                Write-Host $text -ForegroundColor $foreground -BackgroundColor $background
            }
        }
        else {
            if ($noNewLine)
            {
                Write-Host $text -ForegroundColor $foreground -NoNewline
            }
            else {
                Write-Host $text -ForegroundColor $foreground
            }
        }
    }
    else {

        ## Try and parse colours as named colors
        if (!$foreground.StartsWith("#"))
        {
            $foreground = (Convert-ToRGB $foreground)
        }
        
        $foregroundSequence = New-ForegroundVTSequence -hexColor $foreground

        if ($background)
        {
            if (!$background.StartsWith("#"))
            {
                $background = (Convert-ToRGB $background)
            }

            $backgroundSequence = New-BackgroundVTSequence -hexColor $background
        }

        $vtSequence = "$foregroundSequence"

        if ($backgroundSequence)
        {
            $vtSequence = "$vtSequence;$backgroundSequence";
        }

        $vtSequence = "`e[$vtSequence" + "m"

        if ($noNewLine)
        {
            Write-Host "$vtSequence$text" -NoNewLine
        }
        else {
            Write-Host "$vtSequence$text"
        }
    }
}

function Trim
{
    param ([Parameter(Mandatory=$True,Position=1)][string] $text, [Parameter(Mandatory=$True,Position=2)][int] $length)

    if ($text.Length -gt $length)
    {
        $text = $text.Substring(0, $length - 3) + "..."
    }

    return $text;
}