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 |
williamc |
1.5 |
my $rv;
|
30 |
williamc |
1.1 |
my $found=0;
|
31 |
|
|
|
32 |
|
|
# print out informational
|
33 |
|
|
my $title=$userquery->title();
|
34 |
|
|
if ( $title ne "" ) {
|
35 |
williamc |
1.6 |
print "-"x50;
|
36 |
williamc |
1.1 |
print "\n";
|
37 |
|
|
print $title."\n";
|
38 |
williamc |
1.6 |
print "-"x50;
|
39 |
williamc |
1.1 |
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 |
williamc |
1.5 |
$userquery->OK();
|
48 |
|
|
return 1;
|
49 |
williamc |
1.1 |
}
|
50 |
|
|
|
51 |
williamc |
1.2 |
sub _executequery {
|
52 |
|
|
my $self=shift;
|
53 |
|
|
my $userquery=shift;
|
54 |
|
|
my $query=shift;
|
55 |
|
|
|
56 |
|
|
# -- informational for each query
|
57 |
williamc |
1.6 |
print "+"x30;
|
58 |
williamc |
1.2 |
print $userquery->querymessage($query)."\n";
|
59 |
|
|
|
60 |
|
|
# -- ship off to the correct routine based on type
|
61 |
|
|
foreach $command ( @{$self->{allowed_commands}} ) {
|
62 |
|
|
$type=$userquery->querytype($query);
|
63 |
|
|
if ( $command=~/^$type/ ) {
|
64 |
|
|
$rv=$self->_calltype($command, $userquery, $query);
|
65 |
|
|
$found='true';
|
66 |
|
|
last;
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
if ( $found ne "true" ) {
|
70 |
|
|
# default to basic
|
71 |
|
|
$rv=$self->_calltype("basic", $userquery, $query);
|
72 |
|
|
}
|
73 |
|
|
return $rv;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
williamc |
1.3 |
# requires typehadler to return the response - undef assumed to be a cancel
|
77 |
williamc |
1.1 |
sub _calltype {
|
78 |
|
|
my $self=shift;
|
79 |
|
|
my $command=shift;
|
80 |
|
|
my $userquery=shift;
|
81 |
|
|
my $query=shift;
|
82 |
|
|
|
83 |
|
|
my @options=$userquery->queryoptions($query);
|
84 |
|
|
my $rv=$self->$command( $userquery->queryprompt($query), @options );
|
85 |
|
|
|
86 |
williamc |
1.3 |
if ( defined $rv ) {
|
87 |
|
|
# now process the response
|
88 |
|
|
my ($ret,$message,@vals)=$userquery->lodgevalue($query,$rv);
|
89 |
|
|
if ($ret ne 0 ) { # invalid response
|
90 |
|
|
print "Input not valid :\n";
|
91 |
|
|
print $message->read();
|
92 |
|
|
$rv=$self->_executequery($userquery,$query);
|
93 |
|
|
}
|
94 |
williamc |
1.2 |
}
|
95 |
williamc |
1.1 |
return $rv;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
sub freechoice {
|
99 |
|
|
my $self=shift;
|
100 |
|
|
my $prompt=shift;
|
101 |
|
|
my @options=@_;
|
102 |
|
|
|
103 |
|
|
my $menuchoice;
|
104 |
|
|
my ($rv, $item);
|
105 |
|
|
|
106 |
|
|
my $num=1;
|
107 |
|
|
foreach $item ( @options ) {
|
108 |
|
|
print $num.". ".$item."\n";
|
109 |
|
|
$num++;
|
110 |
|
|
}
|
111 |
williamc |
1.4 |
print $num.". Enter".($num==1?"":" Alternative")." Value\n";
|
112 |
|
|
my $test=0;
|
113 |
|
|
# input from user
|
114 |
|
|
while ( ) {
|
115 |
|
|
$menuchoice=$self->basic("Please Enter a Number from".
|
116 |
|
|
" the list above (RETURN to cancel)>");
|
117 |
|
|
dp { $menuchoice=undef; last; } if ( $menuchoice eq "" );
|
118 |
|
|
do {print "Please Select a number Between 1 and $num :\n" ;
|
119 |
|
|
next; } if ( ($menuchoice!~/^\d+$/) ||
|
120 |
|
|
(($menuchoice > $num) || ( $menuchoice < 1)));
|
121 |
|
|
last;
|
122 |
williamc |
1.1 |
}
|
123 |
|
|
# Deal with alternative value
|
124 |
williamc |
1.6 |
if ( !defined $menuchoice ) {
|
125 |
williamc |
1.5 |
print "Cancelled\n";
|
126 |
williamc |
1.3 |
$rv=undef;
|
127 |
|
|
}
|
128 |
williamc |
1.6 |
elsif ( $menuchoice == $num ) {
|
129 |
|
|
$rv=$self->basic($prompt);
|
130 |
|
|
}
|
131 |
williamc |
1.1 |
else {
|
132 |
|
|
$rv=$options[$menuchoice-1];
|
133 |
|
|
print( $rv." selected\n");
|
134 |
|
|
}
|
135 |
|
|
return $rv;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
sub strictchoice {
|
139 |
|
|
my $self=shift;
|
140 |
|
|
my $prompt=shift;
|
141 |
|
|
my @options=@_;
|
142 |
|
|
|
143 |
|
|
my $menuchoice;
|
144 |
|
|
my ($rv, $item);
|
145 |
|
|
|
146 |
|
|
my $num=0;
|
147 |
|
|
foreach $item ( @options ) {
|
148 |
|
|
$num++;
|
149 |
|
|
print $num.". ".$item."\n";
|
150 |
|
|
}
|
151 |
williamc |
1.3 |
if ( $num == 0 ) {
|
152 |
|
|
return $rv=$self->basic($prompt);
|
153 |
williamc |
1.1 |
}
|
154 |
williamc |
1.3 |
elsif ( $num==1 ) {
|
155 |
|
|
$rv=1;
|
156 |
|
|
}
|
157 |
williamc |
1.1 |
else {
|
158 |
|
|
print "Please Enter a Number from the list above :";
|
159 |
|
|
while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
|
160 |
|
|
(($menuchoice > $num) || ( $menuchoice < 1)) ) {
|
161 |
|
|
print "Please Select a number Between 1 and $num :" ;
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
$rv=$options[$menuchoice-1];
|
165 |
|
|
return $rv;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
sub basic {
|
169 |
|
|
my $self=shift;
|
170 |
|
|
my $prompt;
|
171 |
|
|
|
172 |
|
|
((@_ && (defined $_[0]))?($prompt=shift):($prompt=">"));
|
173 |
|
|
print $prompt;
|
174 |
|
|
my $in=<STDIN>;
|
175 |
|
|
chomp $in;
|
176 |
|
|
return $in;
|
177 |
|
|
}
|