Groups > Microsoft > Windows PowerShell > Re: Colored Text and Pipelines




Colored Text and Pipelines

Colored Text and Pipelines
Tue, 15 Apr 2008 12:09:45 -070
All,

I am writing a script that will parse log files for remote backup
jobs.  As it stands right now, if the script is invoked with an -html
flag, the output is an .html file that encloses particular log events
- errors, non 0 exit codes, warnings - in <font color=whatever> </
font> tags.  I want it to output text to console if the -html flag is
not present.  As the logs usually span multiple windows, I would
ideally like to color code the text as in the -html version, and also
pipe the output to more, so that I can look at it a screen at a time.
It seems that I can have either color or 'more' functionality.  I
started with Write-host, however the pipeline is not an option there.
Now I am mucking around with $host.ui methods and have run into a
couple problems.

Currently my display function is as such:

Function display-chunk ($chunk) {
     $fcolor = ""
     $chunk | foreach-object {
          if ($_ -match "###") { $fcolor = "Blue" }
          elseif ($_ -match "Error:") { $fcolor = "Red"}
          elseif ($_ -match "exit code [^0]") {$fcolor =
"Red" }
          elseif ($_ -match "Warning:") { $fcolor = "Green"
}
          else {$fcolor = $host.ui.rawui.foregroundcolor };
          $host.ui.Write($fcolor,
$host.ui.rawui.backgroundcolor,"$_`n")
     }
#     $host.ui.rawui.foregroundcolor = $origFG
}

This colors the text as desired, but when I call 'display-chunk
sometext.txt' it will not pipe.  Previously, for each of my
if...elseif calls I did a scriptblock of:

{ $host.ui.rawui.foregroundcolor = "desired Color" ; $_ ;
$host.ui.rawui.foregroundcolor = "Original Color" }

This method would pipe to more, but would not color text.

What am I missing here?  Or is what I am after simply not possible
right now?

Any help would be most appreciated.

Thanks,

Post Reply
Re: Colored Text and Pipelines
Tue, 15 Apr 2008 12:54:07 -070
What you're trying to do simply isn't possible (using more, but stick
with me a sec)

Calling $Host.UI.Write is essentially the same as calling Write-Host
-- both of them _immediately_ output text to the host (ie: the
Console).

Setting the Host's default colors is likewise an _immediate_ change.
The only text that will be colored is something that gets written out
to the host while the default color is set.  If the output is not
printed while the color is still set (because it's being collected and
cached by "more") then you won't see any difference.

This is what we've all been complaining about since day one (do a
search for colored dir output and you'll find articles going back to
the first pre-release bits) -- Because the "stuff" being piped around
are OBJECTS, not text, the only place you could possibly insert color
is ... at the VERY LAST STEP when the object is suddenly converted to
text to be printed on the console.  Of course, none of the format-*
cmdlets support color, so you're basically out of luck unless you
start implementing your own (which noone has dared).

A possible workaround for you would be to have a paging script BEFORE
your coloring code ...


function pagedout{
  BEGIN {
     $x = 0
     $bg = $Host.UI.RawUI.BackgroundColor;
     $fg = $Host.UI.RawUI.ForegroundColor;
  }
  PROCESS{
     # put whatever conditions and colors you like...
     if($_.ToString()[0] -lt "M") {
        $Host.Ui.RawUI.BackgroundColor = "DarkRed"
     } else {
        $Host.UI.RawUI.BackgroundColor = "DarkBlue"
     }
     # The problem is, you're paging by a count of objects, not by how
many lines of text they'll output
     if(($x++ % 25) -eq 0) {
        Write-Host "Press a key...";
        $Host.UI.RawUI.FlushInputBuffer();
        $null = $Host.UI.RawUI.ReadKey();
     }
     $_
   }
   END {
     # Don't forget to set the colors back...
     $Host.UI.RawUI.BackgroundColor = $bg;
     $Host.UI.RawUI.ForegroundColor = $fg;
   }
}

ls | pagedout


--
Post Reply
Re: Colored Text and Pipelines
Tue, 15 Apr 2008 13:30:22 -070
Joel,

Thank you very much.  I had cribbed some information from the other
text coloring/piping posts in this group.

I very much appreciate your thoughtful and informative explanation.

I had feared this may be the case, so I may cheat and just use an
accumulator and get-input construct for a poor man's pager.

Many thanks,

Post Reply
about | contact