« CPAN/Maker/Bootstrapper/Role/PAUSEUpload.pm
↑ Summary
CPAN/Maker/Bootstrapper/Role/CreateDeps.pm »
| 1 | package Text::ASCIITable::FixANSI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Readonly; | ||
| 7 | |||
| 8 | our $VERSION = '1.0.0'; | ||
| 9 | |||
| 10 | Readonly::Scalar my $VERIFIED_AGAINST => '0.22'; | ||
| 11 | Readonly::Scalar my $ANSI_RE => qr/\e\[[\d;]*[a-zA-Z]|\e\([0B]/; | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | require Text::ASCIITable; | ||
| 15 | |||
| 16 | if ( $Text::ASCIITable::VERSION ne '0.22' ) { | ||
| 17 | warn sprintf | ||
| 18 | "Text::ASCIITable::FixANSI: patch verified against 0.22, found %s\n", | ||
| 19 | $Text::ASCIITable::VERSION; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | my $ORIG_COUNT = \&Text::ASCIITable::count; | ||
| 24 | |||
| 25 | ######################################################################## | ||
| 26 | sub _ansi_aware_wrap { | ||
| 27 | ######################################################################## | ||
| 28 | my ( $text, $max_width, $nostrict ) = @_; | ||
| 29 | |||
| 30 | return $text if !defined $max_width || $max_width <= 0; | ||
| 31 | |||
| 32 | my @wrapped_lines; | ||
| 33 | |||
| 34 | foreach my $line ( split /\n/xsm, $text ) { | ||
| 35 | my $current_line = q{}; | ||
| 36 | my $current_length = 0; | ||
| 37 | |||
| 38 | my @tokens = $line =~ /($ANSI_RE|\s+|(?:(?!\e)[^\s])+)/gxsm; | ||
| 39 | |||
| 40 | foreach my $token (@tokens) { | ||
| 41 | if ( $token =~ /^\e/xsm ) { | ||
| 42 | $current_line .= $token; # zero visible width | ||
| 43 | next; | ||
| 44 | } | ||
| 45 | |||
| 46 | if ( $token =~ /^\s+$/xsm ) { | ||
| 47 | if ( $current_length + length($token) <= $max_width ) { | ||
| 48 | $current_line .= $token; | ||
| 49 | $current_length += length $token; | ||
| 50 | } | ||
| 51 | elsif ( $current_length > 0 ) { | ||
| 52 | push @wrapped_lines, $current_line; | ||
| 53 | ( $current_line, $current_length ) = ( q{}, 0 ); | ||
| 54 | } | ||
| 55 | next; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $word_len = length $token; | ||
| 59 | |||
| 60 | if ( $current_length + $word_len <= $max_width ) { | ||
| 61 | $current_line .= $token; | ||
| 62 | $current_length += $word_len; | ||
| 63 | next; | ||
| 64 | } | ||
| 65 | |||
| 66 | if ( $current_length > 0 ) { | ||
| 67 | push @wrapped_lines, $current_line; | ||
| 68 | ( $current_line, $current_length ) = ( q{}, 0 ); | ||
| 69 | } | ||
| 70 | |||
| 71 | # hard-wrap over-long words, carrying pending zero-width ANSI | ||
| 72 | my $remaining = $token; | ||
| 73 | my $prefix = $current_line; | ||
| 74 | |||
| 75 | if ( !$nostrict ) { | ||
| 76 | while ( length($remaining) > $max_width ) { | ||
| 77 | push @wrapped_lines, $prefix . substr $remaining, 0, $max_width; | ||
| 78 | $prefix = q{}; | ||
| 79 | $remaining = substr $remaining, $max_width; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | $current_line = $prefix . $remaining; | ||
| 84 | $current_length = length $remaining; | ||
| 85 | } | ||
| 86 | |||
| 87 | push @wrapped_lines, $current_line | ||
| 88 | if length($current_line) > 0 || !@tokens; | ||
| 89 | } | ||
| 90 | |||
| 91 | return join "\n", @wrapped_lines; | ||
| 92 | } | ||
| 93 | |||
| 94 | ######################################################################## | ||
| 95 | sub _ansi_aware_count { | ||
| 96 | ######################################################################## | ||
| 97 | my ( $self, $str ) = @_; | ||
| 98 | |||
| 99 | # preserve cb_count precedence and non-ANSI behavior | ||
| 100 | if ( $self->{options}{cb_count} || !$self->{options}{allowANSI} ) { | ||
| 101 | return $ORIG_COUNT->( $self, $str ); | ||
| 102 | } | ||
| 103 | |||
| 104 | if ( $self->{options}{allowHTML} ) { | ||
| 105 | $str =~ s/<.+?>//gxsm; | ||
| 106 | } | ||
| 107 | |||
| 108 | $str =~ s/$ANSI_RE//gxsm; | ||
| 109 | |||
| 110 | return length $str; | ||
| 111 | } | ||
| 112 | |||
| 113 | { | ||
| 114 | no warnings 'redefine'; ## no critic (ProhibitNoWarnings) | ||
| 115 | |||
| 116 | *Text::ASCIITable::count = \&_ansi_aware_count; | ||
| 117 | *Text::ASCIITable::wrap = \&_ansi_aware_wrap; | ||
| 118 | |||
| 119 | # keep Wrap.pm's slot consistent for anyone importing it directly | ||
| 120 | *Text::ASCIITable::Wrap::wrap = \&_ansi_aware_wrap; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | __END__ | ||
| 126 | |||
| 127 | =pod | ||
| 128 | |||
| 129 | =head1 NAME | ||
| 130 | |||
| 131 | Text::ASCIITable::FixANSI - ANSI-safe wrapping and width counting for Text::ASCIITable | ||
| 132 | |||
| 133 | =head1 SYNOPSIS | ||
| 134 | |||
| 135 | use Text::ASCIITable; | ||
| 136 | use Text::ASCIITable::FixANSI; | ||
| 137 | |||
| 138 | =head1 DESCRIPTION | ||
| 139 | |||
| 140 | Process-wide patch for Text::ASCIITable 0.22. Fixes C<wrap()> splitting | ||
| 141 | ANSI escape sequences (and discarding the remainder of over-long words), | ||
| 142 | and widens C<count()> to strip multi-parameter SGR sequences. Retire when | ||
| 143 | the upstream fix ships. | ||
| 144 | |||
| 145 | =cut |