]> Dogcows Code - chaz/git-codeowners/blob - lib/App/Codeowners/Formatter/String.pm
14b07a4096b2769881f9565eb575cfe5a41a3666
[chaz/git-codeowners] / lib / App / Codeowners / Formatter / String.pm
1 package App::Codeowners::Formatter::String;
2 # ABSTRACT: Format codeowners output using printf-like strings
3
4
5 use warnings;
6 use strict;
7
8 our $VERSION = '0.49'; # VERSION
9
10 use parent 'App::Codeowners::Formatter';
11
12 use App::Codeowners::Util qw(stringf zip);
13 use Color::ANSI::Util 0.03 qw(ansifg);
14
15 sub stream {
16 my $self = shift;
17 my $result = shift;
18
19 $result = {zip @{$self->columns}, @$result};
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 T => $self->_create_filterer->($result->{Pattern}, undef),
26 );
27
28 my $text = stringf($self->format, %info);
29 print { $self->handle } $text, "\n";
30 }
31
32 sub _expand_filter_args {
33 my $arg = shift || '';
34
35 my @filters = split(/,/, $arg);
36 my $color_override;
37
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);
43 redo;
44 }
45 }
46
47 return (\@filters, $color_override);
48 }
49
50 sub _ansi_reset { "\033[0m" }
51
52 sub _colored {
53 my $text = shift;
54 my $rgb = shift or return $text;
55
56 return $text if $ENV{NO_COLOR} || (defined $ENV{COLOR_DEPTH} && !$ENV{COLOR_DEPTH});
57
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";
61 return $text;
62 }
63
64 my ($begin, $end) = (ansifg($rgb), _ansi_reset);
65 return "${begin}${text}${end}";
66 }
67
68 sub _create_filterer {
69 my $self = shift;
70
71 my %filter = (
72 quote => sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" },
73 );
74
75 return sub {
76 my $value = shift || '';
77 my $color = shift || '';
78 my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color };
79 return sub {
80 my $arg = shift;
81 my ($filters, $color) = _expand_filter_args($arg);
82 if (ref($value) eq 'ARRAY') {
83 $value = join(',', map { _colored($_, $color // $gencolor->($_)) } @$value);
84 }
85 else {
86 $value = _colored($value, $color // $gencolor->($value));
87 }
88 for my $key (@$filters) {
89 if (my $filter = $filter{$key}) {
90 $value = $filter->($value);
91 }
92 else {
93 warn "Unknown filter: $key\n"
94 }
95 }
96 $value || '';
97 };
98 };
99 }
100
101 sub _owner_colorgen {
102 my $self = shift;
103
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
110 );
111
112 # assign a color to each owner, on demand
113 my %owner_colors;
114 my $num = -1;
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];
120 };
121 };
122 }
123
124 1;
125
126 __END__
127
128 =pod
129
130 =encoding UTF-8
131
132 =head1 NAME
133
134 App::Codeowners::Formatter::String - Format codeowners output using printf-like strings
135
136 =head1 VERSION
137
138 version 0.49
139
140 =head1 DESCRIPTION
141
142 This is a L<App::Codeowners::Formatter> that formats output using a printf-like string.
143
144 See L<git-codeowners/"Format string">.
145
146 =head1 BUGS
147
148 Please report any bugs or feature requests on the bugtracker website
149 L<https://github.com/chazmcgarvey/git-codeowners/issues>
150
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
153 feature.
154
155 =head1 AUTHOR
156
157 Charles McGarvey <chazmcgarvey@brokenzipper.com>
158
159 =head1 COPYRIGHT AND LICENSE
160
161 This software is copyright (c) 2019 by Charles McGarvey.
162
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.
165
166 =cut
This page took 0.043686 seconds and 3 git commands to generate.