amateurjustin amateurjustin - 1 year ago 183
Perl Question

Using Perl Ansi color to color entire screen

I'm using Term::ANSIColor along with Win32::Console::ANSI in Windows.
I often use colored printing in my scripts to highlight some key info or whatever.

For the first time though, I'v tried something that works, but not quite how I would want it to.

What it looks like when I print on grey/white background:

What it looks like

And what I want is (edited to look the way I want it to print):

What it should look like

Thus I'm wondering if anyone knows how to go about ANSI escape sequences. Ho do I fill the entire piece that I'm printing on with a color and not just to the newline.

Also, here is my script (two ways I tried):

print color('bold blue on_white'), "\tnr\tisiNdebele\n";
print colored ['bold blue on_white'], "\tnso\tSepedi\n";

This isn't a major issue. I'm just wondering if someone knows how I can achieve this.

Answer Source

My solution uses Term::Size::Any to figure out how big the terminal is, and Text::Tabs to convert the \ts into spaces. We need to do that because a tabulator is only one character wide, but gets converted to 4, or 8 or whatever number of spaces when printed out.

use strict;
use warnings;
use Term::Size::Any 'chars';
use Term::ANSIColor;
use Text::Tabs 'expand';

my ($columns, $rows) = chars;

sub full_background {
    my ($text) = @_;

    chomp $text;
    return sprintf(qq{%-${columns}s}, expand($text)) . "\n";

print color('bold blue on_white'), full_background( "\tnr\tisiNdebele\n" );
print colored ['bold blue on_red'], full_background( "\tnso\tSepedi\n" );

Here's what it looks like.

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download