]> Dogcows Code - chaz/git-codeowners/commitdiff
fix printing wide char with YAML formatter
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 22 Mar 2020 07:23:07 +0000 (01:23 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 22 Mar 2020 07:30:04 +0000 (01:30 -0600)
lib/App/Codeowners.pm
lib/App/Codeowners/Formatter/CSV.pm
lib/App/Codeowners/Formatter/JSON.pm
lib/App/Codeowners/Formatter/String.pm
lib/App/Codeowners/Formatter/Table.pm
lib/App/Codeowners/Options.pm
lib/File/Codeowners.pm

index 3ab012d137fa72cc7fcaa60851a9eb3661d282c3..ac88f6184c91440656f7f9f83f4662953b01372b 100644 (file)
@@ -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;
index a101625f62690b2004a8dfd0d3d844ceea5e3154..c2bbbae75c80d1466245dda3224ccaeda36f1cd9 100644 (file)
@@ -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
index 3c7f8b8e2311832c212aa4b0e5846d628ed93c76..217df3c79e92ff483c1588fd0651929c6fa0f85a 100644 (file)
@@ -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];
index ecc0808c9b53c615d3d6a5e44d54bdb77f346183..e6613c1954dbfb5b009097804ab17190b59e0bd5 100644 (file)
@@ -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 {
index 1f9373e1e647d53e39053d31bf9bff17f7fb66a4..ce6b63d8339dcaf89f5a8c1f8a923693e94d2ca7 100644 (file)
@@ -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;
index 3c1eb1d26381717147eeb3dcd833c324989ee152..154b09894fd92a793f10654d977575b11221d09a 100644 (file)
@@ -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;
index 7212c238ae25ca8976444b0892b26056d0d1cce1..f4caf4722fe7bac6c0c057cd964e75c20674f575 100644 (file)
@@ -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;
This page took 0.031635 seconds and 4 git commands to generate.