summaryrefslogtreecommitdiffstats
path: root/perl-install/interactive_stdio.pm
blob: 40dcdbb7c83a4fe2af62c042e6e1a220b05c5346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package interactive_stdio;

use diagnostics;
use strict;
use vars qw(@ISA);

@ISA = qw(interactive);

use interactive;
use common qw(:common);

1;

sub readln {
    my $l = <STDIN>;
    chomp $l;
    $l;
}

sub check_it {
    my ($i, $n) = @_;
    $i =~ /^\s*\d+\s*$/ && 1 <= $i && $i <= $n    
}

sub ask_from_listW {
    my ($o, $title, $messages, $list, $def) = @_;
    my $i;
    print map { "$_\n" } @$messages;

    if (@$list < 10 && sum(map { length $_ } @$list) < 50) {
	my @l;
	do {
	    if (defined $i) {
		@l ? print _("Ambiguity (%s) be more precise\n", join(", ", @l)) :
		     print _("Bad choice, try again\n");
	    }
	    @$list == 1 ? print @$list :
	                  print join("/", @$list), _(" ? (default %s) ", $def);
	    $i = readln() || $def;
	    @l = grep { /^$i/ } @$list;
	} until (@l == 1);
	$l[0];
    } else {
	my $n = 0; foreach (@$list) { 
	    $n++;
	    $def eq $_ and $def = $n;
	    print "$n: $_\n"; 
	}
	do {
	    defined $i and print _("Bad choice, try again\n");
	    print _("Your choice? (default %s) ", $def);
	    $i = readln() || $def;
	} until (check_it($i, $n));
	$list->[$i - 1];
    }
}

sub ask_many_from_listW {
    my ($o, $title, $messages, $list, $default) = @_;
    my @defaults;
    print map { "$_\n" } @$messages;
    my $n = 0; foreach (@$list) { 
	$n++; 
	print "$n: $_\n"; 
	push @defaults, $n if $default->[$n - 1];
    }
    my $i;
    TRY_AGAIN:
    defined $i and print _("Bad choice, try again\n");
    print _("Your choice? (default %s  enter `none' for none) ", join(',', @defaults));
    $i = readln();
    my @t = split ',', $i;
    foreach (@t) { check_it($_, $n) or goto TRY_AGAIN }

    my @rr = (0) x @$list;
    $rr[$_ - 1] = 1 foreach @t;
    @rr;
}