gpt4 book ai didi

powershell - 通过 powershell 在 jenkins 中使用 AnsiColor

转载 作者:行者123 更新时间:2023-12-04 17:44:44 26 4
gpt4 key购买 nike

关于如何使用 powershell 在 Jenkins 上为我的输出着色的任何想法?我已经在我的 Jenkins 上安装了 AnsiColor 插件,并且我已经将工作设置为使用 AnsiColor。唯一的问题是如何让我的 powershell 在 Jenkins 上输出颜色。

最佳答案

好吧,我以前从未使用过它,所以我想我会尝试一下。基本上你只是在字面上放一个转义字符(ASCII 27),然后是一个左括号 [然后是代码,as described on this page , 直接插入字符串。

为了使这更容易,我编写了一个格式化字符串的函数:

function Format-AnsiColor {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[AllowEmptyString()]
[String]
$Message ,

[Parameter()]
[ValidateSet(
'normal display'
,'bold'
,'underline (mono only)'
,'blink on'
,'reverse video on'
,'nondisplayed (invisible)'
)]
[Alias('attribute')]
[String]
$Style ,

[Parameter()]
[ValidateSet(
'black'
,'red'
,'green'
,'yellow'
,'blue'
,'magenta'
,'cyan'
,'white'
)]
[Alias('fg')]
[String]
$ForegroundColor ,

[Parameter()]
[ValidateSet(
'black'
,'red'
,'green'
,'yellow'
,'blue'
,'magenta'
,'cyan'
,'white'
)]
[Alias('bg')]
[String]
$BackgroundColor
)

Begin {
$e = [char]27

$attrib = @{
'normal display' = 0
'bold' = 1
'underline (mono only)' = 4
'blink on' = 5
'reverse video on' = 7
'nondisplayed (invisible)' = 8
}

$fore = @{
black = 30
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
white = 37
}

$back = @{
black = 40
red = 41
green = 42
yellow = 43
blue = 44
magenta = 45
cyan = 46
white = 47
}
}

Process {
$formats = @()
if ($Style) {
$formats += $attrib[$Style]
}
if ($ForegroundColor) {
$formats += $fore[$ForegroundColor]
}
if ($BackgroundColor) {
$formats += $back[$BackgroundColor]
}
if ($formats) {
$formatter = "$e[$($formats -join ';')m"
}

"$formatter$_"
}
}

用法:
Format-AnsiColor -Message 'Hey there' -Style Bold -ForegroundColor Red

'Hello' | Format-AnsiColor -BackgroundColor Green

'One','Two','Three' | Format-AnsiColor -Style 'normal display' -ForegroundColor White -BackgroundColor Black

请记住,如果您不再需要它,您必须关闭序列(我的意思是将样式和颜色设置回之前的任何内容)。

关于powershell - 通过 powershell 在 jenkins 中使用 AnsiColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39296240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com