diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index b206469..ee12f5e 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -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 diff --git a/lib/core/tools/Write-ColoredOutput.psm1 b/lib/core/tools/Write-ColoredOutput.psm1 new file mode 100644 index 0000000..ab78229 --- /dev/null +++ b/lib/core/tools/Write-ColoredOutput.psm1 @@ -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 textblue textuncolored textgreen text" +#> +function Write-ColoredOutput() { + param ( + [string]$Text = '' + ); + + <# + Define opening and closing tag and build regex + Example 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; + } +}