summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2023-11-17 19:55:37 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2023-11-17 19:55:37 +0100
commit6e861dc5a167ba7c82e6f434258e891cf35bb361 (patch)
treedbc8ce8af00d7f411f8b23179774c0ec97ac0896 /bin
parent6ca0602ecf0bbbbbb9f2fc9e91ef43c45ec7361d (diff)
hafas: calculate required width of delay field per connection
Diffstat (limited to 'bin')
-rwxr-xr-xbin/hafas28
1 files changed, 17 insertions, 11 deletions
diff --git a/bin/hafas b/bin/hafas
index a0f33b7..f9cae79 100755
--- a/bin/hafas
+++ b/bin/hafas
@@ -240,9 +240,9 @@ sub display_occupancies {
}
sub format_delay {
- my ($delay) = @_;
- if ($delay) {
- return sprintf( '(%+4d)', $delay );
+ my ( $delay, $len ) = @_;
+ if ( $delay and $len ) {
+ return sprintf( "(%+${len}d)", $delay );
}
return q{};
}
@@ -316,34 +316,40 @@ for my $res ( $hafas->connections ) {
}
say q{};
- my $have_delay = 0;
-
+ my $delay_len = 0;
+ my $delay_fmt = 0;
for my $sec ( $res->sections ) {
- if ( $sec->dep_delay or $sec->arr_delay ) {
- $have_delay = 7;
+ if ( $sec->dep_delay ) {
+ $delay_len = max( $delay_len, length( $sec->dep_delay ) + 1 );
+ }
+ if ( $sec->arr_delay ) {
+ $delay_len = max( $delay_len, length( $sec->arr_delay ) + 1 );
}
}
+ if ($delay_len) {
+ $delay_fmt = $delay_len + 2;
+ }
for my $sec ( $res->sections ) {
if ( $sec->type eq 'JNY' ) {
printf( "${output_bold}%s${output_reset} → %s\n",
$sec->name, $sec->direction );
printf(
- "%-5s %-${have_delay}s ab %s%s%s\n",
+ "%-5s %-${delay_fmt}s ab %s%s%s\n",
$sec->dep_cancelled
? '--:--'
: $sec->dep->strftime('%H:%M'),
- format_delay( $sec->dep_delay ),
+ format_delay( $sec->dep_delay, $delay_len ),
$sec->dep_loc->name,
$sec->dep_platform ? q{: } : q{},
$sec->dep_platform // q{},
);
printf(
- "%-5s %-${have_delay}s an %s%s%s\n",
+ "%-5s %-${delay_fmt}s an %s%s%s\n",
$sec->arr_cancelled
? '--:--'
: $sec->arr->strftime('%H:%M'),
- format_delay( $sec->arr_delay ),
+ format_delay( $sec->arr_delay, $delay_len ),
$sec->arr_loc->name,
$sec->arr_platform ? q{: } : q{},
$sec->arr_platform // q{},