1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
Description: Good output format with widechar, ANSI codes ignored when determining message, Goud output with ANSI colour
Bug-Debian: http://bugs.debian.org/769565
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/cowsay/+bug/1027033
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/cowsay/+bug/1437804
Author: Tony Maillefaud <maltouzes@gmail.com>
Last-Update: 2015-12-10
--- cowsay-3.03.orig/cowsay
+++ cowsay-3.03/cowsay
@@ -11,6 +11,7 @@ use Text::Wrap qw(wrap fill $columns);
use File::Basename;
use Getopt::Std;
use Cwd;
+use Text::CharWidth qw(mbswidth);
if (${^UTF8LOCALE}) {
binmode STDIN, ':utf8';
@@ -110,7 +111,8 @@ sub maxlength {
my ($l, $m);
$m = -1;
for my $i (@_) {
- $l = length $i;
+ # $l = mbswidth $i;
+ $l = mbswidth $i =~ s/\e\[\d+(?>(;\d+)*)m//gr;
$m = $l if ($l > $m);
}
## maxlength patch from Jeronimo Pellegrini (Closes: #165218)
@@ -120,10 +122,15 @@ sub maxlength {
return $m;
}
+sub colstr {
+ (my $str, my $columns) = @_;
+ $str . ' ' x ($columns - mbswidth $str)
+}
+
sub construct_balloon {
my $max = &maxlength(@message);
my $max2 = $max + 2; ## border space fudge.
- my $format = "%s %-${max}s %s\n";
+ my $format = "%s %s %s\n";
my @border; ## up-left, up-right, down-left, down-right, left, right
if ($0 =~ /think/i) {
$thoughts = 'o';
@@ -142,12 +149,12 @@ sub construct_balloon {
## no trailing spaces (#276144)
push(@balloon_lines,
" " . ("_" x $max2) . "\n" ,
- sprintf($format, $border[0], $message[0], $border[1]),
- (@message < 2 ? "" :
- map { sprintf($format, $border[4], $_, $border[5]) }
+ sprintf($format, $border[0], colstr($message[0], $max), $border[1]),
+ (@message < 2 ? "" :
+ map { sprintf($format, $border[4], colstr($_, $max), $border[5]) }
@message[1 .. $#message - 1]),
(@message < 2 ? "" :
- sprintf($format, $border[2], $message[$#message], $border[3])),
+ sprintf($format, $border[2], colstr($message[$#message], $max), $border[3])),
" " . ("-" x $max2) . "\n"
);
}
|