5
Debconf::FrontEnd::Readline - Terminal frontend with readline support
9
package Debconf::FrontEnd::Readline;
13
use base qw(Debconf::FrontEnd::Teletype);
17
This FrontEnd is for a traditional unix command-line like user interface.
18
It features completion if you're using Gnu readline.
26
An object of type Term::ReadLine, that is used to do the actual prompting.
30
Set if the varient of readline being used is so lame that it cannot display
31
defaults, so the default must be part of the prompt instead.
44
$this->SUPER::init(@_);
46
# Yeah, you need a controlling tty. Make sure there is one.
47
open(TESTTY, "/dev/tty") || die gettext("This frontend requires a controlling tty.")."\n";
50
$Term::ReadLine::termcap_nowarn = 1; # Turn off stupid termcap warning.
51
$this->readline(Term::ReadLine->new('debconf'));
52
$this->readline->ornaments(1);
54
if (Term::ReadLine->ReadLine =~ /::Gnu$/) {
55
# Well, emacs shell buffer has some annoying interactions
56
# with Term::ReadLine::GNU. It's not worth the pain.
57
if (exists $ENV{TERM} && $ENV{TERM} =~ /emacs/i) {
58
die gettext("Term::ReadLine::GNU is incompatable with emacs shell buffers.")."\n";
61
# Ctrl-u or pageup backs up, while ctrl-v or pagedown moves
62
# forward. These key bindings and history completion are only
63
# supported by Gnu ReadLine.
64
$this->readline->add_defun('previous-question',
66
if ($this->capb_backup) {
68
$this->_direction(-1);
69
# Tell readline to quit. Yes,
70
# this is really the best way. <sigh>
71
$this->readline->stuff_char(ord "\n");
74
$this->readline->ding;
77
# This is only defined so people have a readline function
78
# they can remap if they desire.
79
$this->readline->add_defun('next-question',
81
if ($this->capb_backup) {
83
$this->readline->stuff_char(ord "\n");
86
# FIXME: I cannot figure out a better way to feed in a key
87
# sequence -- someone help me.
88
$this->readline->parse_and_bind('"\e[5~": previous-question');
89
$this->readline->parse_and_bind('"\e[6~": next-question');
90
$this->capb('backup');
93
# Figure out which readline module has been loaded, to tell if
94
# prompts must include defaults or not.
95
if (Term::ReadLine->ReadLine =~ /::Stub$/) {
96
$this->promptdefault(1);
102
This frontend uses the same elements as does the Teletype frontend.
112
Overrides the default go method with something a little more sophisticated.
113
This frontend supports backing up, but it doesn't support displaying blocks of
114
questions at the same time. So backing up from one block to the next is
115
taken care of for us, but we have to handle movement within a block. This
116
includes letting the user move back and forth from one question to the next
117
in the block, which this method supports.
119
The really gritty part is that it keeps track of whether the user moves all
120
the way out of the current block and back, in which case they have to start
121
at the _last_ question of the previous block, not the first.
128
# First, take care of any noninteractive elements in the block.
129
foreach my $element (grep ! $_->visible, @{$this->elements}) {
130
my $value=$element->show;
131
return if $this->backup && $this->capb_backup;
132
$element->question->value($value);
135
# Now we only have to deal with the interactive elements.
136
my @elements=grep $_->visible, @{$this->elements};
138
$this->_didbackup('');
142
# Figure out where to start, based on if we backed up to get here.
143
my $current=$this->_didbackup ? $#elements : 0;
145
# Loop through the elements from starting point until we move
146
# out of either side. The property named "_direction" will indicate
147
# which direction to go next; it is changed elsewhere.
148
$this->_direction(1);
149
for (; $current > -1 && $current < @elements; $current += $this->_direction) {
150
my $value=$elements[$current]->show;
154
$this->_didbackup(1);
158
$this->_didbackup('');
165
Prompts the user for input, and returns it. If a title is pending,
166
it will be displayed before the prompt.
168
This function will return undef if the user opts to skip the question
169
(by backing up or moving on to the next question). Anything that uses this
170
function should catch that and handle it, probably by exiting any
171
read/validate loop it is in.
173
The function uses named parameters.
175
Completion amoung available choices is supported. For this to work, if
176
a reference to an array of all possible completions is passed in.
183
my $prompt=$params{prompt}." ";
184
my $default=$params{default};
185
my $noshowdefault=$params{noshowdefault};
186
my $completions=$params{completions};
189
# Set up completion function (a closure).
191
$this->readline->Attribs->{completion_entry_function} = sub {
197
foreach (@{$completions}) {
198
push @matches, $_ if /^\Q$text\E/i;
206
$this->readline->Attribs->{completion_entry_function} = undef;
209
if (exists $params{completion_append_character}) {
210
$this->readline->Attribs->{completion_append_character}=$params{completion_append_character};
213
$this->readline->Attribs->{completion_append_character}='';
219
if (! $noshowdefault) {
220
$ret=$this->readline->readline($prompt, $default);
223
$ret=$this->readline->readline($prompt);
225
$this->display_nowrap("\n");
226
return if $this->_skip;
227
$this->_direction(1);
228
$this->readline->addhistory($ret);
232
=item prompt_password
234
Safely prompts for a password; arguments are the same as for prompt.
238
sub prompt_password {
242
if (Term::ReadLine->ReadLine =~ /::Perl$/) {
243
# I hate this library. Sigh. It always echos,
244
# so it is unusable here. Use Teletype's prompt_password.
245
return $this->SUPER::prompt_password(%params);
248
# Kill default: not a good idea for passwords.
249
delete $params{default};
251
system('stty -echo 2>/dev/null');
252
my $ret=$this->prompt(@_, noshowdefault => 1, completions => []);
253
system('stty sane 2>/dev/null');
262
Joey Hess <joeyh@debian.org>