2016-09-02 7 views
2

ジェンキンスでpowershellを使用して自分の出力を色づける方法はありますか?私はすでにJenkinsにAnsiColorプラグインをインストールしており、AnsiColorを使用するように設定しました。唯一の問題は、私のパワーシェルがジェンキンスの色を出力する方法です。ジェンキンスでPowershell経由でAnsiColorを使用する

答えて

1

私はそれを試してみる前にそれを使用したことはありませんでした。基本的には文字通りエスケープ文字(ASCII 27)とそれに続く左括弧[とそれに続くコードas described on this pageを文字列に直接入力するだけです。あなたはシーケンスをオフにする(そのことで私が設定を意味していることに注意してください

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 

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$_" 
    } 
} 

使用方法:この簡単に、私は、文字列をフォーマット機能を書いたを作るために

スタイルや色を以前のものに戻します)。

+0

これはとても美しいです:)それは動作しますが、私のjenkinのページは私が使用した最後の色を保持しないように白に色を設定する必要がありますか? – fazlook1

+0

@ fazlook1はい。シーケンスは色を設定し、それを元に戻さないので、自分でそれを行う必要があります。その機能をより良く説明するために関数を記述することもできます。 – briantist

+0

私は自分で編集するように最善を尽くします。本当にありがとう、あなたは私の一日を作った。ああ待って......私は自分のIDEからPSを実行する場合、私はこれらを戻す:[42mHello [0; 37; 43mOne [0; 37; 43mTwo [0; 37; 43mThree、どのように私はそれを修正する? – fazlook1

関連する問題