First pass at trying to write my own.
#!/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) = @_;
my %pads = (flindent => '', slindent => '', %{ $optionsref // {} } )
+;
$text = $pads{flindent} . $text;
my $out = '';
my $save = '';
while( $text =~ s/^(.+)(?: +|$)(??{
$width < length join '', split m{\e[[\d;]*m}, $1 and '(*FAIL)'
})// )
{
$out .= $1 . "\e[m\n";
$save .= join '', $& =~ /\e[[\d;]*m/g;
$save =~ s/^.*\e\[0?m//; # clear up to and including last RESET
length $text and $text = "$pads{slindent}$save$text";
}
return "$out $text";
}
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...";
Give this a shot...
-
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.