Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Latest version. Seems to be faster, maybe, perhaps...

Interesting technique of overshoot and backup, with regex inside regex.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11143680 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 = ''; while( $text =~ /^(.{$width,}?\S)(?: |$)(??{ $width >= length $1 =~ s"\e[[\d;]*m""gr ? '(*FAIL)' : '' })/ ) { my $take = $1 =~ /^(.*\S) +/ ? $1 : die "wrap failed"; $out .= "$take\e[m\n"; my $replace = $+[0]; substr $text, 0, $replace, $pads{slindent} . join('', $take =~ /\e[[\d;]*m/g) =~ s/.*\e\[0?m//r =~ s/(\e[[\d;]*m)(?=.*\1)//gr =~ s/\e\[[39][0-7]m(?=.*\e\[[39][0-7]m)//gr; } return "$out$text\e[m\n"; } 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') . 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[93m01: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);

In reply to Re: Text::ANSI::Util for wrapping "colorful" text by tybalt89
in thread 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-11-29 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?