0

I'm using Write-Color function to write colored messages an I would like to align the status

$file1 = "myFirstFileName.json"
$file2 = "myFile.json"
Function Write-Color([String[]]$Text, [ConsoleColor[]]$ForeGroundColor, [ConsoleColor[]]$BackGroundColor)
{
    for ($i = 0; $i -lt $Text.Length; $i++) {
        $Color = @{ }
        if ($ForeGroundColor -and $BackGroundColor)
        {
            $Color = @{
                ForegroundColor = $ForeGroundColor[$i%($ForeGroundColor.count)]
                BackgroundColor = $BackGroundColor[$i%($BackGroundColor.count)]
            }
        }
        elseif ($ForeGroundColor)
        {
            $Color = @{
                ForegroundColor = $ForeGroundColor[$i%($ForeGroundColor.count)]
            }
        }
        elseif ($BackGroundColor)
        {
            $Color = @{
                BackgroundColor = $BackGroundColor[$i%($BackGroundColor.count)]
            }
        }
        Write-Host $Text[$i] @Color -NoNewLine
    }
    Write-Host
}

Write-Color 'File ', $file1, ' is open'.PadRight(48), '[', ' ERROR! ', ']' -ForeGround Cyan, Yellow, Cyan, White, Red, White
Write-Color 'File ', $file2, ' is open'.PadRight(48), '[', ' ERROR! ', ']' -ForeGround Cyan, Yellow, Cyan, White, Red, White

Result

File myFirstFileName.json is open                                        [ ERROR! ]

File myFile.json is open                                      [ ERROR! ]

 

Expected Result

File myFirstFileName.json is open                                           [ ERROR! ]

File myFile.json is open                                                           [ ERROR! ]

Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
samanosuke
  • 13
  • 3
  • 1
    take a look at the `-f` string format operator. it may prove easier to manage. you can use it to build strings that have left/right padding. then feed the strings to your custom write function. something like this >>> `'This is a test {0,23}' -f 'TwentyThree'` <<< would give you a string that has the last word at the far right of a 23 character section. – Lee_Dailey Oct 23 '19 at 13:06
  • Consider use of function `Write-HostColored` from https://stackoverflow.com/a/46046113/45375, which allows you embed coloring instructions directly into your strings. – mklement0 Oct 23 '19 at 14:33

2 Answers2

0

You need to use double quotes and combine the first set of strings into one string. Other wise you are only padding the last string you will get different results. You'll need to fix your colors some. Not sure how you'd make the filename a different color in this scenario.

Write-Color "File $file1 is open".PadRight(48), '[', ' ERROR! ', ']' -ForeGround Cyan, White, Red, White
Write-Color "File $file2 is open".PadRight(48), '[', ' ERROR! ', ']' -ForeGround Cyan, White, Red, White
Avshalom
  • 8,657
  • 1
  • 25
  • 43
Scott Heath
  • 830
  • 7
  • 5
0

You're currently padding the strings ' is open', which are of the same length.

You'll want to call PadRight() on the string after you've added your variable length substring:

Write-Color "File '$file1' is open".PadRight(48), '[', ' ERROR! ', ']'
Write-Color "File '$file2' is open".PadRight(48), '[', ' ERROR! ', ']'
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • If I do this, the entire string will be printed with the same color, I want to print the filename with a different color. – samanosuke Oct 23 '19 at 11:58