mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2026-02-03 04:09:29 -05:00
Feature: Adds option for formatted, colored console prints (#638)
* Adds feature for colored console prints
This commit is contained in:
parent
538750633c
commit
be29d7dbd5
2 changed files with 66 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
65
lib/core/tools/Write-ColoredOutput.psm1
Normal file
65
lib/core/tools/Write-ColoredOutput.psm1
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue