Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

by tybalt89 (Monsignor)
on May 10, 2022 at 13:06 UTC ( #11143740=note: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl use strict; # use warnings; use 5.010; use Term::ANSIColor qw(color :constants); sub ta_wrap { 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( $text =~ s/^(.+)(?: +|$)(??{ $width < length join '', split m{\e[[\d;]*m}, $1 and '(*FAIL)' })// ) { $out .= "$1\e[m\n"; $carryover .= join '', $1 =~ /\e[[\d;]*m/g; $carryover =~ s/^.*\e\[0?m//; # clear up to and including last RES +ET $text =~ /./ and $text = "$pads{slindent}$carryover$text"; } return $out; } sub show { my $line = shift; my $wrapped = ta_wrap($line, 30, {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') . "dead, to begin 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...";

Some tweaks and renaming variables for clarity.

Replies are listed 'Best First'.
Re^4: Text::ANSI::Util for wrapping "colorful" text
by ibm1620 (Friar) on Jul 25, 2022 at 16:42 UTC
    Just thought I should add this...

    This implementation starts dragging when the number of (space-separated) words exceeds ~300. This paragraph contains 341 words and took around a minute to wrap.

    ^[30m01:06:52:18 [CMP] Okay, Well look at the bottom by the first 2 in +ches without any constant direction. Indeed, at the bottom of this we +alth of nature. President Barbicane, however, lost not one of the Bar +bados. It is but an atom of carbonic acid, by a moment. We'll have fu +rther word on the forward OMNI is good for the inhabitants ought to b +e despised. But that day, about eleven o'clock position from the eart +h. Besides, even without these conditions, as regards the optical ins +truments at their highest pitch throughout this triumphant march. Mic +hel Ardan hoped to get any crew status report. We're about 88 degrees + east, coming up on the earth once more entered the Bay of Espiritu S +anto, 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 St +ates of the projectile! The necessary operations for the food bags ar +e so good, who ventures to affirm it. "True," rejoined the major. "Tr +ue," replied Barbicane. The problem is a PROCEED and allow the hot fi +re is complete, and I'm proceeding with opening the hatch at this cri +sis, as though the samples for that of the journey. Many foolish thin +gs had been probably 20 minutes to pass to Columbia, we would expect +that once this is Houston. We need a change had taken place in a pite +ous tone, that is not inhabited; no! the moon was a little hard for u +s in all four tanks and position the antenna tracks through in the si +gnal for renewed cries of still greater precision, they succeeded in +rising, drew a thermometer from its center. "Then," said Nicholl, it +is a little scratchy. It looks fairly good size. It has the choice be +tween two seas, they pretended that it is true; but it will continue +its elliptical crater, and then add UP DATA LINK switch to RANGE. We' +re going to be in opposition. These eclipses, caused by weight; nor a + boat, whose stability on the B B shot has hit the surface.^[0m
    Another one, with 360 words, ran for five minutes before I killed it.

    I'm running perl 5.36.0 on an M1 MacBook Air.

      I put that line into my test program and it took about 20 milliseconds.
      I'm running 5.36.0 on ArchLinux on a Ryzen 3700x
      I'd suspect either copy/paste error or some problem with Mac's \r vs Linux \n line termination, which of course I can't test since I don't have a Mac.

        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.

Re^4: Text::ANSI::Util for wrapping "colorful" text
by ibm1620 (Friar) on May 10, 2022 at 14:21 UTC
    Seems to handle everything I've thrown at it! Much obliged. :-)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-12-08 23:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found