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 |
|
|
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 |
|
|
$rv=0;
|
56 |
|
|
$userquery->cancel();
|
57 |
|
|
if ( $in=~/^r/ ) {
|
58 |
|
|
return $self->askuser($userquery);
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
if ( $rv ne "0" ) {
|
63 |
|
|
$userquery->OK();
|
64 |
|
|
}
|
65 |
|
|
return $rv;
|
66 |
|
|
}
|
67 |
|
|
|
68 |
williamc |
1.2 |
sub _executequery {
|
69 |
|
|
my $self=shift;
|
70 |
|
|
my $userquery=shift;
|
71 |
|
|
my $query=shift;
|
72 |
|
|
|
73 |
|
|
# -- informational for each query
|
74 |
|
|
print $userquery->querymessage($query)."\n";
|
75 |
|
|
|
76 |
|
|
# -- ship off to the correct routine based on type
|
77 |
|
|
foreach $command ( @{$self->{allowed_commands}} ) {
|
78 |
|
|
$type=$userquery->querytype($query);
|
79 |
|
|
if ( $command=~/^$type/ ) {
|
80 |
|
|
$rv=$self->_calltype($command, $userquery, $query);
|
81 |
|
|
$found='true';
|
82 |
|
|
last;
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
if ( $found ne "true" ) {
|
86 |
|
|
# default to basic
|
87 |
|
|
$rv=$self->_calltype("basic", $userquery, $query);
|
88 |
|
|
}
|
89 |
|
|
return $rv;
|
90 |
|
|
}
|
91 |
|
|
|
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 |
|
|
# now process the response
|
102 |
williamc |
1.2 |
my ($ret,$message,@vals)=$userquery->lodgevalue($query,$rv);
|
103 |
|
|
if ($ret ne 0 ) { # invalid response
|
104 |
|
|
print "Input not valid :";
|
105 |
|
|
print $message->read();
|
106 |
|
|
$rv=$self->_executequery($userquery,$query);
|
107 |
|
|
}
|
108 |
williamc |
1.1 |
return $rv;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
sub freechoice {
|
112 |
|
|
my $self=shift;
|
113 |
|
|
my $prompt=shift;
|
114 |
|
|
my @options=@_;
|
115 |
|
|
|
116 |
|
|
my $menuchoice;
|
117 |
|
|
my ($rv, $item);
|
118 |
|
|
|
119 |
|
|
my $num=1;
|
120 |
|
|
foreach $item ( @options ) {
|
121 |
|
|
print $num.". ".$item."\n";
|
122 |
|
|
$num++;
|
123 |
|
|
}
|
124 |
|
|
if ( $num == 1 ) {
|
125 |
|
|
$rv=$self->basic($prompt);
|
126 |
|
|
}
|
127 |
|
|
else {
|
128 |
|
|
print $num.". Enter Alternative Value\n";
|
129 |
|
|
print "Please Enter a Number from the list above :";
|
130 |
|
|
while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
|
131 |
|
|
(($menuchoice > $num) || ( $menuchoice < 1)) ) {
|
132 |
|
|
print "Please Select a number Between 1 and $num :" ;
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
# Deal with alternative value
|
136 |
|
|
if ( $menuchoice == $num ) {
|
137 |
|
|
$rv=$self->basic("Please enter new value $prompt");
|
138 |
|
|
}
|
139 |
|
|
else {
|
140 |
|
|
$rv=$options[$menuchoice-1];
|
141 |
|
|
print( $rv." selected\n");
|
142 |
|
|
}
|
143 |
|
|
return $rv;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
sub strictchoice {
|
147 |
|
|
my $self=shift;
|
148 |
|
|
my $prompt=shift;
|
149 |
|
|
my @options=@_;
|
150 |
|
|
|
151 |
|
|
my $menuchoice;
|
152 |
|
|
my ($rv, $item);
|
153 |
|
|
|
154 |
|
|
my $num=0;
|
155 |
|
|
foreach $item ( @options ) {
|
156 |
|
|
$num++;
|
157 |
|
|
print $num.". ".$item."\n";
|
158 |
|
|
}
|
159 |
|
|
if ( $num == 1 ) {
|
160 |
|
|
$rv=$self->basic($prompt);
|
161 |
|
|
}
|
162 |
|
|
else {
|
163 |
|
|
print "Please Enter a Number from the list above :";
|
164 |
|
|
while ( (($menuchoice=$self->basic($prompt))!~/^\d+$/) ||
|
165 |
|
|
(($menuchoice > $num) || ( $menuchoice < 1)) ) {
|
166 |
|
|
print "Please Select a number Between 1 and $num :" ;
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
$rv=$options[$menuchoice-1];
|
170 |
|
|
return $rv;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
sub basic {
|
174 |
|
|
my $self=shift;
|
175 |
|
|
my $prompt;
|
176 |
|
|
|
177 |
|
|
((@_ && (defined $_[0]))?($prompt=shift):($prompt=">"));
|
178 |
|
|
print $prompt;
|
179 |
|
|
my $in=<STDIN>;
|
180 |
|
|
chomp $in;
|
181 |
|
|
return $in;
|
182 |
|
|
}
|