Skip to content

Commit be29d7d

Browse files
authored
Feature: Adds option for formatted, colored console prints (#638)
* Adds feature for colored console prints
1 parent 5387506 commit be29d7d

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
3333
* [#633](https://github.com/Icinga/icinga-powershell-framework/pull/633) Adds support for Icinga 2.14.0 native Icinga for Windows API communication
3434
* [#635](https://github.com/Icinga/icinga-powershell-framework/pull/635) Adds support for `Write-IcingaAgentApiConfig` function to configure the Icinga Agent TLS cipher list setting by new argument `-CipherList`
3535
* [#637](https://github.com/Icinga/icinga-powershell-framework/pull/637) Adds new base handling for future data providers with first metrics for `CPU` information
36+
* [#638](https://github.com/Icinga/icinga-powershell-framework/pull/638) Adds option for formatted, colored console prints with `Write-ColoredOutput`
3637
* [#640](https://github.com/Icinga/icinga-powershell-framework/issues/640) Adds support to set the flag `-NoSSLValidation` for Cmdlets `icinga` and `Install-Icinga`, to ignore errors on self-signed certificates within the environment
3738
* [#643](https://github.com/Icinga/icinga-powershell-framework/pull/643) Adds support for `-RebuildCache` flag on `icinga` cmd to rebuild component cache as well
3839
* [#644](https://github.com/Icinga/icinga-powershell-framework/pull/644) Adds progress bar output to repository interaction (sync, update, new) instead of plain text output
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<#
2+
.SYNOPSIS
3+
Colored console output
4+
5+
.DESCRIPTION
6+
Print colored text on the console.
7+
To specify the color you have to use color tags
8+
9+
.PARAMETER Text
10+
The text to print
11+
12+
.EXAMPLE
13+
Write-ColoredOutput -Text "uncolored text<blue>blue text</>uncolored text<green>green text</>"
14+
#>
15+
function Write-ColoredOutput() {
16+
param (
17+
[string]$Text = ''
18+
);
19+
20+
<#
21+
Define opening and closing tag and build regex
22+
Example <red>text</>
23+
#>
24+
$colorTagOpen = '<';
25+
$colorTagClose = '>';
26+
$regexEndTag = '</>';
27+
$regexStartTag = [string]::Format('{0}[A-Za-z]+{1}', $colorTagOpen, $colorTagClose);
28+
$textParts = [regex]::Split($Text, "($regexStartTag.*?$regexEndTag)");
29+
30+
31+
# Loop over all parts
32+
foreach ($part in $textParts) {
33+
34+
# Check if current part is color tagged
35+
if ($part -match "^$regexStartTag.*?$regexEndTag$") {
36+
37+
# Get color tag
38+
$colorTag = [regex]::Matches($cuttedPart, $regexStartTag).Value;
39+
40+
# Get color out of color tag
41+
$color = $colorTag.substring($colorTagOpen.Length, $colorTag.Length - ($colorTagOpen.Length + $colorTagClose.Length));
42+
43+
# Cut opening tag
44+
$finalPart = $cuttedPart.substring($colorTag.Length, $cuttedPart.length - $colorTag.Length);
45+
46+
# Cut closing tag
47+
$cuttedPart = $part.substring(0, $part.Length - $regexEndTag.Length);
48+
49+
<#
50+
Try colored printing. If color does not exist,
51+
catch runtime error and simply print normal
52+
#>
53+
try {
54+
Write-Host -NoNewline -ForegroundColor $color $finalPart;
55+
} catch {
56+
Write-Host -NoNewline $part;
57+
}
58+
59+
continue;
60+
}
61+
62+
# Print non tagged, uncolored part
63+
Write-Host -NoNewline $part;
64+
}
65+
}

0 commit comments

Comments
 (0)