http://qs1969.pair.com?node_id=11143680

ibm1620 has asked for the wisdom of the Perl Monks concerning the following question:

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...