ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/SimpleUserInterface.pm
Revision: 1.4
Committed: Mon Feb 7 07:28:01 2000 UTC (25 years, 3 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.3: +11 -10 lines
Log Message:
cancel added to freechoice - returns undef

File Contents

# User Rev Content
1 williamc 1.1 #
2     # SimpleUserInterface.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     #
8     # Interface
9     # ---------
10     # new() : A new SimpleUserInterface object
11     # askuser(Query) : Get inputs from the user based on the given query
12     # returns 0 if cancelled or 1 if accepted
13    
14     package ActiveDoc::SimpleUserInterface;
15     use ActiveDoc::Query;
16     require 5.004;
17    
18     sub new {
19     my $class=shift;
20     $self={};
21     bless $self, $class;
22     @{$self->{allowed_commands}}=qw(freechoice basic strictchoice);
23     return $self;
24     }
25    
26     sub askuser {
27     my $self=shift;
28     my $userquery=shift;
29     my $rv=1;
30     my $found=0;
31    
32     # print out informational
33     my $title=$userquery->title();
34     if ( $title ne "" ) {
35     print "-"x80;
36     print "\n";
37     print $title."\n";
38     print "-"x80;
39     print "\n";
40     }
41     print $userquery->intro();
42     print "\n\n";
43    
44     foreach $query ( $userquery->querylist() ) {
45 williamc 1.2 $rv=$self->_executequery($userquery,$query);
46 williamc 1.1 }
47    
48    
49     # --- confirm changes with user if we have input more than one value
50 williamc 1.3 # if ( $#{$userquery->querylist()} > 1 ) {
51     # print "\nDo you wish to Accept,Restart or Cancel these values? ".
52     # "(a/r/c)\n";
53     # my $in=<STDIN>;
54     # if ( $in!~/^a/ ) {
55     # $userquery->cancel();
56     # if ( $in=~/^r/ ) {
57     # return $self->askuser($userquery);
58     # }
59     # }
60     # }
61     if ( defined $rv ) {
62 williamc 1.1 $userquery->OK();
63     }
64     return $rv;
65     }
66    
67 williamc 1.2 sub _executequery {
68     my $self=shift;
69     my $userquery=shift;
70     my $query=shift;
71    
72     # -- informational for each query
73     print $userquery->querymessage($query)."\n";
74    
75     # -- ship off to the correct routine based on type
76     foreach $command ( @{$self->{allowed_commands}} ) {
77     $type=$userquery->querytype($query);
78     if ( $command=~/^$type/ ) {
79     $rv=$self->_calltype($command, $userquery, $query);
80     $found='true';
81     last;
82     }
83     }
84     if ( $found ne "true" ) {
85     # default to basic
86     $rv=$self->_calltype("basic", $userquery, $query);
87     }
88     return $rv;
89     }
90    
91 williamc 1.3 # requires typehadler to return the response - undef assumed to be a cancel
92 williamc 1.1 sub _calltype {
93     my $self=shift;
94     my $command=shift;
95     my $userquery=shift;
96     my $query=shift;
97    
98     my @options=$userquery->queryoptions($query);
99     my $rv=$self->$command( $userquery->queryprompt($query), @options );
100    
101 williamc 1.3 if ( defined $rv ) {
102     # now process the response
103     my ($ret,$message,@vals)=$userquery->lodgevalue($query,$rv);
104     if ($ret ne 0 ) { # invalid response
105     print "Input not valid :\n";
106     print $message->read();
107     $rv=$self->_executequery($userquery,$query);
108     }
109 williamc 1.2 }
110 williamc 1.1 return $rv;
111     }
112    
113     sub freechoice {
114     my $self=shift;
115     my $prompt=shift;
116     my @options=@_;
117    
118     my $menuchoice;
119     my ($rv, $item);
120    
121     my $num=1;
122     foreach $item ( @options ) {
123     print $num.". ".$item."\n";
124     $num++;
125     }
126 williamc 1.4 print $num.". Enter".($num==1?"":" Alternative")." Value\n";
127     my $test=0;
128     # input from user
129     while ( ) {
130     $menuchoice=$self->basic("Please Enter a Number from".
131     " the list above (RETURN to cancel)>");
132     dp { $menuchoice=undef; last; } if ( $menuchoice eq "" );
133     do {print "Please Select a number Between 1 and $num :\n" ;
134     next; } if ( ($menuchoice!~/^\d+$/) ||
135     (($menuchoice > $num) || ( $menuchoice < 1)));
136     last;
137 williamc 1.1 }
138     # Deal with alternative value
139     if ( $menuchoice == $num ) {
140 williamc 1.3 $rv=$self->basic($prompt);
141 williamc 1.1 }
142 williamc 1.3 elsif ( $menuchoice eq "" ) {
143     $rv=undef;
144     }
145 williamc 1.1 else {
146     $rv=$options[$menuchoice-1];
147     print( $rv." selected\n");
148     }
149     return $rv;
150     }
151    
152     sub strictchoice {
153     my $self=shift;
154     my $prompt=shift;
155     my @options=@_;
156    
157     my $menuchoice;
158     my ($rv, $item);
159    
160     my $num=0;
161     foreach $item ( @options ) {
162     $num++;
163     print $num.". ".$item."\n";
164     }
165 williamc 1.3 if ( $num == 0 ) {
166     return $rv=$self->basic($prompt);
167 williamc 1.1 }
168 williamc 1.3 elsif ( $num==1 ) {
169     $rv=1;
170     }
171 williamc 1.1 else {
172     print "Please Enter a Number from the list above :";
173     while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
174     (($menuchoice > $num) || ( $menuchoice < 1)) ) {
175     print "Please Select a number Between 1 and $num :" ;
176     }
177     }
178     $rv=$options[$menuchoice-1];
179     return $rv;
180     }
181    
182     sub basic {
183     my $self=shift;
184     my $prompt;
185    
186     ((@_ && (defined $_[0]))?($prompt=shift):($prompt=">"));
187     print $prompt;
188     my $in=<STDIN>;
189     chomp $in;
190     return $in;
191     }