| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::FrontEnd::Readline - Terminal frontend with readline support |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::FrontEnd::Readline; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Term::ReadLine; |
| 13 |
use Debconf::Gettext; |
| 14 |
use base qw(Debconf::FrontEnd::Teletype); |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
This FrontEnd is for a traditional unix command-line like user interface. |
| 19 |
It features completion if you're using Gnu readline. |
| 20 |
|
| 21 |
=head1 FIELDS |
| 22 |
|
| 23 |
=over 4 |
| 24 |
|
| 25 |
=item readline |
| 26 |
|
| 27 |
An object of type Term::ReadLine, that is used to do the actual prompting. |
| 28 |
|
| 29 |
=item promptdefault |
| 30 |
|
| 31 |
Set if the variant of readline being used is so lame that it cannot display |
| 32 |
defaults, so the default must be part of the prompt instead. |
| 33 |
|
| 34 |
=back |
| 35 |
|
| 36 |
=head1 METHODS |
| 37 |
|
| 38 |
=over 4 |
| 39 |
|
| 40 |
=cut |
| 41 |
|
| 42 |
sub init { |
| 43 |
my $this=shift; |
| 44 |
|
| 45 |
$this->SUPER::init(@_); |
| 46 |
|
| 47 |
$Term::ReadLine::termcap_nowarn = 1; # Turn off stupid termcap warning. |
| 48 |
$this->readline(Term::ReadLine->new('debconf')); |
| 49 |
$this->readline->ornaments(1); |
| 50 |
|
| 51 |
if (-p STDOUT && -p STDERR) { # make readline play nice with buffered stdout |
| 52 |
$this->readline->newTTY(*STDIN, *STDOUT); |
| 53 |
} |
| 54 |
|
| 55 |
if (Term::ReadLine->ReadLine =~ /::Gnu$/) { |
| 56 |
# Well, emacs shell buffer has some annoying interactions |
| 57 |
# with Term::ReadLine::GNU. It's not worth the pain. |
| 58 |
if (exists $ENV{TERM} && $ENV{TERM} =~ /emacs/i) { |
| 59 |
die gettext("Term::ReadLine::GNU is incompatable with emacs shell buffers.")."\n"; |
| 60 |
} |
| 61 |
|
| 62 |
# Ctrl-u or pageup backs up, while ctrl-v or pagedown moves |
| 63 |
# forward. These key bindings and history completion are only |
| 64 |
# supported by Gnu ReadLine. |
| 65 |
$this->readline->add_defun('previous-question', |
| 66 |
sub { |
| 67 |
if ($this->capb_backup) { |
| 68 |
$this->_skip(1); |
| 69 |
$this->_direction(-1); |
| 70 |
# Tell readline to quit. Yes, |
| 71 |
# this is really the best way. <sigh> |
| 72 |
$this->readline->stuff_char(ord "\n"); |
| 73 |
} |
| 74 |
else { |
| 75 |
$this->readline->ding; |
| 76 |
} |
| 77 |
}, ord "\cu"); |
| 78 |
# This is only defined so people have a readline function |
| 79 |
# they can remap if they desire. |
| 80 |
$this->readline->add_defun('next-question', |
| 81 |
sub { |
| 82 |
if ($this->capb_backup) { |
| 83 |
# Just move onward. |
| 84 |
$this->readline->stuff_char(ord "\n"); |
| 85 |
} |
| 86 |
}, ord "\cv"); |
| 87 |
# FIXME: I cannot figure out a better way to feed in a key |
| 88 |
# sequence -- someone help me. |
| 89 |
$this->readline->parse_and_bind('"\e[5~": previous-question'); |
| 90 |
$this->readline->parse_and_bind('"\e[6~": next-question'); |
| 91 |
$this->capb('backup'); |
| 92 |
} |
| 93 |
|
| 94 |
# Figure out which readline module has been loaded, to tell if |
| 95 |
# prompts must include defaults or not. |
| 96 |
if (Term::ReadLine->ReadLine =~ /::Stub$/) { |
| 97 |
$this->promptdefault(1); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
=item elementtype |
| 102 |
|
| 103 |
This frontend uses the same elements as does the Teletype frontend. |
| 104 |
|
| 105 |
=cut |
| 106 |
|
| 107 |
sub elementtype { |
| 108 |
return 'Teletype'; |
| 109 |
} |
| 110 |
|
| 111 |
=item go |
| 112 |
|
| 113 |
Overrides the default go method with something a little more sophisticated. |
| 114 |
This frontend supports backing up, but it doesn't support displaying blocks of |
| 115 |
questions at the same time. So backing up from one block to the next is |
| 116 |
taken care of for us, but we have to handle movement within a block. This |
| 117 |
includes letting the user move back and forth from one question to the next |
| 118 |
in the block, which this method supports. |
| 119 |
|
| 120 |
The really gritty part is that it keeps track of whether the user moves all |
| 121 |
the way out of the current block and back, in which case they have to start |
| 122 |
at the _last_ question of the previous block, not the first. |
| 123 |
|
| 124 |
=cut |
| 125 |
|
| 126 |
sub go { |
| 127 |
my $this=shift; |
| 128 |
|
| 129 |
# First, take care of any noninteractive elements in the block. |
| 130 |
foreach my $element (grep { ! $_->visible } @{$this->elements}) { |
| 131 |
my $value=$element->show; |
| 132 |
return if $this->backup && $this->capb_backup; |
| 133 |
$element->question->value($value); |
| 134 |
} |
| 135 |
|
| 136 |
# Now we only have to deal with the interactive elements. |
| 137 |
my @elements=grep { $_->visible } @{$this->elements}; |
| 138 |
unless (@elements) { |
| 139 |
$this->_didbackup(''); |
| 140 |
return 1; |
| 141 |
} |
| 142 |
|
| 143 |
# Figure out where to start, based on if we backed up to get here. |
| 144 |
my $current=$this->_didbackup ? $#elements : 0; |
| 145 |
|
| 146 |
# Loop through the elements from starting point until we move |
| 147 |
# out of either side. The property named "_direction" will indicate |
| 148 |
# which direction to go next; it is changed elsewhere. |
| 149 |
$this->_direction(1); |
| 150 |
for (; $current > -1 && $current < @elements; $current += $this->_direction) { |
| 151 |
my $value=$elements[$current]->show; |
| 152 |
if (not defined $value) { |
| 153 |
$this->_didbackup(1); |
| 154 |
return; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if ($current < 0) { |
| 159 |
$this->_didbackup(1); |
| 160 |
return; |
| 161 |
} |
| 162 |
else { |
| 163 |
$this->_didbackup(''); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
=item prompt |
| 169 |
|
| 170 |
Prompts the user for input, and returns it. If a title is pending, |
| 171 |
it will be displayed before the prompt. |
| 172 |
|
| 173 |
This function will return undef if the user opts to skip the question |
| 174 |
(by backing up or moving on to the next question). Anything that uses this |
| 175 |
function should catch that and handle it, probably by exiting any |
| 176 |
read/validate loop it is in. |
| 177 |
|
| 178 |
The function uses named parameters. |
| 179 |
|
| 180 |
Completion among available choices is supported. For this to work, if |
| 181 |
a reference to an array of all possible completions is passed in. |
| 182 |
|
| 183 |
=cut |
| 184 |
|
| 185 |
sub prompt { |
| 186 |
my $this=shift; |
| 187 |
my %params=@_; |
| 188 |
my $prompt=$params{prompt}." "; |
| 189 |
my $default=$params{default}; |
| 190 |
my $noshowdefault=$params{noshowdefault}; |
| 191 |
my $completions=$params{completions}; |
| 192 |
|
| 193 |
if ($completions) { |
| 194 |
# Set up completion function (a closure). |
| 195 |
my @matches; |
| 196 |
$this->readline->Attribs->{completion_entry_function} = sub { |
| 197 |
my $text=shift; |
| 198 |
my $state=shift; |
| 199 |
|
| 200 |
if ($state == 0) { |
| 201 |
@matches=(); |
| 202 |
foreach (@{$completions}) { |
| 203 |
push @matches, $_ if /^\Q$text\E/i; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return pop @matches; |
| 208 |
}; |
| 209 |
} |
| 210 |
else { |
| 211 |
$this->readline->Attribs->{completion_entry_function} = undef; |
| 212 |
} |
| 213 |
|
| 214 |
if (exists $params{completion_append_character}) { |
| 215 |
$this->readline->Attribs->{completion_append_character}=$params{completion_append_character}; |
| 216 |
} |
| 217 |
else { |
| 218 |
$this->readline->Attribs->{completion_append_character}=''; |
| 219 |
} |
| 220 |
|
| 221 |
$this->linecount(0); |
| 222 |
my $ret; |
| 223 |
$this->_skip(0); |
| 224 |
if (! $noshowdefault) { |
| 225 |
$ret=$this->readline->readline($prompt, $default); |
| 226 |
} |
| 227 |
else { |
| 228 |
$ret=$this->readline->readline($prompt); |
| 229 |
} |
| 230 |
$this->display_nowrap("\n"); |
| 231 |
return if $this->_skip; |
| 232 |
$this->_direction(1); |
| 233 |
$this->readline->addhistory($ret); |
| 234 |
return $ret; |
| 235 |
} |
| 236 |
|
| 237 |
=item prompt_password |
| 238 |
|
| 239 |
Safely prompts for a password; arguments are the same as for prompt. |
| 240 |
|
| 241 |
=cut |
| 242 |
|
| 243 |
sub prompt_password { |
| 244 |
my $this=shift; |
| 245 |
my %params=@_; |
| 246 |
|
| 247 |
if (Term::ReadLine->ReadLine =~ /::Perl$/) { |
| 248 |
# I hate this library. Sigh. It always echos, |
| 249 |
# so it is unusable here. Use Teletype's prompt_password. |
| 250 |
return $this->SUPER::prompt_password(%params); |
| 251 |
} |
| 252 |
|
| 253 |
# Kill default: not a good idea for passwords. |
| 254 |
delete $params{default}; |
| 255 |
# Force echoing off. |
| 256 |
system('stty -echo 2>/dev/null'); |
| 257 |
my $ret=$this->prompt(@_, noshowdefault => 1, completions => []); |
| 258 |
system('stty sane 2>/dev/null'); |
| 259 |
print "\n"; |
| 260 |
return $ret; |
| 261 |
} |
| 262 |
|
| 263 |
=back |
| 264 |
|
| 265 |
=head1 AUTHOR |
| 266 |
|
| 267 |
Joey Hess <[email protected]> |
| 268 |
|
| 269 |
=cut |
| 270 |
|
| 271 |
1 |