App-dirdim

 view release on metacpan or  search on metacpan

dirdim  view on Meta::CPAN

#!/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 )