Debian Codegraph @

Debconf/Element/Teletype/Select.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::Element::Teletype::Select - select from a list of values
6
7 =cut
8
9 package Debconf::Element::Teletype::Select;
10 use warnings;
11 use strict;
12 use Debconf::Config;
13 use POSIX qw(ceil);
14 use base qw(Debconf::Element::Select);
15
16 =head1 DESCRIPTION
17
18 This lets the user pick from a number of values.
19
20 =head1 METHODS
21
22 =over 4
23
24 =item expandabbrev
25
26 Pass this method what the user entered, followed by the list of choices.
27 It will try to intuit which one they picked. User can enter the number of
28 an item in the list, or a unique anchored substring of its name (or the
29 full name). If they do, the function returns the choice they selected.
30 If not, it returns the null string.
31
32 =cut
33
34 sub expandabbrev {
35 my $this=shift;
36 my $input=shift;
37 my @choices=@_;
38
39 # Check for (valid) numbers, unless in terse mode, when they were
40 # never shown any numbers to pick from.
41 if (Debconf::Config->terse eq 'false' and
42 $input=~m/^[0-9]+$/ and $input ne '0' and $input <= @choices) {
43 return $choices[$input - 1];
44 }
45
46 # Check for substrings.
47 my @matches=();
48 foreach (@choices) {
49 return $_ if /^\Q$input\E$/;
50 push @matches, $_ if /^\Q$input\E/;
51 }
52 return $matches[0] if @matches == 1;
53
54 if (! @matches) {
55 # Check again, ignoring case.
56 foreach (@choices) {
57 return $_ if /^\Q$input\E$/i;
58 push @matches, $_ if /^\Q$input\E/i;
59 }
60 return $matches[0] if @matches == 1;
61 }
62
63 return '';
64 }
65
66 =item printlist
67
68 Pass a list of all the choices the user has to choose from. Formats and
69 displays the list, using multiple columns if necessary.
70
71 =cut
72
73 sub printlist {
74 my $this=shift;
75 my @choices=@_;
76 my $width=$this->frontend->screenwidth;
77
78 # Figure out the upper bound on the number of columns.
79 my $choice_min=length $choices[0];
80 map { $choice_min = length $_ if length $_ < $choice_min } @choices;
81 my $max_cols=int($width / (2 + length(scalar(@choices)) + 2 + $choice_min)) - 1;
82 $max_cols = $#choices if $max_cols > $#choices;
83
84 my $max_lines;
85 my $num_cols;
86 COLUMN: for ($num_cols = $max_cols; $num_cols >= 0; $num_cols--) {
87 my @col_width;
88 my $total_width;
89
90 $max_lines=ceil(($#choices + 1) / ($num_cols + 1));
91
92 # The last choice should end up in the last column, or there
93 # are still too many columns.
94 next if ceil(($#choices + 1) / $max_lines) - 1 < $num_cols;
95
96 foreach (my $choice=1; $choice <= $#choices + 1; $choice++) {
97 my $choice_length=2
98 + length(scalar(@choices)) + 2
99 + length($choices[$choice - 1]);
100 my $current_col=ceil($choice / $max_lines) - 1;
101 if (! defined $col_width[$current_col] ||
102 $choice_length > $col_width[$current_col]) {
103 $col_width[$current_col]=$choice_length;
104 $total_width=0;
105 map { $total_width += $_ } @col_width;
106 next COLUMN if $total_width > $width;
107 }
108 }
109
110 last;
111 }
112
113 # Finally, generate and print the output.
114 my $line=0;
115 my $max_len=0;
116 my $col=0;
117 my @output=();
118 for (my $choice=0; $choice <= $#choices; $choice++) {
119 $output[$line] .= " ".($choice+1).". " . $choices[$choice];
120 if (length $output[$line] > $max_len) {
121 $max_len = length $output[$line];
122 }
123 if (++$line >= $max_lines) {
124 # Pad existing lines, if necessary.
125 if ($col++ != $num_cols) {
126 for (my $l=0; $l <= $#output; $l++) {
127 $output[$l] .= ' ' x ($max_len - length $output[$l]);
128 }
129 }
130
131 $line=0;
132 $max_len=0;
133 }
134 }
135
136 # Remove unnecessary whitespace at ends of lines.
137 for (@output) { s/\s+$//; }
138
139 map { $this->frontend->display_nowrap($_) } @output;
140 }
141
142 sub show {
143 my $this=shift;
144
145 my $default=$this->translate_default;
146 my @choices=$this->question->choices_split;
147 my @completions=@choices;
148
149 # Print out the question.
150 $this->frontend->display($this->question->extended_description."\n");
151
152 # Change default to number of default in choices list
153 # except in terse mode.
154 if (Debconf::Config->terse eq 'false') {
155 for (my $choice=0; $choice <= $#choices; $choice++) {
156 if ($choices[$choice] eq $default) {
157 $default=$choice + 1;
158 last;
159 }
160 }
161
162 # Rather expensive, and does nothing in terse mode.
163 $this->printlist(@choices);
164 $this->frontend->display("\n");
165
166 # Add choice numbers to completion list in terse mode.
167 push @completions, 1..@choices;
168 }
169
170 # Prompt until a valid answer is entered.
171 my $value;
172 while (1) {
173 $value=$this->frontend->prompt(
174 prompt => $this->question->description,
175 default => $default ? $default : '',
176 completions => [@completions],
177 question => $this->question,
178 );
179 return unless defined $value;
180 $value=$this->expandabbrev($value, @choices);
181 last if $value ne '';
182 }
183 $this->frontend->display("\n");
184 $this->value($this->translate_to_C($value));
185 }
186
187 =back
188
189 =head1 AUTHOR
190
191 Joey Hess <[email protected]>
192
193 =cut
194
195 1