Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > ANSI colors and the space they take.

Reply
Thread Tools

ANSI colors and the space they take.

 
 
Sandman
Guest
Posts: n/a
 
      12-31-2003
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?
 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      12-31-2003
On Wed, 31 Dec 2003 11:35:48 +0100
Sandman <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      01-01-2004
On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <(E-Mail Removed)> 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__
 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      01-01-2004
In article <(E-Mail Removed)>,
Michele Dondi <(E-Mail Removed)> wrote:

> On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      01-01-2004
Sandman <(E-Mail Removed)> wrote:

: In article <(E-Mail Removed)>,
: Michele Dondi <(E-Mail Removed)> wrote:
:
: > On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <(E-Mail Removed)> 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!";

 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      01-01-2004
On Thu, 01 Jan 2004 16:32:38 +0100, Sandman wrote:

> In article <(E-Mail Removed)>,
> Michele Dondi <(E-Mail Removed)> wrote:
>
>> On Wed, 31 Dec 2003 11:35:48 +0100, Sandman <(E-Mail Removed)> 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

 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      01-01-2004
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) (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]
 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      01-01-2004
In article <(E-Mail Removed) t>,
James Willmore <(E-Mail Removed)> 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]
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      01-04-2004
On Fri, 02 Jan 2004 00:49:30 +0100, Sandman <(E-Mail Removed)> 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 <(E-Mail Removed)> 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__
 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      01-07-2004
In article <(E-Mail Removed)>,
Michele Dondi <(E-Mail Removed)> 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]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pre-ansi to ansi c++ conversion? Frank Iannarilli C++ 2 07-21-2009 11:05 PM
Is there any library that can convert RGB colors to ANSI colors? ZelluX Python 3 12-01-2008 11:08 AM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C Programming 11 04-28-2008 03:00 AM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C++ 1 04-24-2008 06:44 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments