]>
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
8 our $VERSION = '0.50'; # VERSION
10 use parent
'App::Codeowners::Formatter';
12 use App
::Codeowners
::Util
qw(stringf zip);
13 use Color
::ANSI
::Util
0.03 qw(ansifg);
19 $result = {zip
@{$self->columns}, @$result};
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 T
=> $self->_create_filterer->($result->{Pattern
}, undef),
28 my $text = stringf
($self->format, %info);
29 print { $self->handle } $text, "\n";
32 sub _expand_filter_args
{
33 my $arg = shift || '';
35 my @filters = split(/,/, $arg);
38 for (my $i = 0; $i < @filters; ++$i) {
39 my $filter = $filters[$i] or next;
40 if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{3,6}))$/) {
41 $color_override = $1 || '';
42 splice(@filters, $i, 1);
47 return (\
@filters, $color_override);
50 sub _ansi_reset
{ "\033[0m" }
54 my $rgb = shift or return $text;
56 return $text if $ENV{NO_COLOR
} || (defined $ENV{COLOR_DEPTH
} && !$ENV{COLOR_DEPTH
});
58 $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/;
59 if ($rgb !~ m/^[0-9a-fA-F]{6}$/) {
60 warn "Color value must be in 'ffffff' or 'fff' form.\n";
64 my ($begin, $end) = (ansifg
($rgb), _ansi_reset
);
65 return "${begin}${text}${end}";
68 sub _create_filterer
{
72 quote
=> sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" },
76 my $value = shift || '';
77 my $color = shift || '';
78 my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color };
81 my ($filters, $color) = _expand_filter_args
($arg);
82 if (ref($value) eq 'ARRAY') {
83 $value = join(',', map { _colored
($_, $color // $gencolor->($_)) } @$value);
86 $value = _colored
($value, $color // $gencolor->($value));
88 for my $key (@$filters) {
89 if (my $filter = $filter{$key}) {
90 $value = $filter->($value);
93 warn "Unknown filter: $key\n"
101 sub _owner_colorgen
{
104 # https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
105 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
112 # assign a color to each owner, on demand
115 $self->{owner_color
} ||= sub {
116 my $owner = shift or return;
117 $owner_colors{$owner} ||= do {
118 $num = ($num + 1) % scalar @contrasting_colors;
119 $contrasting_colors[$num];
134 App::Codeowners::Formatter::String - Format codeowners output using printf-like strings
142 This is a L<App::Codeowners::Formatter> that formats output using a printf-like string.
144 See L<git-codeowners/"Format string">.
148 Please report any bugs or feature requests on the bugtracker website
149 L<https://github.com/chazmcgarvey/git-codeowners/issues>
151 When submitting a bug or request, please include a test-file or a
152 patch to an existing test-file that illustrates the bug or desired
157 Charles McGarvey <chazmcgarvey@brokenzipper.com>
159 =head1 COPYRIGHT AND LICENSE
161 This software is copyright (c) 2021 by Charles McGarvey.
163 This is free software; you can redistribute it and/or modify it under
164 the same terms as the Perl 5 programming language system itself.
This page took 0.046453 seconds and 5 git commands to generate.