--- 535607.pl.tdy Fri Mar 10 21:16:23 2006 +++ dir-explore.pl Fri Mar 10 21:43:49 2006 @@ -1,3 +1,5 @@ +#!perl + use warnings; use strict; use Tk; @@ -10,10 +12,14 @@ my $main = MainWindow->new( -title => "Folder stats for $rootPath" ); my $tree = $main->ScrlTree( - -font => 'FixedSys 8', - -itemtype => 'text', - -separator => '/', - -scrollbars => "osoe" + #-font => 'FixedSys 8', + -itemtype => 'text', + -separator => '/', + + # Having scrollbars-only-when-needed-option, 'o', does not make the + # scrollbars appear when the content overflows display area (FreeBSD + # 6-STABLE & Tk-804.027). + -scrollbars => 'sw' ); my @pathStack = (1); @@ -26,11 +32,9 @@ my ( $subDirCount, $subFileCount, $subTotalSize ) = buildSubTree( $tree, $rootPath, \@pathStack, \$maxNesting, \$totalLines, \$maxLineLength ); -my $annotation = $rootPath . " \t("; -my $plural = $subDirCount != 1 ? 's' : ''; -$annotation .= $subDirCount . " dir$plural, " if $subDirCount; -$annotation .= "$subFileCount files, $subTotalSize bytes)"; -$tree->entryconfigure( $currPath, -text => $annotation ); +$tree->entryconfigure( $currPath, + -text => annotate( $rootPath, $subDirCount, $subFileCount, $subTotalSize ) +); $totalLines = 40 if $totalLines > 40; $main->geometry( @@ -79,7 +83,7 @@ $tree->add( $currPath, -text => $nodeText ); - if ( -d $path ) { + if ( -d $path && !-l $path ) { my ( $subDirCount, $subFileCount, $subTotalSize ) = buildSubTree( $tree, $path, $pathStack, $maxNesting, $totalLines, $maxLineLength ); @@ -88,17 +92,19 @@ $fileCount += $subFileCount; $sizeTotal += $subTotalSize; - my $annotation = $nodeText . " \t("; - my $plural = $subDirCount != 1 ? 's' : ''; - $annotation .= $subDirCount . " dir$plural, " if $subDirCount; - $annotation .= "$subFileCount files, $subTotalSize bytes)"; - $tree->entryconfigure( $currPath, -text => $annotation ); + $tree->entryconfigure( + $currPath, + -text => annotate( + $nodeText, $subDirCount, $subFileCount, $subTotalSize + ) + ); } else { my $fileSize = -s $path; $tree->entryconfigure( $currPath, - -text => $nodeText . " ($fileSize bytes)" ); + -text => $nodeText . ' (' . size_in_kilobyte($fileSize) . ')' ); ++$fileCount; +#warn $path unless defined $fileSize; $sizeTotal += $fileSize; } @@ -110,6 +116,25 @@ closedir $dir; pop @$pathStack; return ( $dirCount, $fileCount, $sizeTotal ); +} + +sub annotate { + my ( $path, $dirs, $files, $byte_size ) = @_; + return + $path + . " \t(" + . $dirs . ' dir' . count_to_plural_suffix($dirs) . ', ' + . $files . ' file' . count_to_plural_suffix($files) . ', ' + . size_in_kilobyte($byte_size) + . ')'; +} + +sub count_to_plural_suffix { + return $_[0] > 1 ? 's' : ''; +} + +sub size_in_kilobyte { + defined $_[0] ? sprintf( '%0.1f', $_[0] / 1024 ) . ' kB' : 'UNKNOWN SIZE'; } sub ShowHelp {