Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm using Term::ANSIColor to apply colors and styles (boldface, underline) to ASCII text strings, which I then want to wrap to a certain width for printing to the terminal. I found Text::ANSI::Util qw/ta_wrap/ which purports to do this properly for strings with these escape sequences, but I'm encountering what appear to be bugs. On the other hand, am I just not using the module correctly?

Here's a test program that illustrates some problems. Since I can't show the colors or styling I'll try to add some commentary to the output.

#!/usr/bin/env perl use 5.010; use warnings; use strict; use Term::ANSIColor qw(color :constants); use Text::ANSI::Util qw/ta_wrap/; sub show { my $line = shift; my $wrapped = ta_wrap($line, 30, {flindent => q[ ] x 5, slindent => q[ ] x 5 } ); say "BEFORE:"; say $line; # print unpack('H*', $_) . ' ' for split //, $line; say ''; say "AFTER:"; say $wrapped; # print unpack('H*', $_) . ' ' for split //, $wrapped; say "\n"; } show "Marley was " . BOLD . color('green') . "dead, " . RESET . "to be +gin with."; show "Marley was " . color('green') . "dead, " . RESET . "to be +gin with."; show "Marley was " . BOLD . "dead, " . RESET . "to be +gin with."; show "Marley was " . UNDERLINE . color('green') . "dead, to begin with +. " . RESET . "There is no doubt..."; show "Marley was " . UNDERLINE . "dead, to begin with +. " . RESET . "There is no doubt...";
Output:
BEFORE: Marley was dead, to begin with. AFTER: ## 'dead,' is bold and green but we lost the space preceding it + ## Marley wasdead, to begin with. BEFORE: Marley was dead, to begin with. AFTER: ## 'dead,' is green. This is correctly rendered. ## Marley was dead, to begin with. BEFORE: Marley was dead, to begin with. AFTER: ## 'dead,' is bold. This is correctly rendered. ## Marley was dead, to begin with. BEFORE: Marley was dead, to begin with. There is no doubt... AFTER: ## 'dead, to begin with. ' is underlined/green but the precedin +g space was lost, and underlining between "begin" and "with" persiste +d across the margin ## Marley wasdead, to begin with. There is no doubt... BEFORE: Marley was dead, to begin with. There is no doubt... AFTER: ## Underlining only, but again the margin between "begin" and " +with" is underlined. ## Marley was dead, to begin with. There is no doubt...

In reply to Text::ANSI::Util for wrapping "colorful" text by ibm1620

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-12-05 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?