in reply to Wrap while ignoring certain sequences

How 'bout something like the following (granted it can probably be improved upon:
use strict; use warnings; use Text::Wrap; $Text::Wrap::columns = 80; output (q|\e[33mCoruscate\e[0m;\e[37;40m is having trouble with a scri +pt he is working on and therefore makes a trip to \e[32mwww.perlmonks +.org\e[0m;\e[37;40m to seek help|); # Modified print() sub output { my $text = shift; my @removed; my $current_position = 0; while ( $text =~ m!(\\e\[[\d;]+[mK];?)!g ) { push @removed, [pos($text) - length($1), $1]; } $text =~ s!\\e\[[\d;]+[mK];?!!g; my $wrapped = wrap('', '', $text); while ( @removed ) { my $current_element = shift @removed; substr($wrapped,@{$current_element}[0]) = @{$current_element}[1] . substr($wrapped,@{$current_element}[0]); } print $wrapped; }
update:did I mention it could be improved upon. I realized what hv noticed about 20 min after I left for lunch. I admit this code is therefore pretty broken. Thanks hv for the heads up anyhow.

update2: As for tye's solution above WOW!! I tried to fix my mistakes, but after figuring out how his worked I was humbled (need I say more).

-enlil

Replies are listed 'Best First'.
Re: Re: Wrap while ignoring certain sequences
by hv (Prior) on Mar 12, 2003 at 22:32 UTC

    If I understand this approach correctly, you first locate and remove all escape sequences, then wrap the text, and then restore the escape sequences. But after the text has been wrapped, the string has been modified, so you won't necessarily be putting the escape sequences back in the correct places.

    Hugo