From: Charles McGarvey Date: Sun, 22 Mar 2020 07:23:07 +0000 (-0600) Subject: fix printing wide char with YAML formatter X-Git-Tag: v0.49~2 X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=1c893b7e095fdaffdf47ffee426407a1f7e305fb;p=chaz%2Fgit-codeowners fix printing wide char with YAML formatter --- diff --git a/lib/App/Codeowners.pm b/lib/App/Codeowners.pm index 3ab012d..ac88f61 100644 --- a/lib/App/Codeowners.pm +++ b/lib/App/Codeowners.pm @@ -10,7 +10,6 @@ use App::Codeowners::Formatter; use App::Codeowners::Options; use App::Codeowners::Util qw(find_codeowners_in_directory run_git git_ls_files git_toplevel); use Color::ANSI::Util 0.03 qw(ansifg); -use Encode qw(encode); use File::Codeowners; use Path::Tiny; @@ -36,6 +35,8 @@ sub main { my $command = $opts->command; my $handler = $self->can("_command_$command") or die "Unknown command: $command\n"; + + binmode(STDOUT, ':encoding(UTF-8)'); $self->$handler($opts); exit 0; diff --git a/lib/App/Codeowners/Formatter/CSV.pm b/lib/App/Codeowners/Formatter/CSV.pm index a101625..c2bbbae 100644 --- a/lib/App/Codeowners/Formatter/CSV.pm +++ b/lib/App/Codeowners/Formatter/CSV.pm @@ -15,7 +15,6 @@ our $VERSION = '9999.999'; # VERSION use parent 'App::Codeowners::Formatter'; use App::Codeowners::Util qw(stringify); -use Encode qw(encode); sub start { my $self = shift; @@ -27,7 +26,7 @@ sub stream { my $self = shift; my $result = shift; - $self->text_csv->print($self->handle, [map { encode('UTF-8', stringify($_)) } @$result]); + $self->text_csv->print($self->handle, [map { stringify($_) } @$result]); } =attr text_csv diff --git a/lib/App/Codeowners/Formatter/JSON.pm b/lib/App/Codeowners/Formatter/JSON.pm index 3c7f8b8..217df3c 100644 --- a/lib/App/Codeowners/Formatter/JSON.pm +++ b/lib/App/Codeowners/Formatter/JSON.pm @@ -31,7 +31,7 @@ sub finish { my %options; $options{pretty} = 1 if lc($self->format) eq 'pretty'; - my $json = JSON::MaybeXS->new(canonical => 1, utf8 => 1, %options); + my $json = JSON::MaybeXS->new(canonical => 1, %options); my $columns = $self->columns; $results = [map { +{zip @$columns, @$_} } @$results]; diff --git a/lib/App/Codeowners/Formatter/String.pm b/lib/App/Codeowners/Formatter/String.pm index ecc0808..e6613c1 100644 --- a/lib/App/Codeowners/Formatter/String.pm +++ b/lib/App/Codeowners/Formatter/String.pm @@ -18,7 +18,6 @@ use parent 'App::Codeowners::Formatter'; use App::Codeowners::Util qw(stringf zip); use Color::ANSI::Util 0.03 qw(ansifg); -use Encode qw(encode); sub stream { my $self = shift; @@ -34,7 +33,7 @@ sub stream { ); my $text = stringf($self->format, %info); - print { $self->handle } encode('UTF-8', $text), "\n"; + print { $self->handle } $text, "\n"; } sub _expand_filter_args { diff --git a/lib/App/Codeowners/Formatter/Table.pm b/lib/App/Codeowners/Formatter/Table.pm index 1f9373e..ce6b63d 100644 --- a/lib/App/Codeowners/Formatter/Table.pm +++ b/lib/App/Codeowners/Formatter/Table.pm @@ -15,7 +15,6 @@ our $VERSION = '9999.999'; # VERSION use parent 'App::Codeowners::Formatter'; use App::Codeowners::Util qw(stringify); -use Encode qw(encode); sub finish { my $self = shift; @@ -28,7 +27,7 @@ sub finish { rows => [$self->columns, map { [map { stringify($_) } @$_] } @$results], backend => $ENV{PERL_TEXT_TABLE}, ); - print { $self->handle } encode('UTF-8', $table); + print { $self->handle } $table; } 1; diff --git a/lib/App/Codeowners/Options.pm b/lib/App/Codeowners/Options.pm index 3c1eb1d..154b098 100644 --- a/lib/App/Codeowners/Options.pm +++ b/lib/App/Codeowners/Options.pm @@ -5,6 +5,7 @@ use v5.10.1; use warnings; use strict; +use Encode qw(decode); use Getopt::Long 2.39 (); use Path::Tiny; @@ -82,6 +83,9 @@ sub new { my $class = shift; my @args = @_; + # assume UTF-8 args if non-ASCII + @args = map { decode('UTF-8', $_) } @args if grep { /\P{ASCII}/ } @args; + my $self = bless {}, $class; my @args_copy = @args; diff --git a/lib/File/Codeowners.pm b/lib/File/Codeowners.pm index 7212c23..f4caf47 100644 --- a/lib/File/Codeowners.pm +++ b/lib/File/Codeowners.pm @@ -192,7 +192,7 @@ sub write_to_filepath { my $self = shift; my $path = shift or _usage(q{$codeowners->write_to_filepath($filepath)}); - path($path)->spew_utf8([map { "$_\n" } @{$self->write_to_array('')}]); + path($path)->spew_utf8([map { "$_\n" } @{$self->write_to_array}]); } =method write_to_fh @@ -204,10 +204,11 @@ Format the file contents and write to a filehandle. =cut sub write_to_fh { - my $self = shift; - my $fh = shift or _usage(q{$codeowners->write_to_fh($fh)}); + my $self = shift; + my $fh = shift or _usage(q{$codeowners->write_to_fh($fh)}); + my $charset = shift; - for my $line (@{$self->write_to_array}) { + for my $line (@{$self->write_to_array($charset)}) { print $fh "$line\n"; } } @@ -221,9 +222,10 @@ Format the file contents and return a reference to a formatted string. =cut sub write_to_string { - my $self = shift; + my $self = shift; + my $charset = shift; - my $str = join("\n", @{$self->write_to_array}) . "\n"; + my $str = join("\n", @{$self->write_to_array($charset)}) . "\n"; return \$str; } @@ -237,7 +239,7 @@ Format the file contents as an arrayref of lines. sub write_to_array { my $self = shift; - my $charset = shift // 'UTF-8'; + my $charset = shift; my @format; @@ -263,7 +265,7 @@ sub write_to_array { } } - if ($charset) { + if (defined $charset) { $_ = encode($charset, $_) for @format; } return \@format;