File Coverage

blib/lib/Warn/Colorful.pm
Criterion Covered Total %
statement 19 29 65.5
branch 1 4 25.0
condition 1 3 33.3
subroutine 6 6 100.0
pod n/a
total 27 42 64.2


line stmt bran cond sub pod time code
1             package Warn::Colorful;
2 2     2   55505 use 5.008_001;
  2         8  
  2         84  
3 2     2   12 use strict;
  2         4  
  2         71  
4 2     2   21 use warnings;
  2         9  
  2         113  
5              
6             our $VERSION = '0.01';
7              
8 2     2   3336 use Term::ANSIColor;
  2         23839  
  2         1158  
9              
10             my %colors = (
11             base => 'blue',
12             body => undef,
13             file => undef,
14             line => undef,
15             );
16              
17             sub import {
18 2     2   19 my ($package, %args) = @_;
19              
20 2         5 my $config = _parse_config();
21              
22 2         8 for my $name (keys %colors) {
23 8   33     2077 $colors{$name} = $args{$name} || $config->{$name} || $colors{base};
24             }
25             }
26              
27             $SIG{__WARN__} = sub {
28             my $msg = shift;
29              
30             my (undef, $file, $line) = caller;
31              
32             my ($body) = $msg =~ /(.*) at ${file} line ${line}\.\n$/;
33             my $out = sprintf "%s %s %s.\n",
34             colored([$colors{body}], $body),
35             colored([$colors{file}], "at ${file}"),
36             colored([$colors{line}], "line ${line}");
37              
38             print STDERR $out;
39             };
40              
41             sub _parse_config {
42 2     2   6 my $config_path = "$ENV{HOME}/.warn-colorful";
43 2         4 my $config = {};
44              
45 2 50       56 return $config unless -e $config_path;
46              
47 0           open my $fh, '<', $config_path;
48 0           my $text = do { local $/; <$fh> };
  0            
  0            
49 0           close $fh;
50              
51 0           for my $line (split /\n/, $text) {
52 0 0         next unless $line;
53 0           my ($key, $value) = split '=', $line;
54 0           $config->{$key} = $value;
55             }
56              
57 0           return $config;
58             }
59              
60             1;
61             __END__