| 1 | package File::Util::Sort; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Fcntl ':mode'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::chdir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Perinci::Sub::Util qw(gen_modified_sub); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use List::Util qw(uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 16 | 1 | our $DATE = '2026-09-25'; # DATE | |||||||||||||||||||||||||||
| 17 | 1 | our $DIST = 'File-Util-Sort'; # DIST | |||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '0.012'; # VERSION | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | our %SPEC; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 23 | sort_files | ||||||||||||||||||||||||||||
| 24 | foremost | ||||||||||||||||||||||||||||
| 25 | hindmost | ||||||||||||||||||||||||||||
| 26 | largest | ||||||||||||||||||||||||||||
| 27 | smallest | ||||||||||||||||||||||||||||
| 28 | newest | ||||||||||||||||||||||||||||
| 29 | oldest | ||||||||||||||||||||||||||||
| 30 | longest_name | ||||||||||||||||||||||||||||
| 31 | shortest_name | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | my @file_types = qw(file dir); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my @file_fields = qw(name size mtime ctime); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | our %argspecs_common = ( | ||||||||||||||||||||||||||||
| 39 | dirs => { | ||||||||||||||||||||||||||||
| 40 | 'x.name.is_plural' => 1, | ||||||||||||||||||||||||||||
| 41 | 'x.name.singular' => 'dir', | ||||||||||||||||||||||||||||
| 42 | summary => 'Directory to sort files of, defaults to current directory', | ||||||||||||||||||||||||||||
| 43 | schema => ['array*', of=>'dirname*', min_len=>1], | ||||||||||||||||||||||||||||
| 44 | default => ['.'], | ||||||||||||||||||||||||||||
| 45 | pos => 0, | ||||||||||||||||||||||||||||
| 46 | slurpy => 1, | ||||||||||||||||||||||||||||
| 47 | tags => ['category:input'], | ||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | recursive => { | ||||||||||||||||||||||||||||
| 50 | summary => 'Recurse into subdirectories', | ||||||||||||||||||||||||||||
| 51 | schema => 'true*', | ||||||||||||||||||||||||||||
| 52 | cmdline_aliases => {R=>{}}, | ||||||||||||||||||||||||||||
| 53 | tags => ['category:input'], | ||||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | type => { | ||||||||||||||||||||||||||||
| 57 | summary => 'Only include files of certain type', | ||||||||||||||||||||||||||||
| 58 | schema => ['str*', in=>\@file_types], | ||||||||||||||||||||||||||||
| 59 | cmdline_aliases => { | ||||||||||||||||||||||||||||
| 60 | t => {}, | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | f => {summary=>'Shortcut for `--type=file`', is_flag=>1, code=>sub { $_[0]{type} = 'file' }}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | d => {summary=>'Shortcut for `--type=dir`' , is_flag=>1, code=>sub { $_[0]{type} = 'dir' }}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||
| 64 | 1 | tags => ['category:filtering'], | |||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||
| 66 | exclude_filename_pattern => { | ||||||||||||||||||||||||||||
| 67 | summary => 'Exclude filenames that match a regex pattern', | ||||||||||||||||||||||||||||
| 68 | schema => 're_from_str*', | ||||||||||||||||||||||||||||
| 69 | cmdline_aliases => {X=>{}}, | ||||||||||||||||||||||||||||
| 70 | tags => ['category:filtering'], | ||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||
| 72 | include_filename_pattern => { | ||||||||||||||||||||||||||||
| 73 | summary => 'Only include filenames that match a regex pattern', | ||||||||||||||||||||||||||||
| 74 | schema => 're_from_str*', | ||||||||||||||||||||||||||||
| 75 | cmdline_aliases => {I=>{}}, | ||||||||||||||||||||||||||||
| 76 | tags => ['category:filtering'], | ||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||
| 78 | all => { | ||||||||||||||||||||||||||||
| 79 | summary => 'Do not ignore entries starting with .', | ||||||||||||||||||||||||||||
| 80 | schema => 'true*', | ||||||||||||||||||||||||||||
| 81 | cmdline_aliases => {a=>{}}, | ||||||||||||||||||||||||||||
| 82 | tags => ['category:filtering'], | ||||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | detail => { | ||||||||||||||||||||||||||||
| 86 | schema => 'true*', | ||||||||||||||||||||||||||||
| 87 | cmdline_aliases => {l=>{}}, | ||||||||||||||||||||||||||||
| 88 | tags => ['category:output'], | ||||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||
| 90 | num_results => { | ||||||||||||||||||||||||||||
| 91 | summary => 'Number of results to return', | ||||||||||||||||||||||||||||
| 92 | schema => 'uint*', | ||||||||||||||||||||||||||||
| 93 | cmdline_aliases => {n=>{}}, | ||||||||||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||||||||||
| 95 | num_ranks => { | ||||||||||||||||||||||||||||
| 96 | summary => 'Number of ranks to return', | ||||||||||||||||||||||||||||
| 97 | schema => 'uint*', | ||||||||||||||||||||||||||||
| 98 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Difference between `num_results` and `num_ranks`: `num_results` (`-n` option) | ||||||||||||||||||||||||||||
| 101 | specifies number of results regardless of ranks while `num_ranks` (`-N` option) | ||||||||||||||||||||||||||||
| 102 | returns number of ranks. For example, if sorting is by reverse size and if | ||||||||||||||||||||||||||||
| 103 | `num_results` is set to 1 and there are 2 files with the same largest size then | ||||||||||||||||||||||||||||
| 104 | only 1 of those files will be returned. With `num_ranks` set to 1, both files | ||||||||||||||||||||||||||||
| 105 | will be returned because are they both rank #1. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | MARKDOWN | ||||||||||||||||||||||||||||
| 108 | cmdline_aliases => {N=>{}}, | ||||||||||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | our %argspecs_sort = ( | |||||||||||||||||||||||||||
| 113 | by_code => { | ||||||||||||||||||||||||||||
| 114 | summary => 'Perl code to sort', | ||||||||||||||||||||||||||||
| 115 | schema => 'code_from_str*', | ||||||||||||||||||||||||||||
| 116 | tags => ['category:sorting'], | ||||||||||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||||||||||
| 118 | by_sortsub => { | ||||||||||||||||||||||||||||
| 119 | summary => 'Sort::Sub routine name to sort', | ||||||||||||||||||||||||||||
| 120 | schema =>'str*', | ||||||||||||||||||||||||||||
| 121 | 'x.completion' => ['sortsub_spec'], | ||||||||||||||||||||||||||||
| 122 | tags => ['category:sorting'], | ||||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||
| 124 | sortsub_args => { | ||||||||||||||||||||||||||||
| 125 | summary => 'Arguments to pass to Sort::Sub routine', | ||||||||||||||||||||||||||||
| 126 | schema => ['hash*', of=>'str*'], | ||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||
| 128 | by_field => { | ||||||||||||||||||||||||||||
| 129 | summary => 'Field name to sort against', | ||||||||||||||||||||||||||||
| 130 | schema => ['str*', in=>\@file_fields], | ||||||||||||||||||||||||||||
| 131 | tags => ['category:sorting'], | ||||||||||||||||||||||||||||
| 132 | }, | ||||||||||||||||||||||||||||
| 133 | reverse => { | ||||||||||||||||||||||||||||
| 134 | summary => 'Reverse order of sorting', | ||||||||||||||||||||||||||||
| 135 | schema => 'true*', | ||||||||||||||||||||||||||||
| 136 | cmdline_aliases => {r=>{}}, | ||||||||||||||||||||||||||||
| 137 | tags => ['category:sorting'], | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | key => { | ||||||||||||||||||||||||||||
| 140 | summary => 'Perl code to generate key to sort against', | ||||||||||||||||||||||||||||
| 141 | schema => 'code_from_str*', | ||||||||||||||||||||||||||||
| 142 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | If `key` option is not specified, then: 1) if sorting is `by_code` then the code | ||||||||||||||||||||||||||||
| 145 | will receive files as records (hashes) with keys like `name`, `size`, etc; 2) if | ||||||||||||||||||||||||||||
| 146 | sorting is `by_field` then the associated field is used as key; 3) if sorting is | ||||||||||||||||||||||||||||
| 147 | `by_sortsub` then by default the `name` field will be used as the key. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | To select a field, use this: | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | '$_->{FIELDNAME}' | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | for example: | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | '$_->{size}' | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Another example, to generate length of name as key: | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 'length($_->{name})' | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | MARKDOWN | ||||||||||||||||||||||||||||
| 162 | tags => ['category:sorting'], | ||||||||||||||||||||||||||||
| 163 | }, | ||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | our %argspec_ignore_case = ( | |||||||||||||||||||||||||||
| 167 | ignore_case => { | ||||||||||||||||||||||||||||
| 168 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 169 | cmdline_aliases => {i=>{}}, | ||||||||||||||||||||||||||||
| 170 | tags => ['category:sorting'], | ||||||||||||||||||||||||||||
| 171 | }, | ||||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | $SPEC{sort_files} = { | ||||||||||||||||||||||||||||
| 175 | 1 | v => 1.1, | |||||||||||||||||||||||||||
| 176 | summary => 'Sort files in one or more directories and display the result in a flexible way', | ||||||||||||||||||||||||||||
| 177 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | MARKDOWN | ||||||||||||||||||||||||||||
| 181 | args => { | ||||||||||||||||||||||||||||
| 182 | %argspecs_common, | ||||||||||||||||||||||||||||
| 183 | %argspecs_sort, | ||||||||||||||||||||||||||||
| 184 | }, | ||||||||||||||||||||||||||||
| 185 | args_rels => { | ||||||||||||||||||||||||||||
| 186 | 'choose_one&' => [ | ||||||||||||||||||||||||||||
| 187 | [qw/by_field by_sortsub by_code/], | ||||||||||||||||||||||||||||
| 188 | [qw/num_results num_ranks/], | ||||||||||||||||||||||||||||
| 189 | ], | ||||||||||||||||||||||||||||
| 190 | }, | ||||||||||||||||||||||||||||
| 191 | }; | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub sort_files { | ||||||||||||||||||||||||||
|
Subroutine
sort_files: not called CC=32 SCAR=69.6
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | my $all = $args{all} // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"all"} // 0
MC/DC0%0/1
$args{"all"} // 0
$args{"all"}
Truth table0%0/2
A
A= $args{"all"}
| |||||||||||||||||||||||||||||
| 195 | 0 | my $recursive = $args{recursive}; | |||||||||||||||||||||||||||
| 196 | 0 | ▶ | my $dirs = $args{dirs} // ['.']; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"dirs"} // ["."]
MC/DC0%0/1
$args{"dirs"} // ["."]
$args{"dirs"}
Truth table0%0/2
A
A= $args{"dirs"}
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | my $code_get_recs; | |||||||||||||||||||||||||||
| 199 | 0 | ▶ | $code_get_recs = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=21 SCAR=61.4
| |||||||||||||||||||||||||||||
| 200 | 0 | my ($dir, $prefix) = @_; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | opendir my $dh, $dir or return [500, "Can't opendir '$dir': $!"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | local $CWD = $dir; | |||||||||||||||||||||||||||
| 205 | 0 | my @recs; | |||||||||||||||||||||||||||
| 206 | FILE: | ||||||||||||||||||||||||||||
| 207 | 0 | ▶ | while (defined(my $e = readdir $dh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | next if $e eq '.' || $e eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$e eq "." or $e eq ".."
MC/DC0%0/2
$e eq "." or $e eq ".."
$e eq "."
$e eq ".."
Truth table0%0/3
A or B
A= $e eq "."B= $e eq ".."
| |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if (defined $args{include_filename_pattern}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | unless ($e =~ $args{include_filename_pattern}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | log_trace "File $e excluded (not match $args{include_filename_pattern})"; | |||||||||||||||||||||||||||
| 212 | 0 | next FILE; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if (defined $args{exclude_filename_pattern}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | if ($e =~ $args{exclude_filename_pattern}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | log_trace "File $e excluded (matches $args{exclude_filename_pattern})"; | |||||||||||||||||||||||||||
| 218 | 0 | next FILE; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 0 | ▶ | if (!$all) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if ($e =~ /\A\./) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | log_trace "File $e excluded (dotfile hidden)"; | |||||||||||||||||||||||||||
| 224 | 0 | next FILE; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | my $rec = {name=>$e, dir=>(defined $prefix ? "$prefix/$dir" : $dir)}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my @st = lstat $e or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | warn "Can't stat '$e' in '$dir': $!, skipped"; | |||||||||||||||||||||||||||
| 230 | 0 | next; | |||||||||||||||||||||||||||
| 231 | }; | ||||||||||||||||||||||||||||
| 232 | 0 | $rec->{size} = $st[7]; | |||||||||||||||||||||||||||
| 233 | 0 | $rec->{mtime} = $st[9]; | |||||||||||||||||||||||||||
| 234 | 0 | $rec->{ctime} = $st[10]; | |||||||||||||||||||||||||||
| 235 | 0 | $rec->{mode} = $st[2]; | |||||||||||||||||||||||||||
| 236 | FILTER: { | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | if (defined $args{type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | if ($args{type} eq 'file') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | unless ($rec->{mode} & S_IFREG) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 0 | log_trace "File '$e' in '$dir': not a regular file, skipped"; | |||||||||||||||||||||||||||
| 241 | 0 | goto SKIP_ADD_FILE; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } elsif ($args{type} eq 'dir') { | ||||||||||||||||||||||||||||
| 244 | 0 | ▶ | unless ($rec->{mode} & S_IFDIR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | log_trace "File '$e' in '$dir': not a directory, skipped"; | |||||||||||||||||||||||||||
| 246 | 0 | goto SKIP_ADD_FILE; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | } else { | ||||||||||||||||||||||||||||
| 249 | 0 | return [400, "Invalid value of type '$args{type}'"]; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | } # FILTER | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | push @recs, $rec; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | SKIP_ADD_FILE: | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ($recursive && $rec->{mode} & S_IFDIR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$recursive and $rec->{"mode"} & 16384
MC/DC0%0/2
$recursive and $rec->{"mode"} & 16384
$recursive
$rec->{"mode"} & 16384
Truth table0%0/3
A and B
A= $recursiveB= $rec->{"mode"} & 16384
| |||||||||||||||||||||||||||||
| 258 | 0 | log_trace "Recursing into $dir/$e ..."; | |||||||||||||||||||||||||||
| 259 | 0 | my $subres = $code_get_recs->($e, $dir); | |||||||||||||||||||||||||||
| 260 | 0 | ▶ | if ($subres->[0] == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 0 | push @recs, @{ $subres->[2] }; | |||||||||||||||||||||||||||
| 262 | } else { | ||||||||||||||||||||||||||||
| 263 | 0 | log_error "Cannot recurse into $dir/$e: $subres->[0] - $subres->[1]"; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | 0 | closedir $dh; | |||||||||||||||||||||||||||
| 269 | 0 | return [200, "OK", \@recs]; | |||||||||||||||||||||||||||
| 270 | 0 | }; # $code_get_files | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | my @recs; | |||||||||||||||||||||||||||
| 273 | my %seen_abs_paths; | ||||||||||||||||||||||||||||
| 274 | DIR: | ||||||||||||||||||||||||||||
| 275 | 0 | for my $dir (@$dirs) { | |||||||||||||||||||||||||||
| 276 | 0 | my $res = $code_get_recs->($dir); | |||||||||||||||||||||||||||
| 277 | 0 | ▶ | return $res unless $res->[0] == 200; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 0 | for my $rec (@{ $res->[2] }) { | |||||||||||||||||||||||||||
| 279 | #log_trace "rec=%s", $rec; | ||||||||||||||||||||||||||||
| 280 | 0 | my $abs_path = abs_path("$rec->{dir}/$rec->{name}"); | |||||||||||||||||||||||||||
| 281 | 0 | ▶ | push @recs, $rec unless $seen_abs_paths{$abs_path}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | } # DIR | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | my ($code_key, $code_cmp); | |||||||||||||||||||||||||||
| 286 | SET_CODE_CMP: { | ||||||||||||||||||||||||||||
| 287 | 0 | ▶ | if (defined $args{key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 0 | $code_key = $args{key}; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | if (defined $args{by_code}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $code_key //= sub { $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$code_key //= sub {
$_[0];
}
MC/DC0%0/2
$code_key //= sub {
$_[0];
}
$code_key
sub {
$_[0];
}
Truth table0%0/3
A //= B
A= $code_keyB= sub {
$_[0];
}
| |||||||||||||||||||||||||||||
| 293 | 0 | $code_cmp = $args{by_code}; | |||||||||||||||||||||||||||
| 294 | 0 | last; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if (defined $args{by_sortsub}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | require Sort::Sub; | |||||||||||||||||||||||||||
| 299 | 0 | ▶ | $code_key //= sub { $_[0]->{name} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$code_key //= sub {
$_[0]{"name"};
}
MC/DC0%0/2
$code_key //= sub {
$_[0]{"name"};
}
$code_key
sub {
$_[0]{"name"};
}
Truth table0%0/3
A //= B
A= $code_keyB= sub {
$_[0]{"name"};
}
| |||||||||||||||||||||||||||||
| 300 | 0 | $code_cmp = Sort::Sub::gen_sorter($args{by_sortsub}, $args{sortsub_args}); | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | ▶ | if (defined $args{by_field}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | if ($args{by_field} eq 'name') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 305 | 0 | ▶ | $code_key //= sub { $_[0]->{name} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$code_key //= sub {
$_[0]{"name"};
}
MC/DC0%0/2
$code_key //= sub {
$_[0]{"name"};
}
$code_key
sub {
$_[0]{"name"};
}
Truth table0%0/3
A //= B
A= $code_keyB= sub {
$_[0]{"name"};
}
| |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | $code_cmp = sub { $_[0] cmp $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 307 | } elsif ($args{by_field} eq 'size') { | ||||||||||||||||||||||||||||
| 308 | 0 | ▶ | $code_key //= sub { $_[0]->{size} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$code_key //= sub {
$_[0]{"size"};
}
MC/DC0%0/2
$code_key //= sub {
$_[0]{"size"};
}
$code_key
sub {
$_[0]{"size"};
}
Truth table0%0/3
A //= B
A= $code_keyB= sub {
$_[0]{"size"};
}
| |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | $code_cmp = sub { $_[0] <=> $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 310 | } elsif ($args{by_field} eq 'ctime') { | ||||||||||||||||||||||||||||
| 311 | 0 | ▶ | $code_key //= sub { $_[0]->{ctime} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$code_key //= sub {
$_[0]{"ctime"};
}
MC/DC0%0/2
$code_key //= sub {
$_[0]{"ctime"};
}
$code_key
sub {
$_[0]{"ctime"};
}
Truth table0%0/3
A //= B
A= $code_keyB= sub {
$_[0]{"ctime"};
}
| |||||||||||||||||||||||||||||
| 312 | 0 | ▶ | $code_cmp = sub { $_[0] <=> $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 313 | } elsif ($args{by_field} eq 'mtime') { | ||||||||||||||||||||||||||||
| 314 | 0 | ▶ | $code_key //= sub { $_[0]->{mtime} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$code_key //= sub {
$_[0]{"mtime"};
}
MC/DC0%0/2
$code_key //= sub {
$_[0]{"mtime"};
}
$code_key
sub {
$_[0]{"mtime"};
}
Truth table0%0/3
A //= B
A= $code_keyB= sub {
$_[0]{"mtime"};
}
| |||||||||||||||||||||||||||||
| 315 | 0 | ▶ | $code_cmp = sub { $_[0] <=> $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 316 | } else { | ||||||||||||||||||||||||||||
| 317 | 0 | return [400, "Invalid value in by_field: $args{by_field}"]; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 0 | last; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | return [400, "Please specify one of by_field/by_sortsub/by_code"]; | |||||||||||||||||||||||||||
| 323 | } # SET_CODE_CMP | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 0 | my (@ranks, @sorted_recs); | |||||||||||||||||||||||||||
| 326 | SORT: { | ||||||||||||||||||||||||||||
| 327 | 0 | require List::Rank; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | my @res = List::Rank::sortrankby( | ||||||||||||||||||||||||||||
| 330 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 331 | ; | ||||||||||||||||||||||||||||
| 332 | 0 | my ($key1, $key2); | |||||||||||||||||||||||||||
| 333 | { | ||||||||||||||||||||||||||||
| 334 | 0 | local $_ = $a; | |||||||||||||||||||||||||||
| 335 | 0 | $key1 = $code_key->($a); | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | { | ||||||||||||||||||||||||||||
| 338 | 0 | local $_ = $b; | |||||||||||||||||||||||||||
| 339 | 0 | $key2 = $code_key->($b); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | 0 | ▶ | $args{reverse} ? $code_cmp->($key2, $key1) : $code_cmp->($key1, $key2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | }, | ||||||||||||||||||||||||||||
| 343 | @recs | ||||||||||||||||||||||||||||
| 344 | 0 | ); | |||||||||||||||||||||||||||
| 345 | 0 | ▶ | while (my ($e1, $e2) = splice @res, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 0 | push @sorted_recs, $e1; | |||||||||||||||||||||||||||
| 347 | 0 | push @ranks, $e2; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | } # SORT | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | NUM_RESULTS: { | ||||||||||||||||||||||||||||
| 352 | 0 | ▶ | last unless defined $args{num_results} && $args{num_results} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} and $args{"num_results"} > 0
MC/DC0%0/2
defined $args{"num_results"} and $args{"num_results"} > 0
defined $args{"num_results"}
$args{"num_results"} > 0
Truth table0%0/3
A and B
A= defined $args{"num_results"}B= $args{"num_results"} > 0
| |||||||||||||||||||||||||||||
| 353 | 0 | ▶ | last unless $args{num_results} < @sorted_recs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 0 | splice @sorted_recs, $args{num_results}; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | NUM_RANKS: { | ||||||||||||||||||||||||||||
| 358 | 0 | ▶ | last unless defined $args{num_ranks} && $args{num_ranks} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_ranks"} and $args{"num_ranks"} > 0
MC/DC0%0/2
defined $args{"num_ranks"} and $args{"num_ranks"} > 0
defined $args{"num_ranks"}
$args{"num_ranks"} > 0
Truth table0%0/3
A and B
A= defined $args{"num_ranks"}B= $args{"num_ranks"} > 0
| |||||||||||||||||||||||||||||
| 359 | 0 | my @res; | |||||||||||||||||||||||||||
| 360 | 0 | my $i = -1; | |||||||||||||||||||||||||||
| 361 | 0 | while (1) { | |||||||||||||||||||||||||||
| 362 | 0 | $i++; | |||||||||||||||||||||||||||
| 363 | 0 | ▶ | last if $i >= @sorted_recs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | 0 | my $rank = $ranks[$i]; | |||||||||||||||||||||||||||
| 365 | 0 | $rank =~ s/=\z//; | |||||||||||||||||||||||||||
| 366 | 0 | ▶ | last if $rank > $args{num_ranks}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 0 | push @res, $sorted_recs[$i]; | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | 0 | @sorted_recs = @res; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 0 | ▶ | if ($args{detail}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | } else { | ||||||||||||||||||||||||||||
| 374 | 0 | ▶ | @sorted_recs = map { ($_->{dir} eq '.' ? '' : $_->{dir} eq '/' ? '/' : "$_->{dir}/") . $_->{name} } @sorted_recs; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | 0 | log_info "Sorted files: %s", \@sorted_recs; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 0 | [200, "OK", \@sorted_recs]; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # foremost | ||||||||||||||||||||||||||||
| 382 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 383 | die => 1, | ||||||||||||||||||||||||||||
| 384 | output_name => 'foremost', | ||||||||||||||||||||||||||||
| 385 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 386 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 387 | add_args => { | ||||||||||||||||||||||||||||
| 388 | %argspec_ignore_case, | ||||||||||||||||||||||||||||
| 389 | }, | ||||||||||||||||||||||||||||
| 390 | summary => 'Return file(s) which are alphabetically the first', | ||||||||||||||||||||||||||||
| 391 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | Notes: | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | - by default dotfiles are not included, use `--all` (`-a`) to include them | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | Some examples: | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | # return foremost file in current directory | ||||||||||||||||||||||||||||
| 400 | % foremost -f | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | MARKDOWN | ||||||||||||||||||||||||||||
| 403 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 404 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 407 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | sort_files( | ||||||||||||||||||||||||||||
| 411 | %args, | ||||||||||||||||||||||||||||
| 412 | by_field => 'name', | ||||||||||||||||||||||||||||
| 413 | 0 | ▶ | ($args{ignore_case} ? (key=>sub {lc $_[0]{name}}) : ()), | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | ); | ||||||||||||||||||||||||||||
| 415 | }, | ||||||||||||||||||||||||||||
| 416 | 1 | ); | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # hindmost | ||||||||||||||||||||||||||||
| 419 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 420 | die => 1, | ||||||||||||||||||||||||||||
| 421 | output_name => 'hindmost', | ||||||||||||||||||||||||||||
| 422 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 423 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 424 | add_args => { | ||||||||||||||||||||||||||||
| 425 | %argspec_ignore_case, | ||||||||||||||||||||||||||||
| 426 | }, | ||||||||||||||||||||||||||||
| 427 | summary => 'Return file(s) which are alphabetically the last', | ||||||||||||||||||||||||||||
| 428 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | Notes: | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | - by default dotfiles are not included, use `--all` (`-a`) to include them | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Some examples: | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # return hindmost file in current directory | ||||||||||||||||||||||||||||
| 437 | % hindmost -f | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | MARKDOWN | ||||||||||||||||||||||||||||
| 440 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 441 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 444 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | sort_files( | ||||||||||||||||||||||||||||
| 448 | %args, | ||||||||||||||||||||||||||||
| 449 | by_field => 'name', | ||||||||||||||||||||||||||||
| 450 | 0 | ▶ | ($args{ignore_case} ? (key=>sub {lc $_[0]{name}}) : ()), | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | reverse => 1, | ||||||||||||||||||||||||||||
| 452 | ); | ||||||||||||||||||||||||||||
| 453 | }, | ||||||||||||||||||||||||||||
| 454 | 1 | ); | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | # largest | ||||||||||||||||||||||||||||
| 457 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 458 | die => 1, | ||||||||||||||||||||||||||||
| 459 | output_name => 'largest', | ||||||||||||||||||||||||||||
| 460 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 461 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 462 | summary => 'Return the largest file(s) in one or more directories', | ||||||||||||||||||||||||||||
| 463 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | Some examples: | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | # return largest file in current directory | ||||||||||||||||||||||||||||
| 468 | % largest -f | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | # return largest file(s) in /some/dir (if there are multiple files with the | ||||||||||||||||||||||||||||
| 471 | # same size they will all be returned | ||||||||||||||||||||||||||||
| 472 | % largest -N1 -f /some/dir | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | MARKDOWN | ||||||||||||||||||||||||||||
| 475 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 476 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 479 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | sort_files( | ||||||||||||||||||||||||||||
| 483 | 0 | %args, | |||||||||||||||||||||||||||
| 484 | by_field => 'size', | ||||||||||||||||||||||||||||
| 485 | reverse => 1, | ||||||||||||||||||||||||||||
| 486 | ); | ||||||||||||||||||||||||||||
| 487 | }, | ||||||||||||||||||||||||||||
| 488 | 1 | ); | |||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | # smallest | ||||||||||||||||||||||||||||
| 491 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 492 | die => 1, | ||||||||||||||||||||||||||||
| 493 | output_name => 'smallest', | ||||||||||||||||||||||||||||
| 494 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 495 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 496 | summary => 'Return the smallest file(s) in one or more directories', | ||||||||||||||||||||||||||||
| 497 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | Some examples: | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | # return smallest file in current directory | ||||||||||||||||||||||||||||
| 502 | % smallest -f | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | # return smallest file(s) in /some/dir (if there are multiple files with the | ||||||||||||||||||||||||||||
| 505 | # same size they will all be returned | ||||||||||||||||||||||||||||
| 506 | % smallest -N1 -f /some/dir | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | MARKDOWN | ||||||||||||||||||||||||||||
| 509 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 510 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 513 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | sort_files( | ||||||||||||||||||||||||||||
| 517 | 0 | %args, | |||||||||||||||||||||||||||
| 518 | by_field => 'size', | ||||||||||||||||||||||||||||
| 519 | ); | ||||||||||||||||||||||||||||
| 520 | }, | ||||||||||||||||||||||||||||
| 521 | 1 | ); | |||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | # newest | ||||||||||||||||||||||||||||
| 524 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 525 | die => 1, | ||||||||||||||||||||||||||||
| 526 | output_name => 'newest', | ||||||||||||||||||||||||||||
| 527 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 528 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 529 | summary => 'Return the newest file(s) in one or more directories', | ||||||||||||||||||||||||||||
| 530 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | Notes: | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | - by default dotfiles are not included, use `--all` (`-a`) to include them | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | Suppose a new file is downloaded in `~/Downloads`, but you are not sure of its | ||||||||||||||||||||||||||||
| 537 | name. You just want to move that file, which you are pretty sure is the newest | ||||||||||||||||||||||||||||
| 538 | in the `Downloads` directory, somewhere else. So from the CLI in `~/Downloads`: | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | % mv `newest -f` /somewhere/else | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | or, from `/somewhere/else`: | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | % mv `newest -f ~/Downloads` . | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | If you want to see the filename on stderr as well: | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | % mv `newest --verbose -f ~/Downloads` . | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | File is deemed as newest by its mtime. | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | Some examples: | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | # return newest file in current directory | ||||||||||||||||||||||||||||
| 555 | % newest -f | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | # return newest file(s) in /some/dir (if there are multiple files with the | ||||||||||||||||||||||||||||
| 558 | # same newest mtime) they will all be returned | ||||||||||||||||||||||||||||
| 559 | % newest -N1 -f /some/dir | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | MARKDOWN | ||||||||||||||||||||||||||||
| 562 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 563 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 566 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | sort_files( | ||||||||||||||||||||||||||||
| 570 | 0 | %args, | |||||||||||||||||||||||||||
| 571 | by_field => 'mtime', | ||||||||||||||||||||||||||||
| 572 | reverse => 1, | ||||||||||||||||||||||||||||
| 573 | ); | ||||||||||||||||||||||||||||
| 574 | }, | ||||||||||||||||||||||||||||
| 575 | 1 | ); | |||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | # oldest | ||||||||||||||||||||||||||||
| 578 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 579 | die => 1, | ||||||||||||||||||||||||||||
| 580 | output_name => 'oldest', | ||||||||||||||||||||||||||||
| 581 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 582 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 583 | summary => 'Return the oldest file(s) in one or more directories', | ||||||||||||||||||||||||||||
| 584 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | Notes: | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | - by default dotfiles are not included, use `--all` (`-a`) to include them | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | File is deemed as oldest by its mtime. | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | Some examples: | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | # return oldest file in current directory | ||||||||||||||||||||||||||||
| 595 | % oldest -f | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | # return oldest file(s) in /some/dir (if there are multiple files with the | ||||||||||||||||||||||||||||
| 598 | # same oldest mtime) they will all be returned | ||||||||||||||||||||||||||||
| 599 | % oldest -N1 -f /some/dir | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | MARKDOWN | ||||||||||||||||||||||||||||
| 602 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 603 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 606 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | sort_files( | ||||||||||||||||||||||||||||
| 610 | 0 | %args, | |||||||||||||||||||||||||||
| 611 | by_field => 'mtime', | ||||||||||||||||||||||||||||
| 612 | ); | ||||||||||||||||||||||||||||
| 613 | }, | ||||||||||||||||||||||||||||
| 614 | 1 | ); | |||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | # longest_name | ||||||||||||||||||||||||||||
| 617 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 618 | die => 1, | ||||||||||||||||||||||||||||
| 619 | output_name => 'longest_name', | ||||||||||||||||||||||||||||
| 620 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 621 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 622 | summary => 'Return file(s) with the longest name in one or more directories', | ||||||||||||||||||||||||||||
| 623 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | Notes: | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | - by default dotfiles are not included, use `--all` (`-a`) to include them | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | Some examples: | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | # return file with the longest name in current directory | ||||||||||||||||||||||||||||
| 632 | % longest-name -f | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | # return file(s) with the longest name in /some/dir. if there are multiple | ||||||||||||||||||||||||||||
| 635 | # files with the same length, they will all be returned. | ||||||||||||||||||||||||||||
| 636 | % longest-name -N1 -f /some/dir | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | MARKDOWN | ||||||||||||||||||||||||||||
| 639 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 640 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 643 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | sort_files( | ||||||||||||||||||||||||||||
| 647 | %args, | ||||||||||||||||||||||||||||
| 648 | 0 | ▶ | by_code => sub { length($_[1]{name}) <=> length($_[0]{name}) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 649 | 0 | ); | |||||||||||||||||||||||||||
| 650 | }, | ||||||||||||||||||||||||||||
| 651 | 1 | ); | |||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | # shortest_name | ||||||||||||||||||||||||||||
| 654 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 655 | die => 1, | ||||||||||||||||||||||||||||
| 656 | output_name => 'shortest_name', | ||||||||||||||||||||||||||||
| 657 | base_name => 'sort_files', | ||||||||||||||||||||||||||||
| 658 | remove_args => [keys %argspecs_sort], | ||||||||||||||||||||||||||||
| 659 | summary => 'Return file(s) with the shortest name in one or more directories', | ||||||||||||||||||||||||||||
| 660 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | Notes: | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | - by default dotfiles are not included, use `--all` (`-a`) to include them | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | Some examples: | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | # return file with the shortest name in current directory | ||||||||||||||||||||||||||||
| 669 | % shortest-name -f | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | # return file(s) with the shortest name in /some/dir. if there are multiple | ||||||||||||||||||||||||||||
| 672 | # files with the same length, they will all be returned. | ||||||||||||||||||||||||||||
| 673 | % shortest-name -N1 -f /some/dir | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | MARKDOWN | ||||||||||||||||||||||||||||
| 676 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 677 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | 0 | ▶ | unless (defined $args{num_results} || defined $args{num_ranks}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"num_results"} or defined $args{"num_ranks"}
MC/DC0%0/2
defined $args{"num_results"} or defined $args{"num_ranks"}
defined $args{"num_results"}
defined $args{"num_ranks"}
Truth table0%0/3
A or B
A= defined $args{"num_results"}B= defined $args{"num_ranks"}
| |||||||||||||||||||||||||||||
| 680 | 0 | $args{num_results} = 1; | |||||||||||||||||||||||||||
| 681 | } | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | sort_files( | ||||||||||||||||||||||||||||
| 684 | %args, | ||||||||||||||||||||||||||||
| 685 | 0 | ▶ | by_code => sub { length($_[0]{name}) <=> length($_[1]{name}) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 686 | 0 | ); | |||||||||||||||||||||||||||
| 687 | }, | ||||||||||||||||||||||||||||
| 688 | 1 | ); | |||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | 1 | 1; | |||||||||||||||||||||||||||
| 691 | # ABSTRACT: Routines related to sorting files in one or more directories | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | __END__ | ||||||||||||||||||||||||||||