]>
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
6 This is a L<App::Codeowners::Formatter> that formats output using a printf-like string.
8 See L<git-codeowners/"Format string">.
15 our $VERSION = '9999.999'; # VERSION
17 use parent
'App::Codeowners::Formatter';
19 use App
::Codeowners
::Util
qw(stringf zip);
20 use Color
::ANSI
::Util
0.03 qw(ansifg);
26 $result = {zip
@{$self->columns}, @$result};
29 F
=> $self->_create_filterer->($result->{File
}, undef),
30 O
=> $self->_create_filterer->($result->{Owner
}, $self->_owner_colorgen),
31 P
=> $self->_create_filterer->($result->{Project
}, undef),
32 T
=> $self->_create_filterer->($result->{Pattern
}, undef),
35 my $text = stringf
($self->format, %info);
36 print { $self->handle } $text, "\n";
39 sub _expand_filter_args
{
40 my $arg = shift || '';
42 my @filters = split(/,/, $arg);
45 for (my $i = 0; $i < @filters; ++$i) {
46 my $filter = $filters[$i] or next;
47 if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{3,6}))$/) {
48 $color_override = $1 || '';
49 splice(@filters, $i, 1);
54 return (\
@filters, $color_override);
57 sub _ansi_reset
{ "\033[0m" }
61 my $rgb = shift or return $text;
63 return $text if $ENV{NO_COLOR
} || (defined $ENV{COLOR_DEPTH
} && !$ENV{COLOR_DEPTH
});
65 $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/;
66 if ($rgb !~ m/^[0-9a-fA-F]{6}$/) {
67 warn "Color value must be in 'ffffff' or 'fff' form.\n";
71 my ($begin, $end) = (ansifg
($rgb), _ansi_reset
);
72 return "${begin}${text}${end}";
75 sub _create_filterer
{
79 quote
=> sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" },
83 my $value = shift || '';
84 my $color = shift || '';
85 my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color };
88 my ($filters, $color) = _expand_filter_args
($arg);
89 if (ref($value) eq 'ARRAY') {
90 $value = join(',', map { _colored
($_, $color // $gencolor->($_)) } @$value);
93 $value = _colored
($value, $color // $gencolor->($value));
95 for my $key (@$filters) {
96 if (my $filter = $filter{$key}) {
97 $value = $filter->($value);
100 warn "Unknown filter: $key\n"
108 sub _owner_colorgen
{
111 # https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
112 my @contrasting_colors = qw(
113 e6194b 3cb44b ffe119 4363d8 f58231
114 911eb4 42d4f4 f032e6 bfef45 fabebe
115 469990 e6beff 9a6324 fffac8 800000
116 aaffc3 808000 ffd8b1 000075 a9a9a9
119 # assign a color to each owner, on demand
122 $self->{owner_color
} ||= sub {
123 my $owner = shift or return;
124 $owner_colors{$owner} ||= do {
125 $num = ($num + 1) % scalar @contrasting_colors;
126 $contrasting_colors[$num];
This page took 0.054929 seconds and 5 git commands to generate.