Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Colored Text and Pipelines

Closed Thread
 
Thread Tools Display Modes
Old 04-15-2008   #1 (permalink)
NilVeritas
Guest


 

Colored Text and Pipelines

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,

Jonathan
Old 04-15-2008   #2 (permalink)
Joel \Jaykul\ Bennett
Guest


 

Re: Colored Text and Pipelines

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


--
Joel "Jaykul" Bennett
Old 04-15-2008   #3 (permalink)
NilVeritas
Guest


 

Re: Colored Text and Pipelines

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,

Jonathan
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you comment long pipelines RickB PowerShell 9 4 Weeks Ago 10:41 PM
Steppable Pipelines in Powershell 2.0 CTP Tao Ma PowerShell 6 11-13-2007 01:25 AM
Colored Folders DavidH Vista General 2 05-27-2007 06:01 PM
ctrl-c affects background pipelines Adam Milazzo PowerShell 2 09-20-2006 06:06 PM
Colored ls output & zsh-like autocompletion Andrey Balaguta PowerShell 1 08-28-2006 04:18 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50