Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   ANSI colors and the space they take. (http://www.velocityreviews.com/forums/t884411-ansi-colors-and-the-space-they-take.html)

Sandman 12-31-2003 10:35 AM

ANSI colors and the space they take.
 
I like ansi colors, but I've run in to this problem:

printf "\e[32;01m%-14s\e[00;00m \e[37;01m%-30s\e[00;00m\n", "Hello", "World!";
printf "%-14s %-30s\n", "Hello", "World!";
printf "%-14s %-30s\n", "\e[32;01mHello\e[00;00m", "\e[37;01mWorld!\e[00;00m";

This results in:

Hello World!
Hello World!
Hello World!

(albeit color-coded)

In the last example, the length of the actual ansi code seems to be taken into
consideration for printf - is there a way to elaviate this?

James Willmore 12-31-2003 01:32 PM

Re: ANSI colors and the space they take.
 
On Wed, 31 Dec 2003 11:35:48 +0100
Sandman <mr@sandman.net> wrote:

> I like ansi colors, but I've run in to this problem:
>
> printf "\e[32;01m%-14s\e[00;00m \e[37;01m%-30s\e[00;00m\n", "Hello",
> "World!"; printf "%-14s %-30s\n", "Hello", "World!";
> printf "%-14s %-30s\n", "\e[32;01mHello\e[00;00m",
> "\e[37;01mWorld!\e[00;00m";
>
> This results in:
>
> Hello World!
> Hello World!
> Hello World!
>
> (albeit color-coded)
>
> In the last example, the length of the actual ansi code seems to be
> taken into consideration for printf - is there a way to elaviate
> this?


You could save yourself the headaches and use the Term::ANSIColor
module :-) It *may* be what you want.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Time is nature's way of making sure that everything doesn't
happen at once.

Michele Dondi 01-01-2004 02:14 PM

Re: ANSI colors and the space they take.
 
On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <mr@sandman.net> wrote:

>I like ansi colors, but I've run in to this problem:

[snip]
>In the last example, the length of the actual ansi code seems to be taken into
>consideration for printf - is there a way to elaviate this?


perldoc -q color
perldoc Term::ANSIColor


Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__

Sandman 01-01-2004 03:32 PM

Re: ANSI colors and the space they take.
 
In article <dm38vv4g3lncudomnqi3mf91hcbdbulbbm@4ax.com>,
Michele Dondi <bik.mido@tiscalinet.it> wrote:

> On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <mr@sandman.net> wrote:
>
>> I like ansi colors, but I've run in to this problem:

> [snip] In the last example, the length of the actual ansi code seems
> to be taken into consideration for printf - is there a way to elaviate
> this?
>
> perldoc -q color
> perldoc Term::ANSIColor


Thanks, but that didn't help much:

#!/usr/bin/perl

use Term::ANSIColor;
printf "%-14s %-30s\n", "Hello", "World!";
printf "%-14s %-30s\n", "\e[32;01mHello\e[00;00m", "\e[37;01mWorld!\e[00;00m";
printf "%-14s %-30s\n", color("red") . "Hello" . color("reset"), color("green") . "World!" . color("reset");
printf "%-14s %-30s\n", colored("Hello", "red"), colored("World!", "green");

use Term::ANSIColor qw(:constants);
printf "%-14s %-30s\n", RED . "Hello" . RESET, GREEN . "World!" . RESET;

<output>
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
</output>

Seems to me that color(), colored() or the constants eat up as much space as the escape codes.

Jay Tilton 01-01-2004 03:39 PM

Re: ANSI colors and the space they take.
 
Sandman <mr@sandman.net> wrote:

: In article <dm38vv4g3lncudomnqi3mf91hcbdbulbbm@4ax.com>,
: Michele Dondi <bik.mido@tiscalinet.it> wrote:
:
: > On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <mr@sandman.net> wrote:
: >
: >> I like ansi colors, but I've run in to this problem:
: > [snip] In the last example, the length of the actual ansi code seems
: > to be taken into consideration for printf - is there a way to elaviate
: > this?
: >
: > perldoc -q color
: > perldoc Term::ANSIColor
:
: Thanks, but that didn't help much:
:
: #!/usr/bin/perl
:
: use Term::ANSIColor;
: printf "%-14s %-30s\n", "Hello", "World!";
: printf "%-14s %-30s\n", "\e[32;01mHello\e[00;00m", "\e[37;01mWorld!\e[00;00m";
: printf "%-14s %-30s\n", color("red") . "Hello" . color("reset"), color("green") . "World!" . color("reset");
: printf "%-14s %-30s\n", colored("Hello", "red"), colored("World!", "green");
:
: use Term::ANSIColor qw(:constants);
: printf "%-14s %-30s\n", RED . "Hello" . RESET, GREEN . "World!" . RESET;
:
: <output>
: Hello World!
: Hello World!
: Hello World!
: Hello World!
: Hello World!
: </output>
:
: Seems to me that color(), colored() or the constants eat up as much
: space as the escape codes.

