http://www.perlmonks.org?node_id=221475

icius has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise ones,

I have a simple goal of being able to color highlight certain lines of output matched by a regex when using the tail -f command. For example:

If the word "ERROR" appears in the log line, color the whole line red on output

So I want this nice little command window merrily spewing out lines from logfiles and color highlighting certain lines with certain text.

I am also interested in any other tricks to make command line programs look cooler such as:

A rotating star using the characters \ | / - (seen on some unix install tools)

or

Progress bars (as seen on secure copy commands)

How is this kind of dynamic in place line output achieved?

I am on Windows XP Pro with ActivePerl 5.8

Adam
  • Comment on Stupid console tricks (or color highlighted tail)

Replies are listed 'Best First'.
Re: Stupid console tricks (or color highlighted tail)
by Callum (Chaplain) on Dec 20, 2002 at 17:38 UTC
    Take a look at the modules Win32::Console and Win32::ANSIConsole these will enable you to control most aspects of the console, including several elements of what you're seeking.
Stupid console tricks (progress bars)
by Mr. Muskrat (Canon) on Dec 20, 2002 at 18:11 UTC

    Many people here have written some form of a progress bar. There is even Term::ProgressBar on CPAN.

    Super Search for "progress bar". Don't want to weed through the overwhelming number of posts? Check out Progress Bar for a good place to start.

Stupid console tricks (rotating star)
by Mr. Muskrat (Canon) on Dec 20, 2002 at 17:57 UTC

    I hope you enjoy using this little snippet as much as I enjoyed writing it!

    #!/usr/bin/perl use strict; use warnings; my $reps = 100; my $delay = 100; # in milliseconds $delay/=1000; my @star = qw(| / - \\); for(1..$reps) { for(@star) { print "\r$_"; select(undef, undef, undef, $delay); } }

Re: Stupid console tricks (or color highlighted tail)
by rob_au (Abbot) on Dec 20, 2002 at 22:26 UTC
    A rotating star using the characters \ | / - (seen on some unix install tools)

    This is actually quite easy to achieve using the Tie::Cycle module ...

    use Tie::Cycle; tie my $whirley, 'Tie::Cycle', [ qw( | / - \ ) ]; # And now for the demonstration ... ++$|; while (1) { print $whirley, "\b"; sleep 1; }

    There a number of other ways to implement such eye-candy discussed in this thread.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000000010"))'

Re: Stupid console tricks (or color highlighted tail)
by John M. Dlugosz (Monsignor) on Dec 20, 2002 at 21:01 UTC
    The file handle going into the Win32 Console doesn't do ANSI terminal controls or anything like that.

    But, it looks like Win32::ANSIConsole is a wrapper to intercept the control codes and call the Console API to do things between sending the output on to the real device handle. It looks like exactly what you need.

    If that's too rough for you, get inspiration from it. Write your own filehandle tie that calls the Win32::Console commands to position the cursor, draw text, change colors, etc.

    Hmm, I wonder why nobody has written something you can pipe into that does this.

    For another idea, I just did some colorized output in a program, but was using a Tk text window for the output, along with some GUI buttons and indicators. You might want to do that, so you can implement your status indicator plus a colored console within a window, rather than using a TTY.

    —John

Re: Stupid console tricks (or color highlighted tail)
by tstock (Curate) on Dec 20, 2002 at 23:42 UTC
    asking for console tips on windows XP... that's funny :)

    To cat a log file and see error lines in red over black, I would could use this on my machine (linux):
    perl -ne 'print /ERROR/ ? "\033[1;31m\033[40m$_\033[0m" : $_ ' my.log
    A cool trick I use to follow a log file is use 'less my.log', /searchstring, SHIFT+f

    now not only can I jump between a less and a tail -f, but also the searchstring is highlighted everytime it shows up.

    tstock

    update:
    silly rotating star:
    perl -e 'print "\b" . substr("|/-\\", time % 4, 1 ) while 1'