]> Dogcows Code - chaz/git-codeowners/blob - lib/App/Codeowners/Formatter/String.pm
refactor formatters
[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 =head1 DESCRIPTION
5
6 This is a L<App::Codeowners::Formatter> that formats output using a printf-like string.
7
8 See L<git-codeowners/"Format string">.
9
10 =cut
11
12 use warnings;
13 use strict;
14
15 our $VERSION = '9999.999'; # VERSION
16
17 use parent 'App::Codeowners::Formatter';
18
19 use App::Codeowners::Util qw(stringf zip);
20 use Color::ANSI::Util 0.03 qw(ansifg);
21 use Encode qw(encode);
22
23 sub stream {
24 my $self = shift;
25 my $result = shift;
26
27 $result = {zip @{$self->columns}, @$result};
28
29 my %info = (
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),
34 );
35
36 my $text = stringf($self->format, %info);
37 print { $self->handle } encode('UTF-8', $text), "\n";
38 }
39
40 sub _expand_filter_args {
41 my $arg = shift || '';
42
43 my @filters = split(/,/, $arg);
44 my $color_override;
45
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);
51 redo;
52 }
53 }
54
55 return (\@filters, $color_override);
56 }
57
58 sub _ansi_reset { "\033[0m" }
59
60 sub _colored {
61 my $text = shift;
62 my $rgb = shift or return $text;
63
64 return $text if $ENV{NO_COLOR};
65
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";
69 return $text;
70 }
71
72 my ($begin, $end) = (ansifg($rgb), _ansi_reset);
73 return "${begin}${text}${end}";
74 }
75
76 sub _create_filterer {
77 my $self = shift;
78
79 my %filter = (
80 quote => sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" },
81 );
82
83 return sub {
84 my $value = shift || '';
85 my $color = shift || '';
86 my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color };
87 return sub {
88 my $arg = shift;
89 my ($filters, $color) = _expand_filter_args($arg);
90 if (ref($value) eq 'ARRAY') {
91 $value = join(',', map { _colored($_, $color // $gencolor->($_)) } @$value);
92 }
93 else {
94 $value = _colored($value, $color // $gencolor->($value));
95 }
96 for my $key (@$filters) {
97 if (my $filter = $filter{$key}) {
98 $value = $filter->($value);
99 }
100 else {
101 warn "Unknown filter: $key\n"
102 }
103 }
104 $value || '';
105 };
106 };
107 }
108
109 sub _owner_colorgen {
110 my $self = shift;
111
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
118 );
119
120 # assign a color to each owner, on demand
121 my %owner_colors;
122 my $num = -1;
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];
128 };
129 };
130 }
131
132 1;
This page took 0.038324 seconds and 4 git commands to generate.