That's true, but you can put the ANSI escape sequences into printf's
formatting string, much as you did in the first example in your original
article:

printf "\e[32;01m%-14s\e[00;00m \e[37;01m%-30s\e[00;00m\n",
"Hello", "World!";

Using the Term::ANSIColor subroutines, that might go like:

use Term::ANSIColor;
my $fmt = colored('%-14s', 'bold green on_black') . ' '
. colored('%-30s', 'bold white on_black') . "\n";
printf $fmt, "Hello", "World!";


James Willmore 01-01-2004 03:46 PM

Re: ANSI colors and the space they take.
 
On Thu, 01 Jan 2004 16:32:38 +0100, Sandman wrote:

> In article <dm38vv4g3lncudomnqi3mf91hcbdbulbbm@4ax.com>,
> Michele Dondi <bik.mido@tiscalinet.it> wrote:
>
>> On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <mr@sandman.net> wrote:


<snip>

> <output>
> Hello World!
> Hello World!
> Hello World!
> Hello World!
> Hello World!
> </output>
>
> Seems to me that color(), colored() or the constants eat up as much
> space as the escape codes.


Maybe you're looking for something like this (?) .............

----------------------------------------------------------------
#!/usr/bin/perl

use Term::ANSIColor qw(:constants);

my $hello = sprintf "%14s", RED. "Hello".RESET;
my $world = sprintf "%30s", GREEN. "World".RESET;

print "$hello $world\n";
----------------------------------------------------------------

<output>
jim@maxine:~> perl news11.pl
Hello World
jim@maxine:~>
</output>

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
'I believe in getting into hot water; it keeps you clean." --
G. K. Chesterton


Sandman 01-01-2004 11:49 PM

Re: ANSI colors and the space they take.
 
In article <3ff43e0e.45690760@news.erols.com>, tiltonj@erols.com (Jay Tilton)
wrote:

> : Seems to me that color(), colored() or the constants eat up as much
> : space as the escape codes.
>
> That's true, but you can put the ANSI escape sequences into printf's
> formatting string, much as you did in the first example in your original
> article:
>
> printf "\e[32;01m%-14s\e[00;00m \e[37;01m%-30s\e[00;00m\n",
> "Hello", "World!";
>
> Using the Term::ANSIColor subroutines, that might go like:
>
> use Term::ANSIColor;
> my $fmt = colored('%-14s', 'bold green on_black') . ' '
> . colored('%-30s', 'bold white on_black') . "\n";
> printf $fmt, "Hello", "World!";
>


I realize this, of course. BUt I would very much like to keep the formatting
seperate from the printf's. It may not be critical for the application I am
writing now, but I know some script I will try to "ansify" that will not accept
that method.

--
Sandman[.net]

Sandman 01-01-2004 11:53 PM

Re: ANSI colors and the space they take.
 
In article <pan.2004.01.01.15.46.08.282975@remove.adelphia.ne t>,
James Willmore <jwillmore@remove.adelphia.net> wrote:

> > <output>
> > Hello World!
> > Hello World!
> > Hello World!
> > Hello World!
> > Hello World!
> > </output>
> >
> > Seems to me that color(), colored() or the constants eat up as much
> > space as the escape codes.

>
> Maybe you're looking for something like this (?) .............
>
> ----------------------------------------------------------------
> #!/usr/bin/perl
>
> use Term::ANSIColor qw(:constants);
>
> my $hello = sprintf "%14s", RED. "Hello".RESET;
> my $world = sprintf "%30s", GREEN. "World".RESET;
>
> print "$hello $world\n";
> ----------------------------------------------------------------
>
> <output>
> jim@maxine:~> perl news11.pl
> Hello World
> jim@maxine:~>
> </output>


