Vidir: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: twiddles)
m (→‎{{header|Raku}}: Bug fix, rename some variables minor tweaks)
Line 131: Line 131:


'''Named:'''
'''Named:'''
* '''-r''', recurse, flag, optional, default False. Recurse into nested directories and process those files as well.
* '''-r''' or '''--recurse''', flag, optional, default False. Recurse into nested directories and process those files as well.
* '''-v''', verbose, flag, optional, default False. Be chatty about what is going on when making changes.
* '''-v''' or '''--verbose''', flag, optional, default False. Be chatty about what is going on when making changes.
* '''--editor=whatever''' or '''--e=whatever''', string, optional, defaults the default text editor. Pass in a command name to specify a specific editor: (E.G. '''--editor=vim''')
* '''--e=whatever''' or '''--editor=whatever''', string, optional, defaults the default text editor. Pass in a command name to specify a specific editor: (E.G. '''--editor=vim''')




Line 158: Line 158:


unit sub MAIN (
unit sub MAIN (
Str $path = '.', #= default $path
Str $path = '.', #= default $path
Str $filter = '', #= default file filter
Str $filter = '', #= default file filter
Bool :$r = False, #= recursion flag
Bool :r(:$recurse)= False, #= recursion flag
Bool :$v = False, #= verbose mode
Bool :v(:$verbose)= False, #= verbose mode
Str :e(:$editor) = $*DISTRO ~~ /'Darwin'/ ?? "open" !! "xdg-open"; #= default editor
Str :e(:$editor) = $*DISTRO ~~ /'Darwin'/ ?? "open" !! "xdg-open"; #= default editor
);
);


Line 173: Line 173:
die "Can not find directory $dir" unless $dir.IO.d;
die "Can not find directory $dir" unless $dir.IO.d;



# get files from that path
my @files;
my @files;


# get files from that path
getdir( $dir, $filter );
getdir( $dir, $filter );

@files.= sort( &naturally );


# set up a temp file and file handle
# set up a temp file and file handle
my ($filename, $filehandle) = tempfile :suffix('.vidir');
my ($filename, $filehandle) = tempfile :suffix('.vidir');

# editor command
my $command = "$editor $filename";


# write the filenames to the tempfile
# write the filenames to the tempfile
Line 190: Line 190:
$filehandle.flush;
$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");
shell("$command 2> /dev/null");


react {
react {
# watch for file size changes
# watch for file changes
whenever IO::Notification.watch-path($filename) {
whenever IO::Notification.watch-path($filename) {
# allow a short interval for the file to finish writing
# allow a short interval for the file to finish writing
Line 210: Line 213:
checkdir %changes{"$k"};
checkdir %changes{"$k"};
# notify and do it
# notify and do it
say "Renaming: {@files[$k]} to " ~ %changes{"$k"} if $v;
say "Renaming: {@files[$k]} to " ~ %changes{"$k"} if $verbose;
rename @files[$k], %changes{"$k"} orelse .die;
rename @files[$k], %changes{"$k"} orelse .die;
}
}
Line 218: Line 221:
# name is gone, delete the file
# name is gone, delete the file
# notify and do it
# notify and do it
say "Deleting: {@files[$k]}" if $v;
say "Deleting: {@files[$k]}" if $verbose;
@files[$k].unlink orelse .die;
@files[$k].unlink orelse .die;
}
}
Line 227: Line 230:
checkdir $v;
checkdir $v;
# notify and do it
# notify and do it
say "Adding: $v" if $v;
say "Adding: $v" if $verbose;
shell("touch $v") orelse .die;
shell("touch $v") orelse .die;
}
}
Line 245: Line 248:
sub files ( $dir, $filter = '' ) {
sub files ( $dir, $filter = '' ) {
if $filter.chars {
if $filter.chars {
$dir.IO.dir.grep( *.f ).grep( *.basename.contains(/<$filter>/) ).sort( &naturally );
$dir.IO.dir.grep( *.f ).grep( *.basename.contains(/<$filter>/) );
} else {
} else {
$dir.IO.dir.grep( *.f ).sort( &naturally );
$dir.IO.dir.grep( *.f );
}
}
}
}
Line 253: Line 256:
# get the files in the present directory and recurse if desired
# get the files in the present directory and recurse if desired
sub getdir ($dir, $filter) {
sub getdir ($dir, $filter) {
if $r {
if $recurse {
@files.append: files($dir, $filter);
@files.append: files($dir, $filter);
getdir( $_, $filter ) for $dir.IO.dir.grep( *.d );
getdir( $_, $filter ) for $dir.IO.dir.grep( *.d );
Line 269: Line 272:
my $thispath = @path[^$_].join('/');
my $thispath = @path[^$_].join('/');
unless $thispath.IO.e {
unless $thispath.IO.e {
say "Creating new directory $thispath" if $v;
say "Creating new directory $thispath" if $verbose;
mkdir($thispath);
mkdir($thispath);
}
}