From 90a653d7ec45a2e96547035c2968d6e2223d85b5 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 27 Jun 2011 01:50:35 +0200 Subject: db-ris: Do not make output columns (train route etc.) wider than necessary --- bin/db-ris | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/bin/db-ris b/bin/db-ris index 8514831..554d19d 100755 --- a/bin/db-ris +++ b/bin/db-ris @@ -6,13 +6,14 @@ use 5.010; our $VERSION = '0.0'; use Getopt::Long; -use List::Util qw(first); +use List::Util qw(first max); use Travel::Status::DE::DeutscheBahn; my ( $date, $time ); my $types = q{}; my %train_type; my $filter_via; +my @output; binmode( STDOUT, ':encoding(utf-8)' ); @@ -39,6 +40,27 @@ my $status = Travel::Status::DE::DeutscheBahn->new( time => $time, ); +sub display_result { + my (@lines) = @_; + + my @line_length; + + if ( not @lines ) { + die("Nothing to show\n"); + } + + for my $i ( 0 .. 5 ) { + $line_length[$i] = max map { length( $_->[$i] ) } @lines; + } + + for my $line (@lines) { + printf( join( q{ }, ( map { "%-${_}s" } @line_length ) ) . "\n", + @{$line}, ); + } + + return; +} + for my $d ( $status->departures() ) { my ( @via, @via_main, @via_show ); @@ -79,13 +101,18 @@ for my $d ( $status->departures() ) { $stop =~ s{ ?Hbf}{}; } - printf( - "%5s %-10s %-80s %-20s %-2s %s\n", - $d->time, $d->train, join( q{ }, @via_show ), - $d->destination, $d->platform, $d->info + push( + @output, + [ + $d->time, $d->train, + join( q{ }, @via_show ), $d->destination, + $d->platform, $d->info + ] ); } +display_result(@output); + __END__ =head1 NAME -- cgit v1.2.3