Skip to content

Commit

Permalink
added auto_encode method to Mojolicious::Command
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 19, 2013
1 parent a593c4c commit 47274a8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
54 changes: 19 additions & 35 deletions lib/Mojolicious/Command.pm
Expand Up @@ -18,11 +18,17 @@ has encoding => sub { langinfo CODESET };
has quiet => 0;
has usage => "usage: $0\n";

sub auto_encode {
my ($self, @args) = @_;
return @args unless my $encoding = $self->encoding;
return map { encode $encoding, $_ } @args;
}

sub chmod_file {
my ($self, $path, $mod) = @_;
chmod $mod, $path or croak qq{Can't chmod file "$path": $!};
$mod = sprintf '%lo', $mod;
$self->enc_say(" [chmod] $path $mod") unless $self->quiet;
say $self->auto_encode(" [chmod] $path $mod") unless $self->quiet;
return $self;
}

Expand All @@ -34,10 +40,12 @@ sub chmod_rel_file {
sub create_dir {
my ($self, $path) = @_;

if (-d $path) { $self->enc_say(" [exist] $path") unless $self->quiet }
if (-d $path) {
say $self->auto_encode(" [exist] $path") unless $self->quiet;
}
else {
mkpath $path or croak qq{Can't make directory "$path": $!};
$self->enc_say(" [mkdir] $path") unless $self->quiet;
say $self->auto_encode(" [mkdir] $path") unless $self->quiet;
}

return $self;
Expand All @@ -48,15 +56,9 @@ sub create_rel_dir {
$self->create_dir($self->rel_dir($path));
}

sub enc_print { print shift->_enc(@_) }

sub enc_say { shift->enc_print(@_, "\n") }

sub enc_warn { warn shift->_enc(@_) }

sub help {
my $self = shift;
$self->enc_print($self->usage);
print $self->auto_encode($self->usage);
exit 0;
}

Expand Down Expand Up @@ -86,7 +88,7 @@ sub write_file {
my ($self, $path, $data) = @_;
$self->create_dir(dirname $path);
spurt $data, $path;
$self->enc_say(" [write] $path") unless $self->quiet;
say $self->auto_encode(" [write] $path") unless $self->quiet;
return $self;
}

Expand All @@ -95,12 +97,6 @@ sub write_rel_file {
$self->write_file($self->rel_file($path), $data);
}

sub _enc {
my ($self, @args) = @_;
return @args unless my $encoding = $self->encoding;
return map { encode $encoding, $_ } @args;
}

1;

=encoding utf8
Expand Down Expand Up @@ -186,6 +182,12 @@ Usage information for command, used for the help screen.
L<Mojolicious::Command> inherits all methods from L<Mojo::Base> and implements
the following new ones.
=head2 auto_encode
my $bytes = $command->auto_encode('I ♥ Mojolicious!');
Encode characters with the appropriate encoding for the current environment.
=head2 chmod_file
$command = $command->chmod_file('/home/sri/foo.txt', 0644);
Expand All @@ -210,24 +212,6 @@ Create a directory.
Portably create a directory relative to the current working directory.
=head2 enc_print
$command->enc_print('I ♥ Mojolicious!');
Encode and C<print>.
=head2 enc_say
$command->enc_say('I ♥ Mojolicious!');
Encode and C<say>.
=head2 enc_warn
$command->enc_warn('I ♥ Mojolicious!');
Encode and C<warn>.
=head2 help
$command->help;
Expand Down
19 changes: 12 additions & 7 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -97,7 +97,7 @@ sub run {

# Ignore intermediate content
return if $redirect && $res->is_status_class(300);
defined $selector ? ($buffer .= pop) : $self->enc_print(pop);
defined $selector ? ($buffer .= pop) : print $self->auto_encode(pop);
}
);
}
Expand All @@ -108,7 +108,8 @@ sub run {
STDOUT->autoflush(1);
my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content));
my ($err, $code) = $tx->error;
$self->enc_warn(qq{Problem loading URL "$url". ($err)\n}) if $err && !$code;
warn $self->auto_encode(qq{Problem loading URL "$url". ($err)\n})
if $err && !$code;

# JSON Pointer
return unless defined $selector;
Expand All @@ -126,9 +127,9 @@ sub _json {
my $json = Mojo::JSON->new;
return unless my $data = $json->decode(shift);
return unless defined($data = Mojo::JSON::Pointer->new->get($data, shift));
return $self->enc_say($data)
return say $self->auto_encode($data)
unless ref $data eq 'HASH' || ref $data eq 'ARRAY';
$self->enc_say(decode 'UTF-8', $json->encode($data));
say $self->auto_encode(decode 'UTF-8', $json->encode($data));
}

sub _select {
Expand All @@ -147,15 +148,19 @@ sub _select {
}

# Text
elsif ($command eq 'text') { $self->enc_say($_->text) for @$results }
elsif ($command eq 'text') {
say $self->auto_encode($_->text) for @$results;
}

# All text
elsif ($command eq 'all') { $self->enc_say($_->all_text) for @$results }
elsif ($command eq 'all') {
say $self->auto_encode($_->all_text) for @$results;
}

# Attribute
elsif ($command eq 'attr') {
next unless my $name = shift @args;
$self->enc_say($_->attrs->{$name}) for @$results;
say $self->auto_encode($_->attrs->{$name}) for @$results;
}

# Unknown
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/routes.pm
Expand Up @@ -73,7 +73,7 @@ sub _draw {
$format .= '?' if $format && $optional;
push @parts, $format ? "$regex$format" : $regex if $verbose;

$self->enc_say(join ' ', @parts);
say $self->auto_encode(join ' ', @parts);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -97,14 +97,14 @@ sub run {

# Print list of all available commands
my $max = max map { length $_->[0] } @commands;
$self->enc_print($self->message);
print $self->auto_encode($self->message);
for my $command (@commands) {
my $name = $command->[0];
my $description = $command->[1]->new->description;
my $cmd = " $name" . (' ' x ($max - length $name)) . " $description";
$self->enc_print($cmd);
print $self->auto_encode($cmd);
}
return $self->enc_print($self->hint);
print $self->auto_encode($self->hint);
}

sub start_app {
Expand Down

0 comments on commit 47274a8

Please sign in to comment.