diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2025-04-18 12:24:23 +0200 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2025-04-18 12:24:23 +0200 |
commit | 40218881cd5c83d661093b5e80c86ffc47993bf9 (patch) | |
tree | 3954d3a89523d3c8c386bbf4c2510864ee81b432 /bin/dbris-m | |
parent | d66fb794e9277f336f40c1a7a01a444dcfafd70c (diff) |
dbris-m: Add --colour={auto,always,never} option
Diffstat (limited to 'bin/dbris-m')
-rwxr-xr-x | bin/dbris-m | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/bin/dbris-m b/bin/dbris-m index 32772f2..ebd6065 100755 --- a/bin/dbris-m +++ b/bin/dbris-m @@ -19,6 +19,7 @@ my $developer_mode; my $show_jid; my $use_cache = 1; my $cache; +my $use_colour = 'auto'; my ( $json_output, $raw_json_output, $with_polyline ); my %known_mot = map { $_ => 1 } @@ -29,8 +30,8 @@ for my $arg (@ARGV) { $arg = decode( 'UTF-8', $arg ); } -my $output_bold = -t STDOUT ? "\033[1m" : q{}; -my $output_reset = -t STDOUT ? "\033[0m" : q{}; +my $output_bold = "\033[1m"; +my $output_reset = "\033[0m"; my $cf_first = "\e[38;5;11m"; my $cf_mixed = "\e[38;5;208m"; @@ -45,6 +46,8 @@ GetOptions( 't|time=s' => \$time, 'V|version' => \&show_version, 'cache!' => \$use_cache, + 'color=s' => \$use_colour, + 'colour=s' => \$use_colour, 'devmode' => \$developer_mode, 'json' => \$json_output, 'raw-json' => \$raw_json_output, @@ -52,6 +55,14 @@ GetOptions( ) or show_help(1); +if ( $use_colour eq 'auto' + and ( not -t STDOUT or ( defined $ENV{TERM} and $ENV{TERM} eq 'dumb' ) ) + or $use_colour eq 'never' ) +{ + $output_bold = $output_reset = q{}; + $cf_first = $cf_mixed = $cf_second = $cf_reset = q{}; +} + if ($use_cache) { my $cache_path = ( $ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache" ) . '/Travel-Status-DE-DBRIS'; @@ -515,19 +526,19 @@ if ($train_no) { $wagon_desc = ' ■ '; } - my $class_color = ''; + my $class_colour = ''; if ( $wagon->class_type == 1 ) { - $class_color = $cf_first; + $class_colour = $cf_first; } elsif ( $wagon->class_type == 2 ) { - $class_color = $cf_second; + $class_colour = $cf_second; } elsif ( $wagon->class_type == 12 ) { - $class_color = $cf_mixed; + $class_colour = $cf_mixed; } printf( "%s%s%3s%s%s", - ' ' x $spacing_left, $class_color, $wagon_desc, + ' ' x $spacing_left, $class_colour, $wagon_desc, $cf_reset, ' ' x $spacing_right ); } print $wr->direction == 100 ? '>' : '<'; @@ -635,6 +646,14 @@ operating mode(s). =over +=item B<--colour>, B<--color> B<always>|B<auto>|B<never> + +By default, B<dbris-m> uses ANSI escape codes for output formatting whenever +the output is connected to a terminal and the TERM environment variable is not +set to C<< dumb >>. B<--colour=always> causes it to always use output +formatting regardless of terminal setup, and B<--colour=never> disables any +formatting. B<--colour=auto> restores the default behaviour. + =item B<-d>, B<--date> I<DD.MM.[YYYY]> (departure monitor) Request departures on the specified date. |