Update of /cvsroot/fink/fink/perlmod/Fink
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25330/perlmod/Fink

Modified Files:
      Tag: branch_0_24
        CLI.pm ChangeLog 
Log Message:
Backport more robust input

Index: CLI.pm
===================================================================
RCS file: /cvsroot/fink/fink/perlmod/Fink/CLI.pm,v
retrieving revision 1.21.2.1
retrieving revision 1.21.2.2
diff -u -d -r1.21.2.1 -r1.21.2.2
--- CLI.pm      13 Jul 2005 01:48:32 -0000      1.21.2.1
+++ CLI.pm      13 Jul 2005 19:12:30 -0000      1.21.2.2
@@ -263,23 +263,29 @@
        my $prompt = shift;
        my %opts = (default => 1, timeout => 0, @_);
 
+       my $choice_prompt = $opts{default} ? "Y/n" : "y/N";
+
        my $meaning;
+       my $answer = &get_input(
+               "$prompt [$choice_prompt]",
+               $opts{timeout},
+       );
        while (1) {
-               my $answer = &get_input(
-                       "$prompt [".($opts{default} ? "Y/n" : "y/N")."]",
-                       $opts{timeout}
-               );
                chomp $answer;
                if ($answer eq "") {
                        $meaning = $opts{default};
                        last;
-               } elsif ($answer =~ /^y(es?)?/i) {
+               } elsif ($answer =~ /^y(es?)?$/i) {
                        $meaning = 1;
                        last;
-               } elsif ($answer =~ /^no?/i) {
+               } elsif ($answer =~ /^no?$/i) {
                        $meaning = 0;
                        last;
                }
+               $answer = &get_input(
+                       "Invalid choice. Please try again [$choice_prompt]",
+                       $opts{timeout},
+               );
        }
 
        return $meaning;
@@ -338,7 +344,7 @@
        my @choices = @{$opts{choices}};
        my $default = $opts{default};
 
-       my ($count, $answer, $default_value);
+       my ($count, $default_value);
 
        if (@choices/2 != int(@choices/2)) {
                confess 'Odd number of elements in @choices';
@@ -358,7 +364,7 @@
        $count = 0;
        for (my $index = 0; $index <= $#choices; $index+=2) {
                $count++;
-               print "\n($count)        $choices[$index]";
+               print "($count)\t$choices[$index]\n";
                if (!defined $default_value && (
                                                (
                                                 ($default->[0] eq "label" && 
$choices[$index]   eq $default->[1])
@@ -371,17 +377,26 @@
 
        }
        $default_value = 1 if !defined $default_value;
-       print "\n\n";
+       print "\n";
 
-       $answer = &get_input("$prompt [$default_value]", $opts{timeout});
-       chomp($answer);
-       if (!$answer) {
-               $answer = 0;
-       }
-       $answer = int($answer);
-       if ($answer < 1 || $answer > $count) {
-               $answer = $default_value;
+       my $answer = &get_input(
+               "$prompt [$default_value]",
+               $opts{timeout},
+       );
+       while (1) {
+               chomp $answer;
+               if ($answer eq "") {
+                       $answer = $default_value;
+                       last;
+               } elsif ($answer =~ /^[1-9]\d*$/ and $answer >= 1 && $answer <= 
$count) {
+                       last;
+               }
+               $answer = &get_input(
+                       "Invalid choice. Please try again [$default_value]",
+                       $opts{timeout},
+               );
        }
+
        return $choices[2*$answer-1];
 }
 
@@ -405,6 +420,8 @@
        my $prompt = shift;
        my $timeout = shift || 0;
 
+       use POSIX qw(:termios_h tcflush);
+       
        # print the prompt string (leaving cursor on the same line)
        $prompt = "" if !defined $prompt;
        &print_breaking("$prompt ", 0);
@@ -420,6 +437,7 @@
        my $answer = eval {
                local $SIG{ALRM} = sub { die "SIG$_[0]\n"; };  # alarm() expired
                alarm $timeout;  # alarm(0) means cancel the timer
+               tcflush(fileno(STDIN),TCIFLUSH);
                my $answer = <STDIN>;
                alarm 0;
                return $answer;

Index: ChangeLog
===================================================================
RCS file: /cvsroot/fink/fink/perlmod/Fink/ChangeLog,v
retrieving revision 1.873.2.47
retrieving revision 1.873.2.48
diff -u -d -r1.873.2.47 -r1.873.2.48
--- ChangeLog   13 Jul 2005 02:24:08 -0000      1.873.2.47
+++ ChangeLog   13 Jul 2005 19:12:30 -0000      1.873.2.48
@@ -1,3 +1,7 @@
+2005-07-13  Dave Vasilevsky  <[EMAIL PROTECTED]>
+
+       * CLI.pm: Backport more robust input.
+
 2005-07-12  Matthew Sachs  <[EMAIL PROTECTED]>
 
        * Services.pm: Backport fix for looser checking of GCC version.



-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP, 
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
Fink-commits mailing list
Fink-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fink-commits

Reply via email to