Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^6: Text::ANSI::Util for wrapping "colorful" text

by ibm1620 (Hermit)
on Jul 25, 2022 at 20:34 UTC ( [id://11145725]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Text::ANSI::Util for wrapping "colorful" text
in thread Text::ANSI::Util for wrapping "colorful" text

Sorry to create confusion: those '^'s at the beginning and end are "\e". That makes the difference.

Mac line endings are the same as Linux: 0x0a.

When I tested just now in a standalone program, it took about 5 seconds. Doubling the length of the text between the escape sequences, though, I cancelled after about 30 secs. Remove the escape sequences and it's quick.

Replies are listed 'Best First'.
Re^7: Text::ANSI::Util for wrapping "colorful" text
by tybalt89 (Monsignor) on Jul 30, 2022 at 01:07 UTC

    Try this one...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11143680 use warnings; use 5.010; use Term::ANSIColor qw(color :constants); use List::Util qw( uniq ); sub findwrappoint { my ($string, $width) = @_; my $cut = 0; my $char = 0; while( $string =~ /(\e[[\d;]*m)|([^ ]+)|( +)/g ) { $3 and $char <= $width and $cut = $+[3]; $char += $1 ? 0 : $2 ? length $2 : length $3; $char > $width and return substr( $string, 0, $cut, ''), $string; } return $string, ''; } sub ta_wrap { my $cnt = 0; my ($text, $width, $optionsref) = @_; tr/\n\t/ /, s/ +/ /g for $text; # replace newlines and tabs my %pads = (flindent => '', slindent => '', %{ $optionsref // {} } ) +; $text = $pads{flindent} . $text; my $out = ''; my $carryover = ''; while( length $text ) { my ( $line, $rest ) = findwrappoint( $text, $width ); $out .= "$line\e[m\n"; s/.*\e\[0?m// for $carryover .= join '', $line =~ /\e[[\d;]*m/g; $carryover = join '', uniq $carryover =~ /\e[[\d;]*m/g; $text = $rest; $text =~ /./ and $text = "$pads{slindent}$carryover$text"; } return $out; } sub show { my $line = shift; my $wrapped = ta_wrap($line, 79, {flindent => q[ ] x 5, slindent => q[ ] x 5 } ); say "BEFORE:"; say $line; say "AFTER:"; say $wrapped; } 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') . BOLD . "dead, to beg +in with. " . RESET . "There is no doubt..."; show "Marley was " . UNDERLINE . "dead, to begin with +. " . RESET . "There is no doubt..."; show "Marley was dead, to begin with. There is no doubt..."; show UNDERLINE . "Marley was dead, to begin with. There is no doubt... +" . RESET; show qq(\e[33m01:06:52:18 [CMP] Okay, Well look at the bottom by the f +irst 2 inches without any constant direction. Indeed, at the bottom o +f this wealth of nature. President Barbicane, however, lost not one o +f the Barbados. It is but an atom of carbonic acid, by a moment. We'l +l have further word on the forward OMNI is good for the inhabitants o +ught to be despised. But that day, about eleven o'clock position from + the earth. Besides, even without these conditions, as regards the op +tical instruments at their highest pitch throughout this triumphant m +arch. Michel Ardan hoped to get any crew status report. We're about 8 +8 degrees east, coming up on the earth once more entered the Bay of E +spiritu Santo, opening precisely upon the moon's formation, by means +of which one is located at 7 The other one is right in on any of the +United States of the projectile! The necessary operations for the foo +d bags are so good, who ventures to affirm it. "True," rejoined the m +ajor. "True," replied Barbicane. The problem is a PROCEED and allow t +he hot fire is complete, and I'm proceeding with opening the hatch at + this crisis, as though the samples for that of the journey. Many foo +lish things had been probably 20 minutes to pass to Columbia, we woul +d expect that once this is Houston. We need a change had taken place +in a piteous tone, that is not inhabited; no! the moon was a little h +ard for us in all four tanks and position the antenna tracks through +in the signal for renewed cries of still greater precision, they succ +eeded in rising, drew a thermometer from its center. "Then," said Nic +holl, it is a little scratchy. It looks fairly good size. It has the +choice between two seas, they pretended that it is true; but it will +continue its elliptical crater, and then add UP DATA LINK switch to R +ANGE. We're going to be in opposition. These eclipses, caused by weig +ht; nor a boat, whose stability on the B B shot has hit the surface.\ +e[0m);
      On Windows 10, I had to add the following additional USE statement to display colors:

      use Win32::Console::ANSI;

      "It's not how hard you work, it's how much you get done."

      Haven't had a chance to isolate what it was working on, but FWIW I did get the following at one point:

      sub findwrappoint { my ($string, $width) = @_; my $cut = 0; my $char = 0; while( $string =~ /(\e[[\d;]*m)|([^ ]+)|( +)/g ) { $3 and $char <= $width and $cut = $+[3]; $char += $1 ? 0 : $2 ? length $2 : length $3;
      (above line) Use of uninitialized value in addition (+) (repeated 3x)
      $char > $width and return substr( $string, 0, $cut, ''), $string; } return $string, ''; }
      I'll follow up when I have better diagnostics.

      Followup This string triggers the error for me:

      my $string2 = qq{01:11:43:09 [CC] Roger. Go into BLOCK and we'll uplin +k a new CSM state vector, and entry target load. 73 hours 00 minutes: + stop PTC 0 roll at 73; do a little more about that a little.}; my $width = 91; my $first_indent = 15; my $next_indent = 15; say "\n$string2\n"; say ta_wrap( $string2, $width, { flindent => ' ' x $first_indent, slindent => ' ' x $next_indent } ); 01:11:43:09 [CC] Roger. Go into BLOCK and we'll uplink a new CSM state + vector, and entry target load. 73 hours 00 minutes: stop PTC 0 roll +at 73; do a little more about that a little. Use of uninitialized value in addition (+) at /Users/chap/private/perl +/test_wrap line 28. 01:11:43:09 [CC] Roger. Go into BLOCK and we'll uplink +a new CSM state vector, and entry target load. 73 hours 00 minutes: sto +p PTC 0 roll at 73; do a little more about that a little.
      More followup

      Here are a couple more inputs that trigger the same error. I should point out that none of these strings I've shown contains the '\e' escape sequence.

      String=<<EOF; 05:08:28:30 [LMP] Mike, you get a waste-water dump at GET 30 plus 15 d +egrees pitch to 0 degrees pitch to 0 degrees per hour with on the inc +rease, and the force of impulsion will depend on the lunar attraction +. It is very destructive, and ends by enlarging the bore of the count +ry up across north of track. Over. EOF String=<<EOF; 02:02:40:59 [CC] Apollo 11 has got VHF A Simplex on whenever you are. +We're ready to start PTC at approximately 0 degrees per hour with on +the DSKY. Forward and up; now you are going over Mount Marilyn and th +e shock be thereby destroyed. His object now was this with the coordi +nates you have a recommended configuration for you. We'll just keep i +t bearable. But since entering the cone of shadow these last two hour +s, had the shell could not see Tranquility. What were you marking on? + Over. EOF
      Found it

      Doesn't like the word '0'. Changed

      $char += $1 ? 0 : $2 ? length $2 : length $3;
      to
      $char += $1 ? 0 : defined $2 ? length $2 : length $3;
      Doesn't look like it properly accounts for the non-printingness of escape sequences.

      Input: $string= qq{\e[30mDear Prudence won't you want so much glare coming onto my vis +or \e[3m\e[1m\e[32m[FLIK]\e[0m\e[30m shadow \e[3m\e[1m\e[32m[BLAM BLA +M BLAM]\e[0m\e[30m helmet actually gets the shadow.\e[0m} ta_wrap($string, 68, 5, 5) --> ********************************************************************| Dear Prudence won't you want so much glare coming onto my visor [FLIK] shadow [BLAM BLAM BLAM] helmet actually gets the shadow. ********************************************************************|

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145725]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-29 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found