]>
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.46'; # VERSION
10 use parent
'App::Codeowners::Formatter';
12 use App
::Codeowners
::Util
qw(stringf zip);
13 use Color
::ANSI
::Util
0.03 qw(ansifg);
14 use Encode
qw(encode);
20 $result = {zip
@{$self->columns}, @$result};
23 F
=> $self->_create_filterer->($result->{File
}, undef),
24 O
=> $self->_create_filterer->($result->{Owner
}, $self->_owner_colorgen),
25 P
=> $self->_create_filterer->($result->{Project
}, undef),
26 T
=> $self->_create_filterer->($result->{Pattern
}, undef),
29 my $text = stringf
($self->format, %info);
30 print { $self->handle } encode
('UTF-8', $text), "\n";
33 sub _expand_filter_args
{
34 my $arg = shift || '';
36 my @filters = split(/,/, $arg);
39 for (my $i = 0; $i < @filters; ++$i) {
40 my $filter = $filters[$i] or next;
41 if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{3,6}))$/) {
42 $color_override = $1 || '';
43 splice(@filters, $i, 1);
48 return (\
@filters, $color_override);
51 sub _ansi_reset
{ "\033[0m" }
55 my $rgb = shift or return $text;
57 return $text if $ENV{NO_COLOR
} || (defined $ENV{COLOR_DEPTH
} && !$ENV{COLOR_DEPTH
});
59 $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/;
60 if ($rgb !~ m/^[0-9a-fA-F]{6}$/) {
61 warn "Color value must be in 'ffffff' or 'fff' form.\n";
65 my ($begin, $end) = (ansifg
($rgb), _ansi_reset
);
66 return "${begin}${text}${end}";
69 sub _create_filterer
{
73 quote
=> sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" },
77 my $value = shift || '';
78 my $color = shift || '';
79 my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color };
82 my ($filters, $color) = _expand_filter_args
($arg);
83 if (ref($value) eq 'ARRAY') {
84 $value = join(',', map { _colored
($_, $color // $gencolor->($_)) } @$value);
87 $value = _colored
($value, $color // $gencolor->($value));
89 for my $key (@$filters) {
90 if (my $filter = $filter{$key}) {
91 $value = $filter->($value);
94 warn "Unknown filter: $key\n"
102 sub _owner_colorgen
{
105 # https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
106 my @contrasting_colors = qw(
107 e6194b 3cb44b ffe119 4363d8 f58231
108 911eb4 42d4f4 f032e6 bfef45 fabebe
109 469990 e6beff 9a6324 fffac8 800000
110 aaffc3 808000 ffd8b1 000075 a9a9a9
113 # assign a color to each owner, on demand
116 $self->{owner_color
} ||= sub {
117 my $owner = shift or return;
118 $owner_colors{$owner} ||= do {
119 $num = ($num + 1) % scalar @contrasting_colors;
120 $contrasting_colors[$num];
135 App::Codeowners::Formatter::String - Format codeowners output using printf-like strings
143 This is a L<App::Codeowners::Formatter> that formats output using a printf-like string.
145 See L<git-codeowners/"Format string">.
149 Please report any bugs or feature requests on the bugtracker website
150 L<https://github.com/chazmcgarvey/git-codeowners/issues>
152 When submitting a bug or request, please include a test-file or a
153 patch to an existing test-file that illustrates the bug or desired
158 Charles McGarvey <chazmcgarvey@brokenzipper.com>
160 =head1 COPYRIGHT AND LICENSE
162 This software is copyright (c) 2019 by Charles McGarvey.
164 This is free software; you can redistribute it and/or modify it under
165 the same terms as the Perl 5 programming language system itself.
This page took 0.044678 seconds and 4 git commands to generate.