| 1 | # Copyright © 2015-2026 Guillem Jover <guillem@debian.org> | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Dpkg::Color - color output handling | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | This module provides functions to handle colored output. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | B<Note>: This is a private module, its API can change at any time. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | package Dpkg::Color 0.01; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 575 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 573 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 35 | color_setup | ||||||||||||||||||||||||||||
| 36 | color_get | ||||||||||||||||||||||||||||
| 37 | colored_str | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 575 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub color_setup | ||||||||||||||||||||||||||||
| 43 | 26 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
color_setup: called CC=7 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 26 | ▶ | my $mode = $ENV{'DPKG_COLORS'} // 'auto'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"DPKG_COLORS"} // "auto"
MC/DC0%0/1
$ENV{"DPKG_COLORS"} // "auto"
$ENV{"DPKG_COLORS"}
Truth table50%1/2
A
A= $ENV{"DPKG_COLORS"}
| |||||||||||||||||||||||||||||
| 45 | 26 | my $use_color; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 26 | ▶ | if ($mode eq 'auto') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 48 | ## no critic (InputOutput::ProhibitInteractiveTest) | ||||||||||||||||||||||||||||
| 49 | 26 | ▶ | $use_color = 1 if -t *STDOUT or -t *STDERR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-t *STDOUT or -t *STDERR
MC/DC0%0/2
-t *STDOUT or -t *STDERR
-t *STDOUT
-t *STDERR
Truth table33%1/3
A or B
A= -t *STDOUTB= -t *STDERR
| |||||||||||||||||||||||||||||
| 50 | } elsif ($mode eq 'always') { | ||||||||||||||||||||||||||||
| 51 | 0 | $use_color = 1; | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 0 | $use_color = 0; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 26 | ▶ | require Term::ANSIColor if $use_color; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub color_get($color) | ||||||||||||||||||||||||||
|
Subroutine
color_get: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | state $use_color = color_setup(); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($use_color) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | return Term::ANSIColor::color($color); | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 0 | return q{}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 210 | ▶ | sub colored_str($str, $color) | ||||||||||||||||||||||||||
|
Subroutine
colored_str: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 210 | state $use_color = color_setup(); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 210 | ▶ | if ($use_color) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | return Term::ANSIColor::colored($str, $color); | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 210 | return $str; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 CHANGES | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 Version 0.xx | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | This is a private module. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 573 | 1; | |||||||||||||||||||||||||||