| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::Element::Teletype::Boolean - Yes/No question |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::Element::Teletype::Boolean; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Gettext; |
| 13 |
use base qw(Debconf::Element); |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
This is a yes or no question, presented to the user using a teletype |
| 18 |
interface. |
| 19 |
|
| 20 |
=head1 METHODS |
| 21 |
|
| 22 |
=over 4 |
| 23 |
|
| 24 |
=cut |
| 25 |
|
| 26 |
sub show { |
| 27 |
my $this=shift; |
| 28 |
|
| 29 |
my $y=gettext("yes"); |
| 30 |
my $n=gettext("no"); |
| 31 |
|
| 32 |
# Display the question's long desc first. |
| 33 |
$this->frontend->display($this->question->extended_description."\n"); |
| 34 |
|
| 35 |
my $default=''; |
| 36 |
$default=$this->question->value if defined $this->question->value; |
| 37 |
if ($default eq 'true') { |
| 38 |
$default=$y; |
| 39 |
} |
| 40 |
elsif ($default eq 'false') { |
| 41 |
$default=$n; |
| 42 |
} |
| 43 |
|
| 44 |
# If this is not terse mode, we want to print out choices. Since we |
| 45 |
# only have two choices, though, we use a compact display format. |
| 46 |
my $description=$this->question->description; |
| 47 |
if (Debconf::Config->terse eq 'false') { |
| 48 |
$description.=" [$y/$n]"; |
| 49 |
} |
| 50 |
|
| 51 |
my $value=''; |
| 52 |
|
| 53 |
while (1) { |
| 54 |
# Prompt for input. |
| 55 |
$_=$this->frontend->prompt( |
| 56 |
default => $default, |
| 57 |
completions => [$y, $n], |
| 58 |
prompt => $description, |
| 59 |
question => $this->question, |
| 60 |
); |
| 61 |
return unless defined $_; |
| 62 |
|
| 63 |
# Validate the input. Check to see if the first letter |
| 64 |
# matches the start of "yes" or "no". Internationalization |
| 65 |
# makes this harder, because there may be some language where |
| 66 |
# "yes" and "no" both start with the same letter. |
| 67 |
if (substr($y, 0, 1) ne substr($n, 0, 1)) { |
| 68 |
# When possible, trim to first letters. |
| 69 |
$y=substr($y, 0, 1); |
| 70 |
$n=substr($n, 0, 1); |
| 71 |
} |
| 72 |
# I suppose this would break in a language where $y is a |
| 73 |
# anchored substring of $n. Any such language should be taken |
| 74 |
# out and shot. TODO: I hear Chinese actually needs this.. |
| 75 |
if (/^\Q$y\E/i) { |
| 76 |
$value='true'; |
| 77 |
last; |
| 78 |
} |
| 79 |
elsif (/^\Q$n\E/i) { |
| 80 |
$value='false'; |
| 81 |
last; |
| 82 |
} |
| 83 |
|
| 84 |
# As a fallback, check for unlocalised y or n. Perhaps the |
| 85 |
# question was not fully translated and the user chose to |
| 86 |
# answer in English. |
| 87 |
if (/^y/i) { |
| 88 |
$value='true'; |
| 89 |
last; |
| 90 |
} |
| 91 |
elsif (/^n/i) { |
| 92 |
$value='false'; |
| 93 |
last; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$this->frontend->display("\n"); |
| 98 |
$this->value($value); |
| 99 |
} |
| 100 |
|
| 101 |
=back |
| 102 |
|
| 103 |
=head1 AUTHOR |
| 104 |
|
| 105 |
Joey Hess <[email protected]> |
| 106 |
|
| 107 |
=cut |
| 108 |
|
| 109 |
1 |