Well, thanks for the input. I can think of many ways to bypass the problematic
result, and it may have been unwise of me not to state from the beginning that
the color-formatting of the strings should be kept seperate from the print
formatting, as well as the printf structure needs to be kept in the printf,
especially in a particular application I've got.

--
Sandman[.net]

Michele Dondi 01-04-2004 02:58 PM

Re: ANSI colors and the space they take.
 
On Fri, 02 Jan 2004 00:49:30 +0100, Sandman <mr@sandman.net> wrote:

>> That's true, but you can put the ANSI escape sequences into printf's
>> formatting string, much as you did in the first example in your original
>> article:

[snip]
>I realize this, of course. BUt I would very much like to keep the formatting
>seperate from the printf's. It may not be critical for the application I am
>writing now, but I know some script I will try to "ansify" that will not accept
>that method.



On Fri, 02 Jan 2004 00:53:41 +0100, Sandman <mr@sandman.net> wrote:

>> Maybe you're looking for something like this (?) .............

[snip]
>Well, thanks for the input. I can think of many ways to bypass the problematic
>result, and it may have been unwise of me not to state from the beginning that
>the color-formatting of the strings should be kept seperate from the print
>formatting, as well as the printf structure needs to be kept in the printf,
>especially in a particular application I've got.


I'm still not sure I fully understand what your problem is. I
suggested, as others did, Term::ANSIColor because it will help you
writing cleaner code that IMHO will be easier to maintain too.

I am still under the impression that you may/should choose a simpler
logic rather than fiddling with this approach. However, if you really
want to "keep the printf() formatting and the color formatting"
separate, an idea that comes to mind is:

format your strings with sprintf(), then use s///ge to suitably
"highlight" some "words". For example,


#!/usr/bin/perl -l

use strict;
use warnings;
use Term::ANSIColor qw/:constants/;

$_=sprintf '%-14s %-30s', qw/Hello World/;
print;
my @colors=(GREEN, RED);
my $i=0;
s/\b\w+\b/$colors[$i++] . $& . RESET/ge;
print;

__END__


Of course this is a very naive example and things may get definitely
more complicated in a real application; I think it all depends on how
"regular" you strings are, though.


Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__

Sandman 01-07-2004 08:33 PM

Re: ANSI colors and the space they take.
 
In article <17agvvcojoat0moikfgolqa5h67brjrsbj@4ax.com>,
Michele Dondi <bik.mido@tiscalinet.it> wrote:

> >Well, thanks for the input. I can think of many ways to bypass the
> >problematic
> >result, and it may have been unwise of me not to state from the beginning
> >that
> >the color-formatting of the strings should be kept seperate from the print
> >formatting, as well as the printf structure needs to be kept in the printf,
> >especially in a particular application I've got.

>
> I'm still not sure I fully understand what your problem is. I
> suggested, as others did, Term::ANSIColor because it will help you
> writing cleaner code that IMHO will be easier to maintain too.



I appreciate the suggestions, but my problem is that I have an output routine,
which I use when I want to output something to STDOUT from my script(s). Now, I
wanteed to colorenable that output. This is a simplified version:

--
#!/usr/bin/perl

&output("Warning", "Hello World!");

sub output {
printf "%-14s %-30s\n", $_[0], $_[1];
}
--

Now, $_[0] is the "type" of output, so I'll be able to distinguish it from the
rest. Problem is, that I can't anticipate, globally, every kind of type. And
even so, I'm not sure I like to have the entire row in one color (i.e. a
"warning" => "red" hash and then fetch the colors from that, in the sub).

So, what I wanted to do was this (now using Term::Ansi):

--
#!/usr/bin/perl
use Term::ANSIColor;
&output(colored("Warning", "red"), colored("Hello World!", "green"));

sub output {
printf "%-14s %-30s\n", $_[0], $_[1];
}
--

My problem here is that the above results in a messy printf. In short, the
colorcoding should be passed on to the sub routine without adding arguments to
it (i.e. not &output(RED, "Warning", GREEN, "Hello world");) since I'm using it
in so many places that I don't want to change. This is also why I'll probably
colorencode some "well known" messages types, so even output from older scripts
will have color.

There is one thing I can do, and that's to check if the incoming arguments have
colorcoding, allthough I'm unsure on how to do that (=~m/e[/ doesn't yield a
match, regardless if I type the ansi code or use Term::Ansi) and alter the
value of "%-14s", but that strikes me as a poor solution.

--
Sandman[.net]


All times are GMT. The time now is 04:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.