| 1 | package Term::Screen::Wizard; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | no strict 'refs'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use base qw(Term::Screen::ReadLine); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | use Term::Screen::ReadLine; | ||
| 8 | |||
| 9 | use vars qw($VERSION); | ||
| 10 | |||
| 11 | BEGIN { | ||
| 12 | $VERSION=0.56; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub system { | ||
| 16 | my $self=shift; | ||
| 17 | my $cmd; | ||
| 18 | foreach my $f (@_) { | ||
| 19 | $cmd.="$f "; | ||
| 20 | } | ||
| 21 | system "stty -raw echo"; | ||
| 22 | system @_; | ||
| 23 | system "stty raw -echo"; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub add_screen { | ||
| 27 | my $self = shift; | ||
| 28 | my $args = { | ||
| 29 | NAME => "noname", | ||
| 30 | HEADER => "", | ||
| 31 | FOOTER => "", | ||
| 32 | CANCEL => "Esc - Cancel", | ||
| 33 | NEXT => "Ctrl-Enter - Next", | ||
| 34 | PREVIOUS => "F3 - Previous", | ||
| 35 | FINISH => "Ctrl-Enter - Finish", | ||
| 36 | NOFINISH => 0, | ||
| 37 | HASPREVIOUS => 0, | ||
| 38 | HELPTEXT => undef, | ||
| 39 | HELP => "F1 - Help", | ||
| 40 | ROW => 2, | ||
| 41 | COL => 2, | ||
| 42 | PROMPTS => undef, | ||
| 43 | READONLY => undef, | ||
| 44 | @_, | ||
| 45 | }; | ||
| 46 | |||
| 47 | my $arr=$self->{SCREENS}; | ||
| 48 | my @array; | ||
| 49 | |||
| 50 | #$self->del_screen($args->{NAME}); | ||
| 51 | foreach my $scr (@{ $self->{SCREENS} }) { | ||
| 52 | return 0 if ($scr->{NAME} eq $args->{NAME}); | ||
| 53 | } | ||
| 54 | |||
| 55 | if ($arr) { @array=@$arr; } | ||
| 56 | push @array, $args; | ||
| 57 | $self->{SCREENS}=\@array; | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub del_screen { | ||
| 62 | my $self = shift; | ||
| 63 | my $name = shift; | ||
| 64 | my $i; | ||
| 65 | my %screen; | ||
| 66 | my $scr; | ||
| 67 | my $arr=$self->{SCREENS}; | ||
| 68 | my @array=@$arr; | ||
| 69 | my @narray=(); | ||
| 70 | my $retval=0; | ||
| 71 | |||
| 72 | $self->{SCREENS}=(); | ||
| 73 | |||
| 74 | foreach $scr (@array) { | ||
| 75 | if ($scr->{NAME} eq $name) { | ||
| 76 | $retval=1; | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | push @narray,$scr; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | $self->{SCREENS}=\@narray; | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub get_keys { | ||
| 88 | my $self = shift; | ||
| 89 | my $scr; | ||
| 90 | my %values; | ||
| 91 | my %screens; | ||
| 92 | my $screens; | ||
| 93 | |||
| 94 | foreach $scr (@_) { $screens{$scr}=1;$screens+=1; } | ||
| 95 | if ($screens == 0) {my $a; | ||
| 96 | foreach $a (@{ $self->{SCREENS} }) { | ||
| 97 | $screens{$a->{NAME}}=1; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | for $scr (@{ $self->{SCREENS} }) { | ||
| 102 | next if (not exists $screens{$scr->{NAME}}); | ||
| 103 | my $prompt; | ||
| 104 | my $name=$scr->{NAME}; | ||
| 105 | for $prompt (@{ $scr->{PROMPTS} }) { | ||
| 106 | #$self->at(22,0)->puts($prompt->{KEY})->puts(" - ")->puts($prompt->{NEWVALUE})->getch(); | ||
| 107 | $values{$name}{$prompt->{KEY}}=$prompt->{VALUE}; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return %values; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub wizard { | ||
| 114 | my $self=shift; | ||
| 115 | my $i=0; | ||
| 116 | my $arr=$self->{SCREENS}; | ||
| 117 | my @array=@$arr; | ||
| 118 | my $scr; | ||
| 119 | my $i; | ||
| 120 | my $N; | ||
| 121 | my $what; | ||
| 122 | my $footer; | ||
| 123 | my $space=chr(32).chr(32).chr(32); | ||
| 124 | my $scr_name; | ||
| 125 | |||
| 126 | my @screens; | ||
| 127 | $N=0; | ||
| 128 | |||
| 129 | if ($self->{COLS} <= 0) { | ||
| 130 | die "Term::Screen::COLS <= 0, please set environment variable \$COLUMNS\n"; | ||
| 131 | } | ||
| 132 | if ($self->{ROWS} <= 0) { | ||
| 133 | die "Term::Screen::ROWS <= 0, please set environment variable \$LINES\n"; | ||
| 134 | } | ||
| 135 | |||
| 136 | foreach $i (@_) { | ||
| 137 | push @screens,$i; | ||
| 138 | } | ||
| 139 | $N=scalar @screens; | ||
| 140 | if ($N == 0) {my $a; | ||
| 141 | foreach $a (@array) { | ||
| 142 | push @screens,$a->{NAME}; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | $N=scalar @screens; | ||
| 146 | |||
| 147 | $i=0; | ||
| 148 | while ($i < $N) { | ||
| 149 | $scr_name=$screens[$i]; | ||
| 150 | foreach my $a ( @array ) { | ||
| 151 | if ($scr_name eq $a->{NAME}) { | ||
| 152 | $scr=$a; | ||
| 153 | last; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | $footer=""; | ||
| 158 | if ($scr->{HELPTEXT}) { $footer.=$space.$scr->{HELP}; } | ||
| 159 | $footer.=$space.$scr->{CANCEL}; | ||
| 160 | if ($i > 0 or $scr->{HASPREVIOUS} ) { $footer.=$space.$scr->{PREVIOUS}; } | ||
| 161 | if ($i < $N-1 or $scr->{NOFINISH} ) { $footer.=$space.$scr->{NEXT}; } | ||
| 162 | if ($i == $N-1 and not $scr->{NOFINISH} ) { $footer.=$space.$scr->{FINISH}; } | ||
| 163 | $scr->{FOOTER}=$footer; | ||
| 164 | |||
| 165 | $what=$self->_display_screen($scr); | ||
| 166 | |||
| 167 | if ($what eq "previous") { | ||
| 168 | if ($i==0 and $scr->{HASPREVIOUS}) { last; } | ||
| 169 | if ($i >0) { $i-=1; } | ||
| 170 | } | ||
| 171 | elsif ($what eq "next") { | ||
| 172 | $i++; | ||
| 173 | if ($i == $N) { last; } | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | last; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | if ($what ne "cancel") { | ||
| 181 | my $scr_name; | ||
| 182 | $what="finish" if ($what ne "previous"); | ||
| 183 | foreach $scr_name (@screens) { | ||
| 184 | foreach my $a ( @array ) { | ||
| 185 | if ($scr_name eq $a->{NAME}) { | ||
| 186 | $scr=$a; | ||
| 187 | last; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | my $prompt; | ||
| 192 | |||
| 193 | if ($scr->{NOFINISH}) { | ||
| 194 | $what="next" if ($what ne "previous"); | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | $what="finish" if ($what ne "previous"); | ||
| 198 | } | ||
| 199 | |||
| 200 | foreach $prompt (@{ $scr->{PROMPTS} }) { | ||
| 201 | $prompt->{VALUE}=$prompt->{NEWVALUE}; | ||
| 202 | $prompt->{NEWVALUE}=undef; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | foreach $scr_name (@screens) { | ||
| 208 | foreach my $a ( @array ) { | ||
| 209 | if ($scr_name eq $a->{NAME}) { | ||
| 210 | $scr=$a; | ||
| 211 | last; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | foreach my $prompt (@{ $scr->{PROMPTS} }) { | ||
| 215 | $prompt->{NEWVALUE}=undef; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | return $what; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub _display_screen { | ||
| 224 | my $self = shift; | ||
| 225 | my $scr = shift; | ||
| 226 | my $prompt; | ||
| 227 | my $promptlen; | ||
| 228 | my $displen; | ||
| 229 | my $i; | ||
| 230 | my $key; | ||
| 231 | my @prompts=@{ $scr->{PROMPTS} }; | ||
| 232 | my $line; | ||
| 233 | my $N; | ||
| 234 | my $val; | ||
| 235 | my $only; | ||
| 236 | my $convert; | ||
| 237 | my $dashes; | ||
| 238 | my %keys; | ||
| 239 | my $valid; | ||
| 240 | |||
| 241 | |||
| 242 | %keys = ( "esc" => 1, | ||
| 243 | "ctrl-enter" => 1, | ||
| 244 | "pgdn" => 1, | ||
| 245 | "pgup" => 1, | ||
| 246 | "k3" => 1, | ||
| 247 | "k4" => 1, | ||
| 248 | "k1" => 2, | ||
| 249 | ); | ||
| 250 | |||
| 251 | |||
| 252 | {my $i; | ||
| 253 | for(1..$self->{COLS}) { | ||
| 254 | $dashes.="-"; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | $N=scalar @prompts; | ||
| 259 | $key="none"; | ||
| 260 | |||
| 261 | while (not defined $keys{$key} or $keys{$key} == 2) { | ||
| 262 | |||
| 263 | $self->clrscr(); | ||
| 264 | |||
| 265 | if ($scr->{HEADER}) { | ||
| 266 | $self->at(0,0)->puts($scr->{HEADER}); | ||
| 267 | $self->at(1,0)->puts($dashes); | ||
| 268 | } | ||
| 269 | if ($scr->{FOOTER}) { | ||
| 270 | $self->at($self->{ROWS}-1,0)->puts($scr->{FOOTER}); | ||
| 271 | $self->at($self->{ROWS}-2,0)->puts($dashes); | ||
| 272 | } | ||
| 273 | |||
| 274 | if ($key eq "k1") { | ||
| 275 | $self->at(3,0)->puts($scr->{HELPTEXT}); | ||
| 276 | $self->getch(); | ||
| 277 | $key=""; | ||
| 278 | next; | ||
| 279 | } | ||
| 280 | |||
| 281 | $key=""; | ||
| 282 | $promptlen=0; | ||
| 283 | $i=3; | ||
| 284 | foreach $prompt ( @prompts ) { | ||
| 285 | my $s=$prompt->{PROMPT}; | ||
| 286 | if (($prompt->{KEY} ne "NIL") and (not $prompt->{NIL})) { | ||
| 287 | if (length $s > $promptlen) { $promptlen=length $s; } | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | $prompt->{NIL}=1; | ||
| 291 | } | ||
| 292 | $self->at($i,0)->puts($prompt->{PROMPT}); | ||
| 293 | $i++; | ||
| 294 | } | ||
| 295 | |||
| 296 | $promptlen++; | ||
| 297 | $displen=$self->{COLS}-$promptlen-3; # see increment of promptlen below | ||
| 298 | |||
| 299 | $i=3; | ||
| 300 | foreach $prompt ( @prompts ) { | ||
| 301 | if (not defined $prompt->{NEWVALUE}) { | ||
| 302 | $val=$prompt->{VALUE}; | ||
| 303 | $prompt->{NEWVALUE}=$val; | ||
| 304 | } | ||
| 305 | else { | ||
| 306 | $val=$prompt->{NEWVALUE}; | ||
| 307 | } | ||
| 308 | |||
| 309 | my $L=length $val; | ||
| 310 | |||
| 311 | if ($prompt->{PASSWORD}) { | ||
| 312 | $val=$self->setstr("*",$L); | ||
| 313 | } | ||
| 314 | |||
| 315 | if ($L>$displen) { $L=$displen; } | ||
| 316 | $val=substr($val,0,$L); | ||
| 317 | |||
| 318 | if (not $prompt->{NIL}) { | ||
| 319 | $self->at($i,$promptlen)->puts(": $val"); | ||
| 320 | } | ||
| 321 | $i++; | ||
| 322 | } | ||
| 323 | |||
| 324 | $promptlen+=2; | ||
| 325 | |||
| 326 | $i=0; | ||
| 327 | while (($i < $N) and $prompts[$i]->{NIL}) { | ||
| 328 | $i+=1; | ||
| 329 | } | ||
| 330 | #print "$i\n";getc(); | ||
| 331 | |||
| 332 | while (not defined $keys{$key}) { | ||
| 333 | |||
| 334 | if ($prompts[$i]->{ONLYVALID}) { | ||
| 335 | $only=$prompts[$i]->{ONLYVALID}; | ||
| 336 | } | ||
| 337 | else { | ||
| 338 | $only=undef; | ||
| 339 | } | ||
| 340 | |||
| 341 | if ($prompts[$i]->{CONVERT}) { | ||
| 342 | $convert=$prompts[$i]->{CONVERT}; | ||
| 343 | } | ||
| 344 | else { | ||
| 345 | $convert=undef; | ||
| 346 | } | ||
| 347 | |||
| 348 | my $readonly=$scr->{READONLY}; | ||
| 349 | if (not defined $readonly) { | ||
| 350 | $readonly=$prompts[$i]->{READONLY}; | ||
| 351 | } | ||
| 352 | |||
| 353 | $line=$self->readline(ROW => $i+3, COL => $promptlen, | ||
| 354 | LEN => $prompts[$i]->{LEN}, | ||
| 355 | DISPLAYLEN => $displen, | ||
| 356 | LINE => $prompts[$i]->{NEWVALUE}, | ||
| 357 | EXITS => { "pgup" => "pgup", "pgdn" => "pgdn", "k1" => "k1", "k3" => "k3", "k4" => "k4" }, | ||
| 358 | ONLYVALID => $only, | ||
| 359 | CONVERT => $convert, | ||
| 360 | PASSWORD => $prompts[$i]->{PASSWORD}, | ||
| 361 | NOCOMMIT => $prompts[$i]->{NOCOMMIT}, | ||
| 362 | READONLY => $readonly, | ||
| 363 | ); | ||
| 364 | |||
| 365 | {my $L=length $line; | ||
| 366 | my $val=$line; | ||
| 367 | if ($L>$displen) { $L=$displen; } | ||
| 368 | if ($prompts[$i]->{PASSWORD}) { | ||
| 369 | $val=$self->setstr("*",$L); | ||
| 370 | } | ||
| 371 | $val=substr($val,0,$L); | ||
| 372 | $self->at($i+3,$promptlen)->puts($val); | ||
| 373 | } | ||
| 374 | |||
| 375 | if ((exists $prompts[$i]->{VALIDATOR}) and ($self->lastkey() ne "esc")) { | ||
| 376 | my $expr=$prompts[$i]->{VALIDATOR}; | ||
| 377 | if (not $expr=~/::/) { $expr="::".$expr; } | ||
| 378 | $valid=&$expr($self,$line); | ||
| 379 | } | ||
| 380 | else { | ||
| 381 | $valid=1; | ||
| 382 | } | ||
| 383 | |||
| 384 | if ($valid) { | ||
| 385 | $prompts[$i]->{NEWVALUE}=$line; | ||
| 386 | $key=$self->lastkey(); | ||
| 387 | #$self->at(22,0)->puts(" $key - $line")->getch(); | ||
| 388 | } | ||
| 389 | else { | ||
| 390 | $key=$self->lastkey(); | ||
| 391 | if ($key ne "k1" and $key ne "esc" and $key ne "k3") { $key=""; } | ||
| 392 | } | ||
| 393 | |||
| 394 | if ($key eq "tab" or $key eq "enter" or $key eq "kd") { | ||
| 395 | if ($prompts[$i]->{READY} and ((length $line) gt 0)) { | ||
| 396 | $i=$N; | ||
| 397 | } | ||
| 398 | else { | ||
| 399 | $i+=1; | ||
| 400 | while ($prompts[$i]->{NIL} and ($i < $N)) { | ||
| 401 | $i++; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | if ($i >= $N) { | ||
| 405 | $i=0; | ||
| 406 | if ($key eq "enter") { | ||
| 407 | $key="ctrl-enter"; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | } | ||
| 411 | elsif ( $key eq "ku" ) { | ||
| 412 | $i--; | ||
| 413 | while ($prompts[$i]->{NIL} and ($i >= 0)) { | ||
| 414 | $i--; | ||
| 415 | } | ||
| 416 | if ($i < 0 ) { $i=$N-1; | ||
| 417 | while ($prompts[$i]->{NIL} and ($i >= 0)) { | ||
| 418 | $i--; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | if ($key eq "esc") { | ||
| 427 | return "cancel"; | ||
| 428 | } | ||
| 429 | elsif ($key eq "ctrl-enter" or $key eq "pgdn" or $key eq "k4" ) { | ||
| 430 | return "next"; | ||
| 431 | } | ||
| 432 | elsif ($key eq "pgup" or $key eq "k3" ) { | ||
| 433 | return "previous"; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | sub set { | ||
| 438 | my $self = shift; | ||
| 439 | my $screen = shift; | ||
| 440 | my $scr = $self->_get_screen($screen) | ||
| 441 | or die "unknown screen \"$screen\""; | ||
| 442 | my $id = shift; | ||
| 443 | |||
| 444 | if (exists $scr->{$id}) { | ||
| 445 | if ($id eq "PROMPTS") { | ||
| 446 | my $key=shift; | ||
| 447 | my $found=0; | ||
| 448 | my @prompts=@{$scr->{PROMPTS}}; | ||
| 449 | my $prompt; | ||
| 450 | foreach $prompt (@prompts) { | ||
| 451 | if ($prompt->{KEY} eq $key) { | ||
| 452 | my $id=shift; | ||
| 453 | my $val=shift; | ||
| 454 | $found=1; | ||
| 455 | $prompt->{$id}=$val; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | if (not $found) { | ||
| 459 | die "Can't find key <$key> in prompts of screen\n"; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | else { | ||
| 463 | $scr->{$id}=shift; | ||
| 464 | } | ||
| 465 | } | ||
| 466 | else { | ||
| 467 | my $found=0; | ||
| 468 | my $prompt; | ||
| 469 | my @prompts=@{$scr->{PROMPTS}}; | ||
| 470 | foreach $prompt (@prompts) { | ||
| 471 | if ($prompt->{KEY} eq $id) { | ||
| 472 | $prompt->{VALUE}=shift; | ||
| 473 | $found=1; | ||
| 474 | last; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | if (not $found) { | ||
| 478 | die "Can't find key <$id> in screen or prompts of screen\n"; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | return $self; | ||
| 483 | } | ||
| 484 | |||
| 485 | sub get { | ||
| 486 | my $self = shift; | ||
| 487 | my $screen = shift; | ||
| 488 | my $scr = $self->_get_screen($screen) | ||
| 489 | or die "unknown screen \"$screen\""; | ||
| 490 | my $id = shift; | ||
| 491 | |||
| 492 | if (exists $scr->{$id}) { | ||
| 493 | if ($id eq "PROMPTS") { | ||
| 494 | my $key=shift; | ||
| 495 | my $found=0; | ||
| 496 | my @prompts=@{$scr->{PROMPTS}}; | ||
| 497 | my $prompt; | ||
| 498 | foreach $prompt (@prompts) { | ||
| 499 | if ($prompt->{KEY} eq $key) { | ||
| 500 | my $id=shift; | ||
| 501 | #$self->at(17,0)->puts("$key"); | ||
| 502 | #$self->at(18,0)->puts("$id=")->puts($prompt->{$id})->getch(); | ||
| 503 | $found=1; | ||
| 504 | return $prompt->{$id}; | ||
| 505 | } | ||
| 506 | } | ||
| 507 | if (not $found) { | ||
| 508 | die "Can't find key <$key> in prompts of screen\n"; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | else { | ||
| 512 | return $scr->{$id}; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | else { | ||
| 516 | my $found=0; | ||
| 517 | my $prompt; | ||
| 518 | my @prompts=@{$scr->{PROMPTS}}; | ||
| 519 | foreach $prompt (@prompts) { | ||
| 520 | if ($prompt->{KEY} eq $id) { | ||
| 521 | return $prompt->{VALUE}; | ||
| 522 | $found=1; | ||
| 523 | last; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | if (not $found) { | ||
| 527 | die "Can't find key <$id> in screen or prompts of screen\n"; | ||
| 528 | } | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | |||
| 533 | sub _get_screen { | ||
| 534 | my $self = shift; | ||
| 535 | my $name = shift; | ||
| 536 | my %screen; | ||
| 537 | my $scr; | ||
| 538 | my $arr=$self->{SCREENS}; | ||
| 539 | my @array=@$arr; | ||
| 540 | |||
| 541 | foreach $scr (@array) { | ||
| 542 | if ($scr->{NAME} eq $name) { | ||
| 543 | return $scr; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | return undef; | ||
| 548 | } | ||
| 549 | |||
| 550 | =pod | ||
| 551 | |||
| 552 | =head1 NAME | ||
| 553 | |||
| 554 | Term::Screen::Wizard - A wizard on your terminal... | ||
| 555 | |||
| 556 | =head1 SYNOPSIS | ||
| 557 | |||
| 558 | use Term::Screen::Wizard; | ||
| 559 | |||
| 560 | $scr = new Term::Screen::Wizard; | ||
| 561 | |||
| 562 | $scr->clrscr(); | ||
| 563 | |||
| 564 | $scr->add_screen( | ||
| 565 | NAME => "PROCES", | ||
| 566 | HEADER => "Give me the new process id", | ||
| 567 | CANCEL => "Esc - Annuleren", | ||
| 568 | NEXT => "Ctrl-Enter - Volgende", | ||
| 569 | PREVIOUS => "F3 - Vorige", | ||
| 570 | FINISH => "Ctrl-Enter - Klaar", | ||
| 571 | PROMPTS => [ | ||
| 572 | { KEY => "PROCESID", PROMPT => "Proces Id", LEN=>32, VALUE=>"123456789.00.04" , ONLYVALID => "[a-zA-Z0-9.]*" }, | ||
| 573 | { KEY => "TYPE", PROMPT => "Intern or Extern Process (I/E)", CONVERT => "up", LEN=>1, ONLYVALID=>"[ieIE]*" }, | ||
| 574 | { KEY => "OMSCHRIJVING", PROMPT => "Description of Proces", LEN=>75 }, | ||
| 575 | { KEY => "PASSWORD", PROMPT => "Enter a password", LEN=>14, PASSWORD=>1 } | ||
| 576 | ], | ||
| 577 | |||
| 578 | # | ||
| 579 | # OK This helptext is in Dutch, but it's clear how it works isn't it? | ||
| 580 | # | ||
| 581 | HELPTEXT => "\n\n\n". | ||
| 582 | " In dit scherm kan een nieuw proces Id worden opgevoerd\n". | ||
| 583 | "\n". | ||
| 584 | " ProcesId - is het ingevoerde Proces Id\n". | ||
| 585 | " Intern/Extern - is het proces belastingdienst intern of niet?\n". | ||
| 586 | " Omschrijving - Een korte omschrijving van het proces.\n" | ||
| 587 | ); | ||
| 588 | |||
| 589 | $scr->add_screen( | ||
| 590 | NAME => "X.400",, | ||
| 591 | HEADER => "Voer het X.400 adres in", | ||
| 592 | # | ||
| 593 | # So the point is you can change the Wizard 'buttons'. | ||
| 594 | # | ||
| 595 | CANCEL => "Esc - Annuleren", | ||
| 596 | NEXT => "Ctrl-Enter - Volgende", | ||
| 597 | PREVIOUS => "F3 - Vorige", | ||
| 598 | FINISH => "Ctrl-Enter - Klaar", | ||
| 599 | PROMPTS => [ | ||
| 600 | { KEY => "COUNTRY", PROMPT => "COUNTRY", LEN => 2, CONVERT => "up", ONLYVALID => "[^/]*" }, | ||
| 601 | { KEY => "AMDM", PROMPT => "AMDM", LEN => 16, CONVERT => "up", ONLYVALID => "[^/]*" }, | ||
| 602 | { KEY => "PRDM", PROMPT => "PRDM", LEN => 16, CONVERT => "up", ONLYVALID => "[^/]*" }, | ||
| 603 | { KEY => "ORG", PROMPT => "ORGANISATION", LEN => 16, CONVERT => "up", ONLYVALID => "[^/]*" }, | ||
| 604 | { KEY => "OU1", PROMPT => "UNIT1", LEN => 16, CONVERT => "up", ONLYVALID => "[^/]*" }, | ||
| 605 | { KEY => "OU2", PROMPT => "UNIT2", LEN => 16, CONVERT => "up", ONLYVALID => "[^/]*" }, | ||
| 606 | { KEY => "OU3", PROMPT => "UNIT3", LEN => 16, CONVERT => "up", ONLYVALID => "[^/]*" }, | ||
| 607 | ], | ||
| 608 | HELPTEXT => "\n\n\n". | ||
| 609 | " In dit scherm kan een standaard X.400 adres worden ingevoerd voor een ProcesId", | ||
| 610 | ); | ||
| 611 | |||
| 612 | $scr->add_screen( | ||
| 613 | NAME => "GETALLEN",, | ||
| 614 | HEADER => "Voer getallen in", | ||
| 615 | CANCEL => "Esc - Annuleren", | ||
| 616 | NEXT => "Ctrl-Enter - Volgende", | ||
| 617 | PREVIOUS => "F3 - Vorige", | ||
| 618 | FINISH => "Ctrl-Enter - Klaar", | ||
| 619 | PROMPTS => [ | ||
| 620 | { KEY => "ANINT", PROMPT => "INT", LEN => 10, CONVERT => "up", ONLYVALID => "[0-9]*" }, | ||
| 621 | { KEY => "ADOUBLE", PROMPT => "DOUBLE", LEN => 16, CONVERT => "up", ONLYVALID => "[0-9]+([.,][0-9]*)?" }, | ||
| 622 | ], | ||
| 623 | ); | ||
| 624 | |||
| 625 | $scr->wizard(); | ||
| 626 | |||
| 627 | $scr->wizard("PROCES","GETALLEN"); | ||
| 628 | |||
| 629 | $scr->clrscr(); | ||
| 630 | |||
| 631 | %values=$scr->get_keys(); | ||
| 632 | @array=( "PROCES", "X.400", "GETALLEN" ); | ||
| 633 | |||
| 634 | for $i (@array) { | ||
| 635 | print "\n$i\n\r"; | ||
| 636 | for $key (keys % { $values{$i} }) { | ||
| 637 | my $val=$values{$i}{$key}; | ||
| 638 | print " $key=$val\n\r"; | ||
| 639 | } | ||
| 640 | } | ||
| 641 | |||
| 642 | %values=$scr->get_keys("X.400","PROCES"); | ||
| 643 | |||
| 644 | exit; | ||
| 645 | |||
| 646 | =head1 DESCRIPTION | ||
| 647 | |||
| 648 | This is a module to have a Wizard on a Terminal. It inherits from | ||
| 649 | Term::Screen::ReadLine. The module provides some functions to add | ||
| 650 | screens. The result is a Hash with keys that have the (validated) | ||
| 651 | values that the used inputted on the different screens. | ||
| 652 | |||
| 653 | =head1 USAGE | ||
| 654 | |||
| 655 | Description of the interface. | ||
| 656 | |||
| 657 | add_screen( | ||
| 658 | NAME => <name of screen>, | ||
| 659 | HEADER => <header to put on top of screen>, | ||
| 660 | CANCEL => <text of cancel 'button', defaults to 'Esc - Cancel'>, | ||
| 661 | NEXT => <text of next 'button', defaults to 'Ctrl-Enter - Next'>, | ||
| 662 | PREVIOUS => <text of previous 'button', defaults tro 'PgUp - Previous'>, | ||
| 663 | FINISH => <text of finish 'button', defaults to 'Ctrl-Enter - Finish>, | ||
| 664 | HELP => <text of help 'button', defaults to 'F1 - Help'>, | ||
| 665 | HELPTEXT => <text to put on your helpscreen> | ||
| 666 | NOFINISH => <1/0 - Inidicates that this wizard is/is not (1/0) part | ||
| 667 | of an ongoing 'wizard sequence'> | ||
| 668 | READONLY => <1/0 - read only option to get a read only screen> | ||
| 669 | PROMPTS => <array of fields to input> | ||
| 670 | ) | ||
| 671 | |||
| 672 | This function add's a screen to the list of screens that the wizards goes | ||
| 673 | through sequentially. If NOFINISH==1, the finish 'button' is not used. Use | ||
| 674 | this, if the last screen of this wizard is not actually the last screen | ||
| 675 | of a sequence of wizards. | ||
| 676 | |||
| 677 | For instance, if you need to go one way or the other after the first screen, | ||
| 678 | you provide a wizard with one screen and no FINISH button. After that you | ||
| 679 | call the next sequence of screens. | ||
| 680 | |||
| 681 | PROMPTS => [ | ||
| 682 | { KEY => "ANINT", PROMPT => "INT", LEN => 10, CONVERT => "up", ONLYVALID => "[0-9]*", READONLY => 1 }, | ||
| 683 | { KEY => "ADOUBLE", PROMPT => "DOUBLE", LEN => 16, CONVERT => "up", ONLYVALID => "[0-9]+([.,][0-9]*)?" }, | ||
| 684 | { KEY => "DATE", PROMPT => "DATE", LEN => 8, CONVERT => "up", ONLYVALID => "[0-9]+", VALIDATOR => "ValidateCCYYMMDD" }, | ||
| 685 | ] | ||
| 686 | |||
| 687 | sub ValidateCCYYMMDD { | ||
| 688 | my $wizard=shift; | ||
| 689 | my $line=shift; | ||
| 690 | (...) | ||
| 691 | |||
| 692 | return <1/0> | ||
| 693 | } | ||
| 694 | |||
| 695 | Note the entries in PROMPTS : | ||
| 696 | |||
| 697 | KEY is the hash key with what you can access the field. | ||
| 698 | PROMPT is the prompt to use for the field. | ||
| 699 | LEN is the maximum length of the field. | ||
| 700 | CONVERT 'up' or 'lo' for uppercase or lowercase. If not used | ||
| 701 | it won't convert. | ||
| 702 | ONLYVALID is a regex to use for validation. Note: validation is | ||
| 703 | done *before* conversion! If not used, no validation is | ||
| 704 | done. | ||
| 705 | VALUE a default value to use. This value will change if the | ||
| 706 | wizard is used. | ||
| 707 | VALIDATOR a validator sub to validate a line of input, after it has | ||
| 708 | been inputted. | ||
| 709 | READONLY Set this prompt readonly. | ||
| 710 | NOCOMMIT defined/undefined ==> This field will not ask for a return, | ||
| 711 | works great for choices (LEN=1). | ||
| 712 | READY If this field has had it's input, go to the next screen. | ||
| 713 | |||
| 714 | |||
| 715 | del_screen(<name>) | ||
| 716 | |||
| 717 | This function deletes a screen with given name from the list of screens. | ||
| 718 | |||
| 719 | |||
| 720 | get_keys([screens]) | ||
| 721 | |||
| 722 | Optional arguments are screens to use. Example: | ||
| 723 | |||
| 724 | %values=$a->get_keys() -> gives all screens. | ||
| 725 | %values=$a->get_keys("PROCESS","NUMBERS") -> gives only screens PROCESS and NUMBERS. | ||
| 726 | |||
| 727 | This function gives you all the keys in a hash of a hash. Actually | ||
| 728 | a hash of screens and each screen a hash of keys. See synopsis for | ||
| 729 | usage. | ||
| 730 | |||
| 731 | |||
| 732 | set(SCREEN,KEY,VALUE,...) | ||
| 733 | |||
| 734 | To set key KEY of screen SCREEN equal to VALUE. Example: | ||
| 735 | |||
| 736 | $wizard->set("NUMBERS",HEADER,"This is the new header"); | ||
| 737 | |||
| 738 | sets the HEADER of screen NUMBERS to a new value. | ||
| 739 | |||
| 740 | $wizard->set("NUMBERS","ADOUBLE",999.999); | ||
| 741 | |||
| 742 | sets the prompt ADOUBLE of screen NUMBERS to 999.999 | ||
| 743 | |||
| 744 | More examples: | ||
| 745 | |||
| 746 | $scr->set("GETALLEN",HEADER,"dit is de header"); | ||
| 747 | $scr->set("GETALLEN","ADOUBLE",999.99); | ||
| 748 | $scr->set("PROCES",READONLY,1); | ||
| 749 | $scr->set("PROCES",HEADER,"proces scherm is read only nu"); | ||
| 750 | $scr->set("GETALLEN",PROMPTS,ANINT,READONLY,1); | ||
| 751 | |||
| 752 | get(SCREEN,KEY,...) | ||
| 753 | |||
| 754 | To get the value of key KEY of screen SCREEN. Example: | ||
| 755 | |||
| 756 | $wizard->get(NUMBERS,HEADER); | ||
| 757 | $wizard->get(NUMBERS,PROMPTS,ANINT,PROMPT); | ||
| 758 | |||
| 759 | |||
| 760 | wizard([screens]) | ||
| 761 | |||
| 762 | Optional arguments are screens to use. Example: | ||
| 763 | |||
| 764 | $result=$a->wizard() -> processes all screens. | ||
| 765 | $result=$a->wizard("PROCESS") -> processes only screen PROCESS. | ||
| 766 | |||
| 767 | This function starts the wizard. | ||
| 768 | Possible results are: | ||
| 769 | |||
| 770 | ="cancel", the user canceled the wizard; values are not updated. | ||
| 771 | ="finish", the user finished the wizard. | ||
| 772 | ="next", the user finished the wizard and the last prompt | ||
| 773 | has option "NOFINISH". | ||
| 774 | |||
| 775 | =head1 AUTHOR | ||
| 776 | |||
| 777 | Hans Dijkema <hans@oesterholt-dijkema.emailt.nl> | ||
| 778 | |||
| 779 | =cut | ||
| 780 | |||
| 781 | 1; | ||
| 782 | |||