Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Adds option for formatted, colored console prints #638

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#633](https://github.com/Icinga/icinga-powershell-framework/pull/633) Adds support for Icinga 2.14.0 native Icinga for Windows API communication
* [#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`
* [#637](https://github.com/Icinga/icinga-powershell-framework/pull/637) Adds new base handling for future data providers with first metrics for `CPU` information
* [#638](https://github.com/Icinga/icinga-powershell-framework/pull/638) Adds option for formatted, colored console prints with `Write-ColoredOutput`
* [#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
* [#643](https://github.com/Icinga/icinga-powershell-framework/pull/643) Adds support for `-RebuildCache` flag on `icinga` cmd to rebuild component cache as well
* [#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
Expand Down
65 changes: 65 additions & 0 deletions lib/core/tools/Write-ColoredOutput.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<#
.SYNOPSIS
Colored console output

.DESCRIPTION
Print colored text on the console.
To specify the color you have to use color tags

.PARAMETER Text
The text to print

.EXAMPLE
Write-ColoredOutput -Text "uncolored text<blue>blue text</>uncolored text<green>green text</>"
#>
function Write-ColoredOutput() {
param (
[string]$Text = ''
);

<#
Define opening and closing tag and build regex
Example <red>text</>
#>
$colorTagOpen = '<';
$colorTagClose = '>';
$regexEndTag = '</>';
$regexStartTag = [string]::Format('{0}[A-Za-z]+{1}', $colorTagOpen, $colorTagClose);
$textParts = [regex]::Split($Text, "($regexStartTag.*?$regexEndTag)");


# Loop over all parts
foreach ($part in $textParts) {

# Check if current part is color tagged
if ($part -match "^$regexStartTag.*?$regexEndTag$") {

# Get color tag
$colorTag = [regex]::Matches($cuttedPart, $regexStartTag).Value;

# Get color out of color tag
$color = $colorTag.substring($colorTagOpen.Length, $colorTag.Length - ($colorTagOpen.Length + $colorTagClose.Length));

# Cut opening tag
$finalPart = $cuttedPart.substring($colorTag.Length, $cuttedPart.length - $colorTag.Length);

# Cut closing tag
$cuttedPart = $part.substring(0, $part.Length - $regexEndTag.Length);

<#
Try colored printing. If color does not exist,
catch runtime error and simply print normal
#>
try {
Write-Host -NoNewline -ForegroundColor $color $finalPart;
} catch {
Write-Host -NoNewline $part;
}

continue;
}

# Print non tagged, uncolored part
Write-Host -NoNewline $part;
}
}