Commit b5152ca0 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Make platforms argument optional (display all available if left out)

parent 7b5b6c7a
Loading
Loading
Loading
Loading
+43 −34
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ use Travel::Status::DE::DeutscheBahn;

our $VERSION = '0.00';

my @params;
my ( $station, @platforms );
my $template_file;
my $template;
@@ -24,19 +25,15 @@ GetOptions(

) or show_help(1);

if (@ARGV < 2) {
	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
);
if ( not defined $station ) {
	show_help(1);
}

my @params;
my $status = Travel::Status::DE::DeutscheBahn->new( station => $station );

sub handle_template {
	my ( undef, $template_name ) = @_;
@@ -45,8 +42,7 @@ sub handle_template {
		$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