From b5152ca00cb52d5a1c6b755acd5f3c81c240a92a Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 14 Jul 2011 17:49:15 +0200 Subject: Make platforms argument optional (display all available if left out) --- bin/db-fakedisplay | 77 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/bin/db-fakedisplay b/bin/db-fakedisplay index 7724473..5350c05 100755 --- a/bin/db-fakedisplay +++ b/bin/db-fakedisplay @@ -12,41 +12,37 @@ use Travel::Status::DE::DeutscheBahn; our $VERSION = '0.00'; -my ($station, @platforms); +my @params; +my ( $station, @platforms ); my $template_file; my $template; GetOptions( - 'h|help' => sub { show_help(0) }, + 'h|help' => sub { show_help(0) }, 't|template=s' => \&handle_template, 'V|version' => sub { say "db-fakedisplay version ${VERSION}"; exit 0 }, ) or show_help(1); -if (@ARGV < 2) { +( $station, @platforms ) = @ARGV; +$template_file //= dist_file( 'db-fakedisplay', 'single-lcd.html' ); +$template = HTML::Template->new( filename => $template_file ); + +if ( not defined $station ) { show_help(1); } -($station, @platforms) = @ARGV; -$template_file //= dist_file('db-fakedisplay', 'single-lcd.html'); -$template = HTML::Template->new( filename => $template_file); - -my $status = Travel::Status::DE::DeutscheBahn->new( - station => $station -); - -my @params; +my $status = Travel::Status::DE::DeutscheBahn->new( station => $station ); sub handle_template { - my (undef, $template_name) = @_; + my ( undef, $template_name ) = @_; - if (-e $template_name) { + if ( -e $template_name ) { $template_file = $template_name; } else { - $template_file = dist_file('db-fakedisplay', - $template_name); + $template_file = dist_file( 'db-fakedisplay', $template_name ); } return; @@ -55,35 +51,45 @@ sub handle_template { sub show_help { my ($exit_status) = @_; - say 'Usage: db-fakedisplay [-t template] '; + say 'Usage: db-fakedisplay [-t template] [platforms ...]'; say 'See also: man db-fakedisplay'; exit $exit_status; } +if ( not @platforms ) { + for my $result ( $status->results ) { + if ( $result->platform ~~ \@platforms ) { + next; + } + push( @platforms, $result->platform ); + } + @platforms = sort { $a <=> $b } @platforms; +} + for my $platform (@platforms) { - my $info = first { $_->platform =~ m{ ^ $platform (?: \s | $ )}x } $status->results; + my $result = first { $_->platform =~ m{ ^ $platform (?: \s | $ )}x } + $status->results; - if (not defined $info) { - push(@params, { - platform => $platform - }); + if ( not defined $result ) { + push( @params, { platform => $platform } ); next; } - push(@params, { - time => $info->time, - train => $info->train, - via => [ map { { stop => $_ } } $info->route_interesting(3) ], - destination => $info->destination, - platform => (split(/ /, $info->platform))[0], - info => $info->info, - }); + push( + @params, + { + time => $result->time, + train => $result->train, + via => [ map { { stop => $_ } } $result->route_interesting(3) ], + destination => $result->destination, + platform => ( split( / /, $result->platform ) )[0], + info => $result->info, + } + ); } -$template->param( - platform => \@params, -); +$template->param( platform => \@params, ); say $template->output; @@ -95,7 +101,7 @@ db-fakedisplay - Show train departures, as seen on the displays on most main sta =head1 SYNOPSIS -B I I +B [-t I