Skip to content

Instantly share code, notes, and snippets.

@arc279
Last active April 12, 2019 04:27
Show Gist options
  • Save arc279/fa2df27ec8a0093be4daf9aadacde2a8 to your computer and use it in GitHub Desktop.
Save arc279/fa2df27ec8a0093be4daf9aadacde2a8 to your computer and use it in GitHub Desktop.
cowsay for utf8 patch
--- cowsay.org 2017-02-01 17:40:08.000000000 +0900
+++ cowsay 2017-02-01 18:03:47.000000000 +0900
@@ -10,6 +10,7 @@
use Text::Wrap qw(wrap fill $columns);
use File::Basename;
use Getopt::Std;
+use Encode;
use Cwd;
$version = "3.03";
@@ -68,6 +69,7 @@
&construct_face;
&get_cow;
print @balloon_lines;
+binmode STDOUT, ":utf8";
print $the_cow;
sub list_cowfiles {
@@ -103,7 +105,7 @@
my ($l, $m);
$m = -1;
for my $i (@_) {
- $l = length $i;
+ $l = length encode('sjis', decode('utf-8', $i));
$m = $l if ($l > $m);
}
return $m;
@@ -112,7 +114,7 @@
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 %s\n";
my @border; ## up-left, up-right, down-left, down-right, left, right
if ($0 =~ /think/i) {
$thoughts = 'o';
@@ -130,12 +132,12 @@
}
push(@balloon_lines,
" " . ("_" x $max2) . " \n" ,
- sprintf($format, $border[0], $message[0], $border[1]),
+ sprintf($format, $border[0], $message[0], " " x ($max - &maxlength($message[0])), $border[1]),
(@message < 2 ? "" :
- map { sprintf($format, $border[4], $_, $border[5]) }
+ map { sprintf($format, $border[4], $_, " " x ($max - &maxlength($_)), $border[5]) }
@message[1 .. $#message - 1]),
(@message < 2 ? "" :
- sprintf($format, $border[2], $message[$#message], $border[3])),
+ sprintf($format, $border[2], $message[$#message], " " x ($max - &maxlength($message[$#message])), $border[3])),
" " . ("-" x $max2) . " \n"
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment