Vidir: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: twiddles) |
Thundergnat (talk | contribs) m (→{{header|Raku}}: Bug fix, rename some variables minor tweaks) |
||
Line 131: | Line 131: | ||
'''Named:''' |
'''Named:''' |
||
* '''-r''' |
* '''-r''' or '''--recurse''', flag, optional, default False. Recurse into nested directories and process those files as well. |
||
* '''-v''' |
* '''-v''' or '''--verbose''', flag, optional, default False. Be chatty about what is going on when making changes. |
||
* '''-- |
* '''--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 : |
Bool :r(:$recurse)= False, #= recursion flag |
||
Bool : |
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; |
||
⚫ | |||
my @files; |
my @files; |
||
⚫ | |||
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'); |
||
⚫ | |||
⚫ | |||
# write the filenames to the tempfile |
# write the filenames to the tempfile |
||
Line 190: | Line 190: | ||
$filehandle.flush; |
$filehandle.flush; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
shell("$command 2> /dev/null"); |
shell("$command 2> /dev/null"); |
||
react { |
react { |
||
# watch for file |
# 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 $ |
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 $ |
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 $ |
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>/) |
$dir.IO.dir.grep( *.f ).grep( *.basename.contains(/<$filter>/) ); |
||
} else { |
} else { |
||
$dir.IO.dir.grep( *.f |
$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 $ |
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 $ |
say "Creating new directory $thispath" if $verbose; |
||
mkdir($thispath); |
mkdir($thispath); |
||
} |
} |