| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::FrontEnd::Teletype - FrontEnd for any teletype |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::FrontEnd::Teletype; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Encoding qw(width wrap); |
| 13 |
use Debconf::Gettext; |
| 14 |
use Debconf::Config; |
| 15 |
use base qw(Debconf::FrontEnd::ScreenSize); |
| 16 |
|
| 17 |
=head1 DESCRIPTION |
| 18 |
|
| 19 |
This is a very basic frontend that should work on any terminal, from a real |
| 20 |
teletype on up. It also serves as the parent for the Readline frontend. |
| 21 |
|
| 22 |
=head1 FIELDS |
| 23 |
|
| 24 |
=over 4 |
| 25 |
|
| 26 |
=item linecount |
| 27 |
|
| 28 |
How many lines have been displayed since the last pause. |
| 29 |
|
| 30 |
=back |
| 31 |
|
| 32 |
=head1 METHODS |
| 33 |
|
| 34 |
=over 4 |
| 35 |
|
| 36 |
=cut |
| 37 |
|
| 38 |
sub init { |
| 39 |
my $this=shift; |
| 40 |
|
| 41 |
$this->SUPER::init(@_); |
| 42 |
$this->interactive(1); |
| 43 |
$this->linecount(0); |
| 44 |
|
| 45 |
# Yeah, you need a controlling tty. Make sure there is one. |
| 46 |
-t STDIN || die gettext("This frontend requires a controlling tty.")."\n"; |
| 47 |
} |
| 48 |
|
| 49 |
=item display |
| 50 |
|
| 51 |
Displays text wrapped to fit on the screen. If too much text is displayed at |
| 52 |
once, it will page it. If a title has been set and has not yet been displayed, |
| 53 |
displays it first. |
| 54 |
|
| 55 |
The important flag, if set, will make it always be shown. If unset, the |
| 56 |
text will not be shown in terse mode, |
| 57 |
|
| 58 |
=cut |
| 59 |
|
| 60 |
sub display { |
| 61 |
my $this=shift; |
| 62 |
my $text=shift; |
| 63 |
|
| 64 |
$Debconf::Encoding::columns=$this->screenwidth; |
| 65 |
$this->display_nowrap(wrap('','',$text)); |
| 66 |
} |
| 67 |
|
| 68 |
=item display_nowrap |
| 69 |
|
| 70 |
Display text, paging if necessary. If a title has been set and has not |
| 71 |
yet been displayed, displays it first. |
| 72 |
|
| 73 |
=cut |
| 74 |
|
| 75 |
sub display_nowrap { |
| 76 |
my $this=shift; |
| 77 |
my $text=shift; |
| 78 |
|
| 79 |
# Terse mode skips all this stuff. |
| 80 |
return if Debconf::Config->terse eq 'true'; |
| 81 |
|
| 82 |
# Silly split elides trailing null matches. |
| 83 |
my @lines=split(/\n/, $text); |
| 84 |
push @lines, "" if $text=~/\n$/; |
| 85 |
|
| 86 |
# Add to the display any pending title. |
| 87 |
my $title=$this->title; |
| 88 |
if (length $title) { |
| 89 |
unshift @lines, $title, ('-' x width $title), ''; |
| 90 |
$this->title(''); |
| 91 |
} |
| 92 |
|
| 93 |
foreach (@lines) { |
| 94 |
# If we had to guess at the screenheight, don't bother |
| 95 |
# ever pausing; for all I know this is some real teletype |
| 96 |
# with an infinite height "screen" of fan-fold paper.. |
| 97 |
# Also don't bother pausing if the screenheight is 2 rows or |
| 98 |
# less, since that would leave no space to display the actual |
| 99 |
# text. |
| 100 |
if (! $this->screenheight_guessed && $this->screenheight > 2 && |
| 101 |
$this->linecount($this->linecount+1) > $this->screenheight - 2) { |
| 102 |
my $resp=$this->prompt( |
| 103 |
prompt => '['.gettext("More").']', |
| 104 |
default => '', |
| 105 |
completions => [], |
| 106 |
); |
| 107 |
# Hack, there's not a good UI to suggest this is |
| 108 |
# allowed, but you can enter 'q' to break out of |
| 109 |
# the pager. |
| 110 |
if (defined $resp && $resp eq 'q') { |
| 111 |
last; |
| 112 |
} |
| 113 |
} |
| 114 |
print "$_\n"; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
=item prompt |
| 119 |
|
| 120 |
Prompts the user for input, and returns it. If a title is pending, |
| 121 |
it will be displayed before the prompt. |
| 122 |
|
| 123 |
This function will return undef if the user opts to skip the question |
| 124 |
(by backing up or moving on to the next question). Anything that uses this |
| 125 |
function should catch that and handle it, probably by exiting any |
| 126 |
read/validate loop it is in. |
| 127 |
|
| 128 |
The function uses named parameters. |
| 129 |
|
| 130 |
=cut |
| 131 |
|
| 132 |
sub prompt { |
| 133 |
my $this=shift; |
| 134 |
my %params=@_; |
| 135 |
|
| 136 |
$this->linecount(0); |
| 137 |
local $|=1; |
| 138 |
print "$params{prompt} "; |
| 139 |
my $ret=<STDIN>; |
| 140 |
chomp $ret if defined $ret; |
| 141 |
$this->display_nowrap("\n"); |
| 142 |
return $ret; |
| 143 |
} |
| 144 |
|
| 145 |
=item prompt_password |
| 146 |
|
| 147 |
Safely prompts for a password; arguments are the same as for prompt. |
| 148 |
|
| 149 |
=cut |
| 150 |
|
| 151 |
sub prompt_password { |
| 152 |
my $this=shift; |
| 153 |
my %params=@_; |
| 154 |
|
| 155 |
# Kill default: not a good idea for passwords. |
| 156 |
delete $params{default}; |
| 157 |
# Force echoing off. |
| 158 |
system('stty -echo 2>/dev/null'); |
| 159 |
# Always use this class's version of prompt here, not whatever |
| 160 |
# children put in its place. Only this one is guarenteed to not |
| 161 |
# echo, and work properly for password prompting. |
| 162 |
my $ret=$this->Debconf::FrontEnd::Teletype::prompt(%params); |
| 163 |
system('stty sane 2>/dev/null'); |
| 164 |
return $ret; |
| 165 |
} |
| 166 |
|
| 167 |
=back |
| 168 |
|
| 169 |
=head1 AUTHOR |
| 170 |
|
| 171 |
Joey Hess <[email protected]> |
| 172 |
|
| 173 |
=cut |
| 174 |
|
| 175 |
1 |