From 5b375768101d93054d42f9326f010d6f8019f541 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 20 May 2011 14:54:15 +0200 Subject: Syntax cleanup --- lib/App/Raps2/UI.pm | 208 +++++++++++++++++++++++++++------------------------- 1 file changed, 110 insertions(+), 98 deletions(-) (limited to 'lib/App/Raps2/UI.pm') diff --git a/lib/App/Raps2/UI.pm b/lib/App/Raps2/UI.pm index 4d7f3d0..b588222 100644 --- a/lib/App/Raps2/UI.pm +++ b/lib/App/Raps2/UI.pm @@ -10,113 +10,61 @@ use Term::ReadLine; our $VERSION = '0.3'; -=head1 NAME - -App::Raps2::UI - App::Raps2 User Interface - -=head1 SYNOPSIS - - my $ui = App::Raps2::UI->new(); - - my $input = $ui->read_line('Say something'); - - my $password = $ui->read_pw('New password', 1); - - $ui->to_clipboard('stuff!'); - -=head1 VERSION - -This manual documents B version 0.3 - -=head1 METHODS - -=over - -=item $ui = App::Raps2::UI->new() - -Returns a new App::Raps2::UI object. - -=cut - sub new { my ($obj) = @_; - my $ref = {}; - $ref->{term_readline} = Term::ReadLine->new('App::Raps2'); - return bless($ref, $obj); -} -=item $ui->list(I<\@item1>, I<\@item2>, I<\@item3>) + my $ref = {}; -Print the list items neatly formatted to stdout. Each I looks like B<[> -I, I B<]>. When B is called for the first time, it will -print the keys as well as the values. + $ref->{term_readline} = Term::ReadLine->new('App::Raps2'); -=cut + return bless( $ref, $obj ); +} sub list { - my ($self, @list) = @_; + my ( $self, @list ) = @_; + my $format = "%-20s %-20s %s\n"; - if (not $self->{list}->{header}) { - printf($format, map { $_->[0] } @list); + if ( not $self->{list}->{header} ) { + printf( $format, map { $_->[0] } @list ); $self->{list}->{header} = 1; } - printf($format, map { $_->[1] // q{} } @list); -} - -=item $ui->read_line(I<$question>, [I<$prefill>]) - -Print "I: " to stdout and wait for the user to input text followed -by a newline. I sets the default content of the answer field. - -Returns the user's reply, excluding the newline. + printf( $format, map { $_->[1] // q{} } @list ); -=cut + return; +} sub read_line { - my ($self, $str, $pre) = @_; + my ( $self, $str, $pre ) = @_; - my $input = $self->{term_readline}->readline("${str}: ", $pre); + my $input = $self->{term_readline}->readline( "${str}: ", $pre ); return $input; } -=item $ui->read_multiline(I<$message>) - -Like B, but repeats I each time the user hits return. -Input is terminated by EOF (Ctrl+D). Returns a string concatenation of all -lines (including newlines). - -=cut - sub read_multiline { - my ($self, $str) = @_; + my ( $self, $str ) = @_; + my $in; say "${str} (^D to quit)"; - while (my $line = $self->read_line('multiline')) { + while ( my $line = $self->read_line('multiline') ) { $in .= "${line}\n"; } + return $in; } -=item $ui->read_pw(I<$message>, I<$verify>) - -Prompt the user for a password. I is displayed, the user's input is -noch echoed. If I is set, the user has to enter the same input twice, -otherwise B dies. Returns the input. - -=cut - sub read_pw { - my ($self, $str, $verify) = @_; - my ($in1, $in2); + my ( $self, $str, $verify ) = @_; + my ( $in1, $in2 ); my $term = POSIX::Termios->new(); + $term->getattr(0); - $term->setlflag($term->getlflag() & ~POSIX::ECHO); - $term->setattr(0, POSIX::TCSANOW); + $term->setlflag( $term->getlflag() & ~POSIX::ECHO ); + $term->setattr( 0, POSIX::TCSANOW ); print "${str}: "; $in1 = readline(STDIN); @@ -128,49 +76,39 @@ sub read_pw { print "\n"; } - $term->setlflag($term->getlflag() | POSIX::ECHO); - $term->setattr(0, POSIX::TCSANOW); + $term->setlflag( $term->getlflag() | POSIX::ECHO ); + $term->setattr( 0, POSIX::TCSANOW ); - if ($verify and $in1 ne $in2) { + if ( $verify and $in1 ne $in2 ) { confess('Input lines did not match'); } chomp $in1; + return $in1; } -=item $ui->to_clipboard(I<$string>) +sub to_clipboard { + my ( $self, $str ) = @_; -Place I in the primary X Clipboard. + open( my $clipboard, q{|-}, 'xclip -l 1' ) + or confess("Failed to execute xclip -l 1: $!"); -=cut + print $clipboard $str; -sub to_clipboard { - my ($self, $str) = @_; + close($clipboard) + or confess("Failed to close pipe to xclip: $!"); - open(my $clipboard, '|-', 'xclip -l 1'); - print $clipboard $str; - close($clipboard); return; } -=item $ui->output(I<\@pair>, I<...>) - -I consinsts of B<[> I, I B<]>. For each I, prints -" key : value" to stdout. - -=cut - sub output { - my ($self, @out) = @_; + my ( $self, @out ) = @_; for my $pair (@out) { - printf( - "%-8s : %s\n", - $pair->[0], - $pair->[1] // q{}, - ); + printf( "%-8s : %s\n", $pair->[0], $pair->[1] // q{}, ); } + return; } @@ -178,12 +116,86 @@ sub output { __END__ +=head1 NAME + +App::Raps2::UI - App::Raps2 User Interface + +=head1 SYNOPSIS + + my $ui = App::Raps2::UI->new(); + + my $input = $ui->read_line('Say something'); + + my $password = $ui->read_pw('New password', 1); + + $ui->to_clipboard('stuff!'); + +=head1 VERSION + +This manual documents B version 0.3 + +=head1 DESCRIPTION + +App::Raps2::UI is used by App::Raps2 to interface with the user, i.e. do input +and output on the terminal. + +=head1 METHODS + +=over + +=item $ui = App::Raps2::UI->new() + +Returns a new App::Raps2::UI object. + +=item $ui->list(I<\@item1>, I<\@item2>, I<\@item3>) + +Print the list items neatly formatted to stdout. Each I looks like B<[> +I, I B<]>. When B is called for the first time, it will +print the keys as well as the values. + +=item $ui->read_line(I<$question>, [I<$prefill>]) + +Print "I: " to stdout and wait for the user to input text followed +by a newline. I sets the default content of the answer field. + +Returns the user's reply, excluding the newline. + +=item $ui->read_multiline(I<$message>) + +Like B, but repeats I each time the user hits return. +Input is terminated by EOF (Ctrl+D). Returns a string concatenation of all +lines (including newlines). + +=item $ui->read_pw(I<$message>, I<$verify>) + +Prompt the user for a password. I is displayed, the user's input is +noch echoed. If I is set, the user has to enter the same input twice, +otherwise B dies. Returns the input. + +=item $ui->to_clipboard(I<$string>) + +Place I in the primary X Clipboard. + +=item $ui->output(I<\@pair>, I<...>) + +I consinsts of B<[> I, I B<]>. For each I, prints +" key : value" to stdout. + =back +=head1 DIAGNOSTICS + +When App::Raps2::UI encounters an error, it uses Carp(3pm)'s B +function to die with a backtrace. + =head1 DEPENDENCIES This module requires B and the B executable. +=head1 BUGS AND LIMITATIONS + +Unknown. + =head1 SEE ALSO App::Raps2(3pm). -- cgit v1.2.3