Skip to content

Commit

Permalink
added prompt function and method
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 15, 2012
1 parent 48f2f70 commit af03414
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@

3.48 2012-10-15
- Added prompt method to Mojo::ByteStream.
- Added prompt function to Mojo::Util.
- Improved Mojo::Content performance slightly.
- Improved cpanify command to prompt for password if necessary.
- Fixed memory leak in Mojo::Headers.

3.47 2012-10-13
Expand Down
8 changes: 7 additions & 1 deletion lib/Mojo/ByteStream.pm
Expand Up @@ -11,7 +11,7 @@ our @EXPORT_OK = ('b');
# Turn most functions from Mojo::Util into methods
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_md5_sum hmac_sha1_sum),
qw(html_escape html_unescape md5_bytes md5_sum punycode_decode),
qw(html_escape html_unescape md5_bytes md5_sum prompt punycode_decode),
qw(punycode_encode quote sha1_bytes sha1_sum slurp spurt squish trim),
qw(unquote url_escape url_unescape xml_escape xor_encode)
);
Expand Down Expand Up @@ -210,6 +210,12 @@ Generate binary MD5 checksum for bytestream with L<Mojo::Util/"md5_bytes">.
Generate MD5 checksum for bytestream with L<Mojo::Util/"md5_sum">.
=head2 C<prompt>
$stream = $stream->prompt;
Prompt user with L<Mojo::Util/"prompt">.
=head2 C<punycode_decode>
$stream = $stream->punycode_decode;
Expand Down
21 changes: 18 additions & 3 deletions lib/Mojo/Util.pm
Expand Up @@ -40,9 +40,9 @@ $REVERSE{$ENTITIES{$_}} //= $_
our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_escape),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp spurt squish trim unquote),
qw(url_escape url_unescape xml_escape xor_encode)
qw(html_unescape md5_bytes md5_sum prompt punycode_decode punycode_encode),
qw(quote secure_compare sha1_bytes sha1_sum slurp spurt squish trim),
qw(unquote url_escape url_unescape xml_escape xor_encode)
);

sub b64_decode { decode_base64(shift) }
Expand Down Expand Up @@ -126,6 +126,14 @@ sub html_unescape {
sub md5_bytes { md5(@_) }
sub md5_sum { md5_hex(@_) }

sub prompt {
local $| = 1;
print shift;
my $input = <STDIN>;
chomp $input;
return $input;
}

sub punycode_decode {
my $input = shift;
use integer;
Expand Down Expand Up @@ -525,6 +533,13 @@ Generate binary MD5 checksum for string.
Generate MD5 checksum for string.
=head2 C<prompt>
my $string = prompt 'Password: ';
Print string to C<STDOUT>, wait for user to type something and finish with an
C<ENTER> and return the string typed in wihout the trailing newline.
=head2 C<punycode_decode>
my $string = punycode_decode $punycode;
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojolicious/Command/cpanify.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojolicious::Command';

use File::Basename 'basename';
use Mojo::UserAgent;
use Mojo::Util 'prompt';

has description => "Upload distribution to CPAN.\n";
has usage => <<"EOF";
Expand All @@ -21,10 +22,11 @@ sub run {
# Options
$self->_options(
\@args,
'p|password=s' => \(my $password = ''),
'u|user=s' => \(my $user = '')
'p|password=s' => \(my $password),
'u|user=s' => \(my $user = '')
);
die $self->usage unless my $file = shift @args;
$password //= prompt 'Password: ';

# Upload
my $tx = Mojo::UserAgent->new->detect_proxy->post_form(
Expand Down

0 comments on commit af03414

Please sign in to comment.