diff options
author | Rafael Garcia-Suarez <rgarciasuarez@mandriva.org> | 2004-04-14 13:47:02 +0000 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@mandriva.org> | 2004-04-14 13:47:02 +0000 |
commit | 0c8f651988b155d12deb3aea26c2f206648c61e5 (patch) | |
tree | 9bebdb61d92b19c9db9b98a15efdf8e8d3cf7fae /urpm | |
parent | fadf35a458884372e139c97282a333f4a0d84f25 (diff) | |
download | urpmi-0c8f651988b155d12deb3aea26c2f206648c61e5.tar urpmi-0c8f651988b155d12deb3aea26c2f206648c61e5.tar.gz urpmi-0c8f651988b155d12deb3aea26c2f206648c61e5.tar.bz2 urpmi-0c8f651988b155d12deb3aea26c2f206648c61e5.tar.xz urpmi-0c8f651988b155d12deb3aea26c2f206648c61e5.zip |
Factorize the message/prompt/I18N code of urpm* in a separate module
Diffstat (limited to 'urpm')
-rw-r--r-- | urpm/args.pm | 14 | ||||
-rw-r--r-- | urpm/msg.pm | 109 |
2 files changed, 118 insertions, 5 deletions
diff --git a/urpm/args.pm b/urpm/args.pm index 15d246fb..6dc6fb45 100644 --- a/urpm/args.pm +++ b/urpm/args.pm @@ -18,6 +18,9 @@ Getopt::Long::Configure(@configuration); # global urpm object to be passed by the main program my $urpm; +# stores the values of the command-line options +our %options; + # options specifications for Getopt::Long my %options_spec = ( @@ -35,7 +38,9 @@ my %options_spec = ( 'excludemedia|exclude-media=s' => \$::excludemedia, 'sortmedia|sort-media=s' => \$::sortmedia, 'synthesis=s' => \$::synthesis, - auto => \$urpm->{options}{auto}, + auto => sub { + $urpm->{options}{auto} = $::auto = 1; + }, 'allow-medium-change' => \$::allow_medium_change, 'auto-select' => \$::auto_select, 'no-remove|no-uninstall' => \$::no_remove, @@ -75,12 +80,12 @@ my %options_spec = ( $value =~ /(.+):(.+)/ or die N("bad proxy declaration on command line\n"); @{$urpm->{proxy}}{qw(user pwd)} = ($1, $2); }, - 'bug=s' => \$::bug, + 'bug=s' => \$options{bug}, 'env=s' => \$::env, - X => \$::X, + X => \$options{X}, WID => \$::WID, 'best-output' => sub { - $::X ||= $ENV{DISPLAY} && system('/usr/X11R6/bin/xtest', '') == 0 + $options{X} ||= $ENV{DISPLAY} && system('/usr/X11R6/bin/xtest', '') == 0 }, 'verify-rpm!' => \$urpm->{options}{'verify-rpm'}, 'test!' => \$::test, @@ -238,7 +243,6 @@ my %options_spec = ( ); # common options setup -# TODO <> for arguments foreach my $k ("help|h", "no-locales", "test!", "force", "root=s", "use-distrib=s", "parallel=s") diff --git a/urpm/msg.pm b/urpm/msg.pm new file mode 100644 index 00000000..bf3272e2 --- /dev/null +++ b/urpm/msg.pm @@ -0,0 +1,109 @@ +package urpm::msg; + +use strict; +use Exporter; +our @ISA = 'Exporter'; +our @EXPORT = qw(N log_it to_utf8 message_input gmessage message); + +my $noexpr = N("Nn"); +my $yesexpr = N("Yy"); + +#- I18N. +eval { + require Locale::gettext; + use POSIX qw(LC_ALL); + setlocale(LC_ALL, ""); + Locale::gettext::textdomain("urpmi"); +}; + +sub N { + my ($format, @params) = @_; + sprintf(eval { Locale::gettext::gettext($format || '') } || $format, @params); +} + +sub log_it { + #- if invoked as a simple user, nothing should be logged. + if (our $log) { + open my $fh, ">>$log" or die "can't output to log file: $!\n"; + print $fh @_; + close $fh; + } +} + +sub to_utf8 { Locale::gettext::iconv($_[0], undef, "UTF-8") } + +sub gmessage { + my ($msg, %params) = @_; + my $ok = to_utf8($params{ok} || N("Ok")); + my $cancel = to_utf8($params{cancel} || N("Cancel")); + $ok =~ s/,/\\,/g; $cancel =~ s/,/\\,/g; + my $buttons = $params{ok_only} ? "$ok:0" : "$ok:0,$cancel:2"; + foreach (@{$params{add_buttons}}) { + s/,/\\,/g; + $buttons .= ",$_"; + } + $msg = to_utf8($msg); + `gmessage -default "$ok" -buttons "$buttons" "$msg"`; +} + +sub message_input { + my ($msg, $default_input, %opts) = @_; + my $input; + if ($urpm::args::options{X} && !$default_input) { + #- if a default input is given, the user doesn't have to choose (and being asked). + gmessage($msg, ok_only => 1); + $urpm::args::options{bug} and log_it($msg); + } else { + while (1) { + if ($urpm::args::options{bug}) { + print STDOUT $msg; + } else { + print SAVEOUT $msg; + } + if ($default_input) { + $urpm::args::options{bug} and log_it($default_input); + return $default_input; + } + $input = <STDIN>; + defined $input or return undef; + $urpm::args::options{bug} and log_it($input); + if ($opts{boolean}) { + $input =~ /^[$noexpr$yesexpr]*$/ and last; + } elsif ($opts{range}) { + 1 <= $input && $input <= $opts{range} and last; + } else { + last; + } + message(N("Sorry, bad choice, try again\n")); + } + } + return $input; +} + +sub message { + my ($msg, $no_X) = @_; + if ($urpm::args::options{X} && !$no_X && !$::auto) { + gmessage($msg, ok_only => 1); + $urpm::args::options{bug} and log_it($msg); + } else { + if ($urpm::args::options{bug}) { + print STDOUT "$msg\n"; + } else { + print SAVEOUT "$msg\n"; + } + } +} + +1; + +__END__ + +=head1 NAME + +urpm::msg - routines to prompt messages from the urpm* tools + +=head1 SYNOPSIS + +=head1 DESCRIPTION + +=cut |