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