Debian Codegraph @

Debconf/Element/Teletype/Multiselect.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::Element::Teletype::Multiselect - select multiple items
6
7 =cut
8
9 package Debconf::Element::Teletype::Multiselect;
10 use warnings;
11 use strict;
12 use Debconf::Gettext;
13 use Debconf::Config;
14 use base qw(Debconf::Element::Multiselect Debconf::Element::Teletype::Select);
15
16 =head1 DESCRIPTION
17
18 This lets the user select multiple items from a list of values, using a
19 teletype interface. (This is hard to do in plain text, and the UI I have made
20 isn't very intuitive. Better UI designs welcomed.)
21
22 =cut
23
24 =head1 METHODS
25
26 =over 4
27
28 =item expand_ranges
29
30 Expand ranges of numbers input by the user.
31
32 expand_ranges(qw(1 foo 4-6 bar));
33 => qw(1 foo 4 5 6 bar)
34
35 =back
36
37 =cut
38
39 sub expand_ranges {
40 my @ranges = @_;
41 my @accumulator;
42 for my $item (@ranges) {
43 if ($item =~ /\A(\d+)-(\d+)\Z/) {
44 my ($begin, $end) = ($1, $2);
45 for (my $i = $begin; $i <= $end; $i++) {
46 push @accumulator, $i;
47 }
48 }
49 else {
50 push @accumulator, $item;
51 }
52 }
53 return @accumulator;
54 }
55
56 sub show {
57 my $this=shift;
58
59 my @selected;
60 my $none_of_the_above=gettext("none of the above");
61
62 my @choices=$this->question->choices_split;
63 my %value = map { $_ => 1 } $this->translate_default;
64 if ($this->frontend->promptdefault && $this->question->value ne '') {
65 push @choices, $none_of_the_above;
66 }
67 my @completions=@choices;
68 my $i=1;
69 my %choicenum=map { $_ => $i++ } @choices;
70
71 # Print out the question.
72 $this->frontend->display($this->question->extended_description."\n");
73
74 # If this is not terse mode, we want to print out choices, and
75 # add numbers to the completions, and use numbers in the default
76 # prompt.
77 my $default;
78 if (Debconf::Config->terse eq 'false') {
79 $this->printlist(@choices);
80 $this->frontend->display("\n(".gettext("Enter the items or ranges you want to select, separated by spaces.").")\n");
81 push @completions, 1..@choices;
82 $default=join(" ", map { $choicenum{$_} }
83 grep { $value{$_} } @choices);
84 }
85 else {
86 $default=join(" ", grep { $value{$_} } @choices);
87 }
88
89 # Prompt until a valid answer is entered.
90 while (1) {
91 $_=$this->frontend->prompt(
92 prompt => $this->question->description,
93 default => $default,
94 completions => [@completions],
95 completion_append_character => " ",
96 question => $this->question,
97 );
98 return unless defined $_;
99
100 # Split up what they entered. They can separate items
101 # with whitespace or commas.
102 @selected=split(/[ ,]+/, $_);
103
104 @selected=expand_ranges(@selected);
105
106 # Expand what they entered.
107 @selected=map { $this->expandabbrev($_, @choices) } @selected;
108
109 # Test to make sure everything they entered expanded ok.
110 # If not loop.
111 next if grep { $_ eq '' } @selected;
112
113 # Make sure that they didn't select "none of the above"
114 # along with some other item. That's undefined, so don't
115 # accept it.
116 if ($#selected > 0) {
117 map { next if $_ eq $none_of_the_above } @selected;
118 }
119
120 last;
121 }
122
123 $this->frontend->display("\n");
124
125 if (defined $selected[0] && $selected[0] eq $none_of_the_above) {
126 $this->value('');
127 }
128 else {
129 # Make sure that no item was entered twice. If so, remove
130 # the duplicate.
131 my %selected=map { $_ => 1 } @selected;
132
133 # Translate back to C locale, and join the list.
134 $this->value(join(', ', $this->order_values(
135 map { $this->translate_to_C($_) }
136 keys %selected)));
137 }
138 }
139
140 1