From 25372f260d4e7e1919a9dfec018476321d1df8e3 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Thu, 7 Nov 2019 20:29:18 -0700 Subject: [PATCH] allow shorter color codes --- bin/git-codeowners | 36 +++++++++++++++++++++++++++++++++++- dist.ini | 3 ++- lib/App/Codeowners.pm | 8 +++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/bin/git-codeowners b/bin/git-codeowners index e620ea8..d565192 100755 --- a/bin/git-codeowners +++ b/bin/git-codeowners @@ -42,6 +42,17 @@ Alias: C<-h> You can also use C<--manual> to print the full documentation. +=head2 --color + +Enable colorized output. + +Color is ON by default on terminals; use C<--no-color> to disable. Some environment variables may +also alter the behavior of colorizing output: + +=for :list +* C - Set to disable color (same as C<--no-color>). +* C - Set the number of supportable colors (e.g. 0, 16, 256, 16777216). + =head2 --format Specify the output format to use. See L. @@ -98,7 +109,30 @@ The C<--format> argument can be one of: * C - YAML (requires L) * C - Custom format (see below) -You can specify a custom format using printf-like format sequences. +You can specify a custom format using printf-like format sequences. These are the items that can +substituted: + +=for :list +* C<%F> - Filename +* C<%O> - Owner or owners +* C<%P> - Project +* C<%T> - Pattern +* C<%n> - newline +* C<%t> - tab +* C<%%> - percent sign + +The syntax also allows padding and some filters. Examples: + + git-codeowners show -f ' * %-50F %O' # default for "show" + git-codeowners show -f '%{quote}F,%{quote}O' # ad hoc CSV + git-codeowners patterns -f '--> %{color:0c0}T' # whatever... + +Available filters: + +=for :list +* C - Quote the replacement string. +* C - Colorize the replacement string (if color is ON). +* C - Do not colorize replacement string. =cut diff --git a/dist.ini b/dist.ini index c1f4b41..c7fe9e6 100644 --- a/dist.ini +++ b/dist.ini @@ -21,7 +21,8 @@ remove_runtime = Text::Table remove_runtime = Unicode::GCString remove_runtime = YAML [Prereqs / RuntimeRecommends] -Unicode::GCString = 0 +Unicode::GCString = 0 +Term::Detect::Software = 0 [Prereqs / RuntimeSuggests] JSON::MaybeXS = 0 Text::CSV = 0 diff --git a/lib/App/Codeowners.pm b/lib/App/Codeowners.pm index eebe058..efe86f7 100644 --- a/lib/App/Codeowners.pm +++ b/lib/App/Codeowners.pm @@ -275,7 +275,7 @@ sub _expand_filter_args { for (my $i = 0; $i < @filters; ++$i) { my $filter = $filters[$i] or next; - if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{6}))$/) { + if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{3,6}))$/) { $color_override = $1 || ''; splice(@filters, $i, 1); redo; @@ -292,6 +292,12 @@ sub _colored { # ansifg honors NO_COLOR already, but ansi_reset does not. return $text if $ENV{NO_COLOR}; + $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/; + if ($rgb !~ m/^[0-9a-fA-F]{6}$/) { + warn "Color value must be in 'ffffff' or 'fff' form.\n"; + return $text; + } + my ($begin, $end) = (ansifg($rgb), ansi_reset); return "${begin}${text}${end}"; } -- 2.43.0