line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Codeowners::Formatter::String; |
2
|
|
|
|
|
|
|
# ABSTRACT: Format codeowners output using printf-like strings |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1082
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
131
|
|
6
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
91
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.50'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
7
|
use parent 'App::Codeowners::Formatter'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
151
|
use App::Codeowners::Util qw(stringf zip); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
185
|
|
13
|
1
|
|
|
1
|
|
8
|
use Color::ANSI::Util 0.03 qw(ansifg); |
|
1
|
|
|
|
|
59
|
|
|
1
|
|
|
|
|
1257
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub stream { |
16
|
6
|
|
|
6
|
1
|
17
|
my $self = shift; |
17
|
6
|
|
|
|
|
13
|
my $result = shift; |
18
|
|
|
|
|
|
|
|
19
|
6
|
|
|
|
|
11
|
$result = {zip @{$self->columns}, @$result}; |
|
6
|
|
|
|
|
40
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my %info = ( |
22
|
|
|
|
|
|
|
F => $self->_create_filterer->($result->{File}, undef), |
23
|
|
|
|
|
|
|
O => $self->_create_filterer->($result->{Owner}, $self->_owner_colorgen), |
24
|
|
|
|
|
|
|
P => $self->_create_filterer->($result->{Project}, undef), |
25
|
6
|
|
|
|
|
25
|
T => $self->_create_filterer->($result->{Pattern}, undef), |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
6
|
|
|
|
|
40
|
my $text = stringf($self->format, %info); |
29
|
6
|
|
|
|
|
15
|
print { $self->handle } $text, "\n"; |
|
6
|
|
|
|
|
35
|
|
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _expand_filter_args { |
33
|
15
|
|
50
|
15
|
|
62
|
my $arg = shift || ''; |
34
|
|
|
|
|
|
|
|
35
|
15
|
|
|
|
|
40
|
my @filters = split(/,/, $arg); |
36
|
15
|
|
|
|
|
27
|
my $color_override; |
37
|
|
|
|
|
|
|
|
38
|
15
|
|
|
|
|
42
|
for (my $i = 0; $i < @filters; ++$i) { |
39
|
0
|
0
|
|
|
|
0
|
my $filter = $filters[$i] or next; |
40
|
0
|
0
|
|
|
|
0
|
if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{3,6}))$/) { |
41
|
0
|
|
0
|
|
|
0
|
$color_override = $1 || ''; |
42
|
0
|
|
|
|
|
0
|
splice(@filters, $i, 1); |
43
|
0
|
|
|
|
|
0
|
redo; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
15
|
|
|
|
|
44
|
return (\@filters, $color_override); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
0
|
|
0
|
sub _ansi_reset { "\033[0m" } |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _colored { |
53
|
15
|
|
|
15
|
|
29
|
my $text = shift; |
54
|
15
|
100
|
|
|
|
44
|
my $rgb = shift or return $text; |
55
|
|
|
|
|
|
|
|
56
|
4
|
50
|
0
|
|
|
27
|
return $text if $ENV{NO_COLOR} || (defined $ENV{COLOR_DEPTH} && !$ENV{COLOR_DEPTH}); |
|
|
|
33
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
$rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/; |
59
|
0
|
0
|
|
|
|
0
|
if ($rgb !~ m/^[0-9a-fA-F]{6}$/) { |
60
|
0
|
|
|
|
|
0
|
warn "Color value must be in 'ffffff' or 'fff' form.\n"; |
61
|
0
|
|
|
|
|
0
|
return $text; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
my ($begin, $end) = (ansifg($rgb), _ansi_reset); |
65
|
0
|
|
|
|
|
0
|
return "${begin}${text}${end}"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _create_filterer { |
69
|
24
|
|
|
24
|
|
44
|
my $self = shift; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my %filter = ( |
72
|
0
|
|
|
0
|
|
0
|
quote => sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" }, |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
73
|
24
|
|
|
|
|
113
|
); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
return sub { |
76
|
24
|
|
100
|
24
|
|
73
|
my $value = shift || ''; |
77
|
24
|
|
100
|
|
|
77
|
my $color = shift || ''; |
78
|
24
|
100
|
|
|
|
76
|
my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color }; |
|
9
|
|
|
|
|
44
|
|
79
|
|
|
|
|
|
|
return sub { |
80
|
15
|
|
|
|
|
27
|
my $arg = shift; |
81
|
15
|
|
|
|
|
28
|
my ($filters, $color) = _expand_filter_args($arg); |
82
|
15
|
100
|
|
|
|
37
|
if (ref($value) eq 'ARRAY') { |
83
|
4
|
|
33
|
|
|
9
|
$value = join(',', map { _colored($_, $color // $gencolor->($_)) } @$value); |
|
4
|
|
|
|
|
14
|
|
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
11
|
|
33
|
|
|
32
|
$value = _colored($value, $color // $gencolor->($value)); |
87
|
|
|
|
|
|
|
} |
88
|
15
|
|
|
|
|
38
|
for my $key (@$filters) { |
89
|
0
|
0
|
|
|
|
0
|
if (my $filter = $filter{$key}) { |
90
|
0
|
|
|
|
|
0
|
$value = $filter->($value); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
0
|
|
|
|
|
0
|
warn "Unknown filter: $key\n" |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
15
|
100
|
|
|
|
60
|
$value || ''; |
97
|
24
|
|
|
|
|
185
|
}; |
98
|
24
|
|
|
|
|
121
|
}; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _owner_colorgen { |
102
|
6
|
|
|
6
|
|
16
|
my $self = shift; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/ |
105
|
6
|
|
|
|
|
44
|
my @contrasting_colors = qw( |
106
|
|
|
|
|
|
|
e6194b 3cb44b ffe119 4363d8 f58231 |
107
|
|
|
|
|
|
|
911eb4 42d4f4 f032e6 bfef45 fabebe |
108
|
|
|
|
|
|
|
469990 e6beff 9a6324 fffac8 800000 |
109
|
|
|
|
|
|
|
aaffc3 808000 ffd8b1 000075 a9a9a9 |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# assign a color to each owner, on demand |
113
|
6
|
|
|
|
|
10
|
my %owner_colors; |
114
|
6
|
|
|
|
|
18
|
my $num = -1; |
115
|
|
|
|
|
|
|
$self->{owner_color} ||= sub { |
116
|
6
|
100
|
|
6
|
|
20
|
my $owner = shift or return; |
117
|
4
|
|
33
|
|
|
35
|
$owner_colors{$owner} ||= do { |
118
|
4
|
|
|
|
|
12
|
$num = ($num + 1) % scalar @contrasting_colors; |
119
|
4
|
|
|
|
|
20
|
$contrasting_colors[$num]; |
120
|
|
|
|
|
|
|
}; |
121
|
6
|
|
100
|
|
|
53
|
}; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
__END__ |