mirror of
				https://codeberg.org/h3xx/simplify_static_dir
				synced 2024-08-14 23:57:24 +00:00 
			
		
		
		
	Update options processing
- Make it more obvious in the code what the options do. - Omit output of progress bar unless -v flag is present.
This commit is contained in:
		
							parent
							
								
									89b64b1ca1
								
							
						
					
					
						commit
						5454fe681f
					
				
					 2 changed files with 39 additions and 33 deletions
				
			
		|  | @ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. | ||||||
| ### Changed | ### Changed | ||||||
| 
 | 
 | ||||||
| - Limit "Skipped" output to only summary | - Limit "Skipped" output to only summary | ||||||
|  | - Omit output of progress bar unless -v flag is present | ||||||
| 
 | 
 | ||||||
| ## [3.0.0] | ## [3.0.0] | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -120,15 +120,14 @@ EOF | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| MAIN: { | MAIN: { | ||||||
|     my %opts = ( |     &getopts('vfm:M:z', \ my %opts); | ||||||
|         v => 0, |  | ||||||
|         f => 0, |  | ||||||
|         m => '', |  | ||||||
|         M => '', |  | ||||||
|         z => 0, |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     &getopts('vfm:M:z', \%opts); |     my $verbose = defined $opts{v}; | ||||||
|  |     my $print_freed = defined $opts{f}; | ||||||
|  |     my $files_match = $opts{m} || ''; | ||||||
|  |     my $files_exclude = $opts{M} || ''; | ||||||
|  |     my $include_zero_length_files = defined $opts{z}; | ||||||
|  |     my $print_progress = $verbose; | ||||||
| 
 | 
 | ||||||
|     # correct relative paths |     # correct relative paths | ||||||
|     # OR if no directories given, search the current directory |     # OR if no directories given, search the current directory | ||||||
|  | @ -136,7 +135,7 @@ MAIN: { | ||||||
| 
 | 
 | ||||||
|     my @files; |     my @files; | ||||||
|     print STDERR 'Finding files...' |     print STDERR 'Finding files...' | ||||||
|         if $opts{v}; |         if $verbose; | ||||||
| 
 | 
 | ||||||
|     &find(sub { |     &find(sub { | ||||||
|         # outright skip directories (don't report skip) |         # outright skip directories (don't report skip) | ||||||
|  | @ -152,7 +151,7 @@ MAIN: { | ||||||
| 
 | 
 | ||||||
|     printf STDERR "%d files found", |     printf STDERR "%d files found", | ||||||
|         scalar @files |         scalar @files | ||||||
|         if $opts{v}; |         if $verbose; | ||||||
| 
 | 
 | ||||||
|     # Limit to or exclude file patterns specified by `-m' or `-M', respectively |     # Limit to or exclude file patterns specified by `-m' or `-M', respectively | ||||||
|     # |     # | ||||||
|  | @ -165,22 +164,22 @@ MAIN: { | ||||||
|     # note: m// will match everything |     # note: m// will match everything | ||||||
|     my $file_ct_before_filter = scalar @files; |     my $file_ct_before_filter = scalar @files; | ||||||
|     @files = grep { |     @files = grep { | ||||||
|         $_->{rel_name} =~ $opts{m} |         $_->{rel_name} =~ $files_match | ||||||
|     } @files; |     } @files; | ||||||
|     if ($file_ct_before_filter != scalar @files) { |     if ($file_ct_before_filter != scalar @files) { | ||||||
|         printf STDERR " (%d files filtered by -m rule)", |         printf STDERR " (%d files filtered by -m rule)", | ||||||
|             $file_ct_before_filter - scalar @files |             $file_ct_before_filter - scalar @files | ||||||
|             if $opts{v}; |             if $verbose; | ||||||
|     } |     } | ||||||
|     if (length $opts{M}) { |     if (length $files_exclude) { | ||||||
|         $file_ct_before_filter = scalar @files; |         $file_ct_before_filter = scalar @files; | ||||||
|         @files = grep { |         @files = grep { | ||||||
|             not $_->{rel_name} =~ $opts{M} |             not $_->{rel_name} =~ $files_exclude | ||||||
|         } @files; |         } @files; | ||||||
|         if ($file_ct_before_filter != scalar @files) { |         if ($file_ct_before_filter != scalar @files) { | ||||||
|             printf STDERR " (%d files filtered by -M rule)", |             printf STDERR " (%d files filtered by -M rule)", | ||||||
|                 $file_ct_before_filter - scalar @files |                 $file_ct_before_filter - scalar @files | ||||||
|                 if $opts{v}; |                 if $verbose; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -195,21 +194,23 @@ MAIN: { | ||||||
| 
 | 
 | ||||||
|     printf STDERR " (%d candidates).\n", |     printf STDERR " (%d candidates).\n", | ||||||
|         scalar @files |         scalar @files | ||||||
|         if $opts{v}; |         if $verbose; | ||||||
| 
 | 
 | ||||||
|     unless (@files) { |     unless (@files) { | ||||||
|         printf STDERR "Nothing to do.\n"; |         printf STDERR "Nothing to do.\n"; | ||||||
|         exit 0; |         exit 0; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     print STDERR "Generating hashes..." if $opts{v}; |     print STDERR "Generating hashes..." if $verbose; | ||||||
|     my $filehash = Directory::Simplify::FileHash->new; |     my $filehash = Directory::Simplify::FileHash->new; | ||||||
|     my $report_every = 1; # seconds |     my $report_every = 1; # seconds | ||||||
|     my $processed_bytes = 0; |     my $processed_bytes = 0; | ||||||
|     my $last_report = time; |     my $last_report = time; | ||||||
|     my $total_size_hr = sprintf "%0.4G %s", Directory::Simplify::Utils::hr_size(&sum(map { $_->{size} } @files) or 0); |     my $total_size_hr = sprintf "%0.4G %s", Directory::Simplify::Utils::hr_size(&sum(map { $_->{size} } @files) or 0); | ||||||
|  |     my $cb; | ||||||
|  |     if ($print_progress) { | ||||||
|         printf STDERR "\e\x{37}"; |         printf STDERR "\e\x{37}"; | ||||||
|     my $cb = sub { |         $cb = sub { | ||||||
|             my ($file, $now) = (shift, time); |             my ($file, $now) = (shift, time); | ||||||
|             $processed_bytes += $file->{size}; |             $processed_bytes += $file->{size}; | ||||||
|             if ($now >= $last_report + $report_every) { |             if ($now >= $last_report + $report_every) { | ||||||
|  | @ -219,19 +220,23 @@ MAIN: { | ||||||
|                 $last_report = $now; |                 $last_report = $now; | ||||||
|             } |             } | ||||||
|         }; |         }; | ||||||
|     $filehash->add({ files => \@files, callback => $cb }); |     } | ||||||
|  |     $filehash->add({ | ||||||
|  |         files => \@files, | ||||||
|  |         callback => $cb, | ||||||
|  |     }); | ||||||
|     print STDERR "done.\n" |     print STDERR "done.\n" | ||||||
|         if $opts{v}; |         if $verbose; | ||||||
| 
 | 
 | ||||||
|     my $generator = Directory::Simplify::Instruction::Generator->new( |     my $generator = Directory::Simplify::Instruction::Generator->new( | ||||||
|         filehash => $filehash, |         filehash => $filehash, | ||||||
|         min_size => ($opts{z} ? 0 : 1), |         min_size => ($include_zero_length_files ? 0 : 1), | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     my $freed_bytes = 0; |     my $freed_bytes = 0; | ||||||
| 
 | 
 | ||||||
|     foreach my $inst ($generator->instructions) { |     foreach my $inst ($generator->instructions) { | ||||||
|         print STDERR $inst, "\n" if $opts{v}; |         print STDERR $inst, "\n" if $verbose; | ||||||
|         $inst->run; |         $inst->run; | ||||||
|         $freed_bytes += $inst->bytes_freed; |         $freed_bytes += $inst->bytes_freed; | ||||||
|     } |     } | ||||||
|  | @ -239,7 +244,7 @@ MAIN: { | ||||||
|     printf STDERR "freed %d bytes (%0.4G %s)\n", |     printf STDERR "freed %d bytes (%0.4G %s)\n", | ||||||
|         $freed_bytes, |         $freed_bytes, | ||||||
|         Directory::Simplify::Utils::hr_size($freed_bytes) |         Directory::Simplify::Utils::hr_size($freed_bytes) | ||||||
|             if $opts{f} or $opts{v}; |             if $print_freed or $verbose; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| package Directory::Simplify::Utils; | package Directory::Simplify::Utils; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue