]>
Dogcows Code - chaz/git-codeowners/blob - lib/App/Codeowners/Formatter/String.pm
1 package App
::Codeowners
::Formatter
::String
;
2 # ABSTRACT: Format codeowners output using printf-like strings
6 This is a L<App::Codeowners::Formatter> that formats output using a printf-like string.
8 See L<git-codeowners/"Format string">.
15 our $VERSION = '9999.999'; # VERSION
17 use parent
'App::Codeowners::Formatter';
19 use App
::Codeowners
::Util
qw(stringf zip);
20 use Color
::ANSI
::Util
0.03 qw(ansifg);
21 use Encode
qw(encode);
27 $result = {zip
@{$self->columns}, @$result};
30 F
=> $self->_create_filterer->($result->{File
}, undef),
31 O
=> $self->_create_filterer->($result->{Owner
}, $self->_owner_colorgen),
32 P
=> $self->_create_filterer->($result->{Project
}, undef),
33 T
=> $self->_create_filterer->($result->{Pattern
}, undef),
36 my $text = stringf
($self->format, %info);
37 print { $self->handle } encode
('UTF-8', $text), "\n";
40 sub _expand_filter_args
{
41 my $arg = shift || '';
43 my @filters = split(/,/, $arg);
46 for (my $i = 0; $i < @filters; ++$i) {
47 my $filter = $filters[$i] or next;
48 if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{3,6}))$/) {
49 $color_override = $1 || '';
50 splice(@filters, $i, 1);
55 return (\
@filters, $color_override);
58 sub _ansi_reset
{ "\033[0m" }
62 my $rgb = shift or return $text;
64 return $text if $ENV{NO_COLOR
};
66 $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/;
67 if ($rgb !~ m/^[0-9a-fA-F]{6}$/) {
68 warn "Color value must be in 'ffffff' or 'fff' form.\n";
72 my ($begin, $end) = (ansifg
($rgb), _ansi_reset
);
73 return "${begin}${text}${end}";
76 sub _create_filterer
{
80 quote
=> sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" },
84 my $value = shift || '';
85 my $color = shift || '';
86 my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color };
89 my ($filters, $color) = _expand_filter_args
($arg);
90 if (ref($value) eq 'ARRAY') {
91 $value = join(',', map { _colored
($_, $color // $gencolor->($_)) } @$value);
94 $value = _colored
($value, $color // $gencolor->($value));
96 for my $key (@$filters) {
97 if (my $filter = $filter{$key}) {
98 $value = $filter->($value);
101 warn "Unknown filter: $key\n"
109 sub _owner_colorgen
{
112 # https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
113 my @contrasting_colors = qw(
114 e6194b 3cb44b ffe119 4363d8 f58231
115 911eb4 42d4f4 f032e6 bfef45 fabebe
116 469990 e6beff 9a6324 fffac8 800000
117 aaffc3 808000 ffd8b1 000075 a9a9a9
120 # assign a color to each owner, on demand
123 $self->{owner_color
} ||= sub {
124 my $owner = shift or return;
125 $owner_colors{$owner} ||= do {
126 $num = ($num + 1) % scalar @contrasting_colors;
127 $contrasting_colors[$num];
This page took 0.050792 seconds and 4 git commands to generate.