App-dirdim
view release on metacpan or search on metacpan
#!/usr/bin/perl
#use 5.001 ;
use strict ;
use warnings ;
use feature qw[ say ] ;
use Cwd qw [ getcwd ] ;
use App::dirdim ; # $App::dirdim::VERSIONãå©ç¨ãããããéçºä¸ã¯ãæ¢ã«ã¤ã³ã¹ãã¼ã«æ¸ã¿ã®ãã®ã使ã£ã¦ãã¾ãã®ã§ãæå³ãã«ããåä½ã«ãªãã ããã
use File::Find qw[ find ] ;
use File::Spec::Functions qw[ catfile splitdir ] ;
use Getopt::Std ;
use List::Util qw[ minstr maxstr all any max ] ;
use Term::ANSIColor qw [ :constants ] ; ${Term::ANSIColor::AUTORESET} = 1 ;
use Time::HiRes qw[gettimeofday tv_interval] ;
my $time_start ;# = [ gettimeofday ] ;
BEGIN { $time_start = [ gettimeofday ] }; # BEGIN UNITCHECK CHECK INIT # å使éã®æ¸¬å®ãã§ããã ãæ£ç¢ºã«ããããBEGINã§å²ãã ã
my $I = catfile '', '' ; # OSæ¯ã«ç°ãªãå¯è½æ§ã®ããããã¡ã¤ã«ãã¹ã®åºåãæåãåå¾ã
getopts '.:adv' , \my%o ;
$o{'.'} //= '' ;
@ARGV = ( "." ) unless @ARGV ; # & HELP_MESSAGE unless @ARGV ;
my ($y,$z) = (0,0) ; # çµæçã«å¦ç対象ã¨ãªã£ããã®ã¨ãããã§ãªããã®
& measure_simple ;
exit 0 ;
sub measure_simple {
my @tmp ;
push @tmp , ( "nondir" , CYAN ( "subdir" ) ) ;
push @tmp , ( BRIGHT_BLUE ("depth"), "alltree" ) if defined $o{d} ;
push @tmp , YELLOW "given_dir" ;
push @tmp , BRIGHT_BLUE "An example at the deepest" if defined $o{d} ;
push @tmp , ("fileName_minstr" , "fileName_maxstr" ) if $o{v} ;
say join "\t" , map { UNDERLINE $_ } @tmp ;
my %t ; # åè¨ total ã ããããªãã®ã§æ°ããã
my $dir0 = getcwd ;
for ( @ARGV ) {
chomp ;
do { print join("\t",'','',$_,RED ": Not a directory."),"\n" if $o{a} ; $z++ ; next } if ! -d $_ ;
opendir my $dh , $_ or do { warn "$_ does not open.\n" ; next } ;
my @files = grep { ! /^\.{1,2}$/ } readdir $dh ; # ãã®ãã£ã¬ã¯ããªã®ä¸ã®åãã¡ã¤ã«ã«ã¤ãã¦ããã¡ã¤ã«åã®ã¿ãå
¥ã(ãã£ã¬ãã¯ãªåã¯å
¥ããªã)ã
@files = grep {/^\./ } @files if $o{'.'} =~ m/only/io ; # -. ãªããããããã¡ã¤ã«ã®ã¿ã対象ã¨ããã
@files = grep { ! /^\./ } @files if 0 eq $o{'.'} ; # -. 0 ãªããããããã¡ã¤ã«ã¯å¯¾è±¡å¤ã¨ããã
chdir $_ ;
my ( @dfiles , @nfiles ) ; # ãã£ã¬ã¯ããªããã以å¤ã
for ( @files ) { if ( -d ) { push @dfiles, $_ ; $_ .= $I } else { push @nfiles, $_ } }
my ( $dp, $dn , $finum, $links ) = max_depth ( $_ ) if $o{d} ; # <-- è¿å¤ã¯ãæ·±ãã»ææ·±ãã¡ã¤ã«åã®ä¾ã»è¨æ°ãããã¡ã¤ã«æ°ã»ã·ã³ããªãã¯ãªæ°
chdir $dir0 ;
do { $t{d} += @dfiles ; $t{n} += @nfiles ; $t{D} = max $t{D}//0, $dp if defined $dp ; $t{a} += $finum // 0 } ;
my @out ;
push @out , fsl ( @nfiles ) , CYAN fsl ( @dfiles ) ;
push @out , BRIGHT_BLUE ("$dp"), "$finum" . ($links ? FAINT "($links)" : '') if defined $dp ; # æãæ·±ãæã®æ·±ã ããã¨æ°ãããã¡ã¤ã«æ°
push @out , YELLOW $_ . $I ;
push @out , BRIGHT_BLUE $dn if defined $dn ; # æãæ·±ãæã«ãããã¡ã¤ã«ã®åå
if ( $o{v} ) {
my $f1 = minstr @files ;
my $f2 = maxstr @files ;
push @out , ($f1) if defined $f1 ;
push @out , ($f2) if defined $f1 && $f1 ne $f2 ;
}
say join "\t", @out ;
$y ++ ;
#closedir $dh ;
}
say join "\t" , $t{n},$t{d}, $o{d}?($t{D},$t{a}):(), 'total' if $y >= 2 ;
}
END{
my $time_elapsed = sprintf '%.4f', tv_interval ( $time_start , [ gettimeofday ] ) ;
my $end ;
$end .= "$y entries. " if $y > 1 ;
$end .= "${time_elapsed}s. " ;
$end .= "The numbers of files are shown in left columns. " ;
$end .= "$z entrie(s) are suppressed in the processing. " if $z ;
( run in 0.926 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )