in reply to Text::ANSI::Util for wrapping "colorful" text
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...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Text::ANSI::Util for wrapping "colorful" text
by ibm1620 (Hermit) on May 10, 2022 at 04:42 UTC | |
Re^2: Text::ANSI::Util for wrapping "colorful" text
by ibm1620 (Hermit) on May 10, 2022 at 12:26 UTC | |
by tybalt89 (Monsignor) on May 10, 2022 at 13:06 UTC | |
by ibm1620 (Hermit) on Jul 25, 2022 at 16:42 UTC | |
by tybalt89 (Monsignor) on Jul 25, 2022 at 19:22 UTC | |
by ibm1620 (Hermit) on Jul 25, 2022 at 20:34 UTC | |
| |
by ibm1620 (Hermit) on May 10, 2022 at 14:21 UTC |
In Section
Seekers of Perl Wisdom