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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.