summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-07-14 17:49:15 +0200
committerDaniel Friesel <derf@finalrewind.org>2011-07-14 17:49:15 +0200
commitb5152ca00cb52d5a1c6b755acd5f3c81c240a92a (patch)
tree74271650e0502cdc46c14fdae00c8f6503ad5b76
parent7b5b6c7ad849a6a2f68df8ac1b717effeebae9bd (diff)
Make platforms argument optional (display all available if left out)
-rwxr-xr-xbin/db-fakedisplay77
1 files 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] <station> <platforms ...>';
+ say 'Usage: db-fakedisplay [-t template] <station> [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<db-fakedisplay> I<station> I<platforms ...>
+B<db-fakedisplay> [-t I<template>] I<station> [I<platforms ...>]
=head1 VERSION
@@ -107,6 +113,9 @@ B<db-fakedisplay> outputs HTML showing the next departure for every
I<platform> on I<station> on stdout. The HTML is styled to look like the LCDs
installed on most (major) stations.
+If no I<platforms> were specified, B<db-fakedisplay> shows all platforms for
+which departures are reported.
+
=head1 OPTIONS
=over