1 package App
::Codeowners
::Util
;
2 # ABSTRACT: Grab bag of utility subs for Codeowners modules
6 B<DO NOT USE> except in L<App::Codeowners> and related modules.
13 use Exporter
qw(import);
14 use File
::Codeowners
::Util
;
19 find_codeowners_in_directory
20 find_nearest_codeowners
31 our $VERSION = '9999.999'; # VERSION
33 =func find_nearest_codeowners
37 Use L
<File
::Codeowners
::Util
/find_nearest_codeowners
> instead
.
41 sub find_nearest_codeowners
{ goto &File
::Codeowners
::Util
::find_nearest_codeowners
}
43 =func find_codeowners_in_directory
47 Use L
<File
::Codeowners
::Util
/find_codeowners_in_directory
> instead
.
51 sub find_codeowners_in_directory
{ goto &File
::Codeowners
::Util
::find_codeowners_in_directory
}
57 Use L
<File
::Codeowners
::Util
/run_command
> instead
.
61 sub run_command
{ goto &File
::Codeowners
::Util
::run_command
}
67 Use L
<File
::Codeowners
::Util
/run_git
> instead
.
71 sub run_git
{ goto &File
::Codeowners
::Util
::run_git
}
77 Use L
<File
::Codeowners
::Util
/git_ls_files
> instead
.
81 sub git_ls_files
{ goto &File
::Codeowners
::Util
::git_ls_files
}
87 Use L
<File
::Codeowners
::Util
/git_toplevel
> instead
.
91 sub git_toplevel
{ goto &File
::Codeowners
::Util
::git_toplevel
}
95 $str = colorstrip
($str);
97 Strip ANSI color control commands
.
102 my $str = shift || '';
103 $str =~ s/\e\[[\d;]*m//g;
109 $str = stringify
($scalar);
110 $str = stringify
(\
@array);
112 Get a useful string representation of a scallar
or arrayref
.
118 return ref($item) eq 'ARRAY' ? join(',', @$item) : $item;
127 # The stringf code is from String::Format (thanks SREZIC), with changes:
128 # - Use Unicode::GCString for better Unicode character padding,
129 # - Strip ANSI color sequences,
130 # - Prevent 'Negative repeat count does nothing' warnings
132 my ($args, $orig, $alignment, $min_width,
133 $max_width, $passme, $formchar) = @_;
135 # For unknown escapes, return the orignial
136 return $orig unless defined $args->{$formchar};
138 $alignment = '+' unless defined $alignment;
140 my $replacement = $args->{$formchar};
141 if (ref $replacement eq 'CODE') {
142 # $passme gets passed to subrefs.
145 $replacement = $replacement->($passme);
149 if (eval { require Unicode
::GCString
}) {
150 my $gcstring = Unicode
::GCString-
>new(colorstrip
($replacement));
151 $replength = $gcstring->columns;
154 $replength = length colorstrip
($replacement);
157 $min_width ||= $replength;
158 $max_width ||= $replength;
160 # length of replacement is between min and max
161 if (($replength > $min_width) && ($replength < $max_width)) {
165 # length of replacement is longer than max; truncate
166 if ($replength > $max_width) {
167 return substr($replacement, 0, $max_width);
170 my $padding = $min_width - $replength;
171 $padding = 0 if $padding < 0;
173 # length of replacement is less than min: pad
174 if ($alignment eq '-') {
175 # left align; pad in front
176 return $replacement . ' ' x
$padding;
179 # right align, pad at end
180 return ' ' x
$padding . $replacement;
184 (-)? # left-align, rather than right
185 (\d
*)? # (optional) minimum field width
186 (?:\
.(\d
*))? # (optional) maximum field width
187 (\
{.*?\
})? # (optional) stuff inside
188 (\S
) # actual format character
191 my $format = shift || return;
192 my $args = UNIVERSAL
::isa
($_[0], 'HASH') ? shift : { @_ };
193 $args->{'n'} = "\n" unless exists $args->{'n'};
194 $args->{'t'} = "\t" unless exists $args->{'t'};
195 $args->{'%'} = "%" unless exists $args->{'%'};
197 $format =~ s/$regex/_replace($args, $1, $2, $3, $4, $5, $6)/ge;
206 Use L
<File
::Codeowners
::Util
/unbackslash
> instead
.
210 # The unbacklash code is from String::Escape (thanks EVO), with changes:
211 # - Handle \a, \b, \f and \v (thanks Berk Akinci)
215 # Earlier definitions are preferred to later ones, thus we output \n not \x0d
217 ( map { $_ => $_ } ( '\\', '"', '$', '@' ) ),
218 ( 'r' => "\r", 'n' => "\n", 't' => "\t" ),
219 ( map { 'x' . unpack('H2', chr($_)) => chr($_) } (0..255) ),
220 ( map { sprintf('%03o', $_) => chr($_) } (0..255) ),
221 ( 'a' => "\x07", 'b' => "\x08", 'f' => "\x0c", 'v' => "\x0b" ),
223 $str =~ s/ (\A|\G|[^\\]) \\ ( [0-7]{3} | x[\da-fA-F]{2} | . ) / $1 . $unbackslash{lc($2)} /gsxe;
229 Same as L
<List
::SomeUtils
/zip-ARRAY1-ARRAY2-
[-ARRAY3-
...-]>.
233 # The zip code is from List::SomeUtils (thanks DROLSKY), copied just so as not
234 # to bring in the extra dependency.
235 sub zip
(\
@\
@) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
237 $max < $#$_ && ( $max = $#$_ ) foreach @_;