Vidir: Difference between revisions

62 bytes added ,  4 years ago
m
→‎{{header|Raku}}: Bug fix, rename some variables minor tweaks
m (→‎{{header|Raku}}: twiddles)
m (→‎{{header|Raku}}: Bug fix, rename some variables minor tweaks)
Line 131:
 
'''Named:'''
* '''-r''', or '''--recurse''', flag, optional, default False. Recurse into nested directories and process those files as well.
* '''-v''', or '''--verbose''', flag, optional, default False. Be chatty about what is going on when making changes.
* '''--editore=whatever''' or '''--eeditor=whatever''', string, optional, defaults the default text editor. Pass in a command name to specify a specific editor: (E.G. '''--editor=vim''')
 
 
Line 158:
 
unit sub MAIN (
Str $path = '.', #= default $path
Str $filter = '', #= default file filter
Bool :$r (:$recurse)= False, #= recursion flag
Bool :$v (:$verbose)= False, #= verbose mode
Str :e(:$editor) = $*DISTRO ~~ /'Darwin'/ ?? "open" !! "xdg-open"; #= default editor
);
 
Line 173:
die "Can not find directory $dir" unless $dir.IO.d;
 
 
# get files from that path
my @files;
 
# get files from that path
getdir( $dir, $filter );
 
@files.= sort( &naturally );
 
# set up a temp file and file handle
my ($filename, $filehandle) = tempfile :suffix('.vidir');
 
# editor command
my $command = "$editor $filename";
 
# write the filenames to the tempfile
Line 190:
$filehandle.flush;
 
# editor command
# suppress STDERR, some editors complain about open files being deleted
my $command = "$editor $filename";
 
# start text editor; suppress STDERR, some editors complain about open files being deleted
shell("$command 2> /dev/null");
 
react {
# watch for file size changes
whenever IO::Notification.watch-path($filename) {
# allow a short interval for the file to finish writing
Line 210 ⟶ 213:
checkdir %changes{"$k"};
# notify and do it
say "Renaming: {@files[$k]} to " ~ %changes{"$k"} if $vverbose;
rename @files[$k], %changes{"$k"} orelse .die;
}
Line 218 ⟶ 221:
# name is gone, delete the file
# notify and do it
say "Deleting: {@files[$k]}" if $vverbose;
@files[$k].unlink orelse .die;
}
Line 227 ⟶ 230:
checkdir $v;
# notify and do it
say "Adding: $v" if $vverbose;
shell("touch $v") orelse .die;
}
Line 245 ⟶ 248:
sub files ( $dir, $filter = '' ) {
if $filter.chars {
$dir.IO.dir.grep( *.f ).grep( *.basename.contains(/<$filter>/) ).sort( &naturally );
} else {
$dir.IO.dir.grep( *.f ).sort( &naturally );
}
}
Line 253 ⟶ 256:
# get the files in the present directory and recurse if desired
sub getdir ($dir, $filter) {
if $rrecurse {
@files.append: files($dir, $filter);
getdir( $_, $filter ) for $dir.IO.dir.grep( *.d );
Line 269 ⟶ 272:
my $thispath = @path[^$_].join('/');
unless $thispath.IO.e {
say "Creating new directory $thispath" if $vverbose;
mkdir($thispath);
}
10,333

edits