App-renlikewd
view release on metacpan or search on metacpan
script/renlikewd view on Meta::CPAN
#!perl
use strict;
use warnings;
use Log::ger;
use Perinci::CmdLine::Any;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2023-11-23'; # DATE
our $DIST = 'App-renlikewd'; # DIST
our $VERSION = '0.005'; # VERSION
our %SPEC;
$SPEC{app} = {
v => 1.1,
summary => "REName a file so it becomes LIKE the current (Working) Directory's name",
description => <<'_',
% pwd
/home/u1/Some Project/Some Rather Long Title
% renlikewd --no-dry-run foo.jpg; # foo.jpg now becomes "Some Rather Long Title.jpg"
Here's what I now often have to do: put some file under a subdirectory with the
same name as the file (sans the extension), with the name being quite long. I
also often create the directory first, then move the originally named file into
this directory and rename it to follow the directory name.
I use Midnight Commander and this workflow sometimes makes it hard for me to use
Alt-Enter because one of the pane is already inside the directory.
This utility helps.
Note that this utility can default to the only file in the current directory (if
there are only a single file and zero or more directories and nothing else), so
to minimize surprise, the default mode is dry-run. Supply `--no-dry-run` to turn
off dry-run mode.
_
args => {
file => {
schema => 'filename::default_only_file_not_dir_in_curdir*',
pos => 0,
},
parents => {
schema => 'posint*',
summary => 'Instead of working directory, use its parent directory (if parents=1) or grandparent directory (if parents=2) and so on',
cmdline_aliases => {
p => {is_flag=>1, code=>sub { $_[0]{parents} = 1 }, summary => 'Shortcut for --parents=1'},
},
},
#extension => {
# summary => 'Add extension to the new name',
# schema => 'str*',
# pos => 1,
#},
overwrite => {
schema => 'bool*',
cmdline_aliases => {O=>{}},
},
},
features => {
dry_run => {default=>1},
},
};
sub app {
require Cwd;
require File::Util::Test;
my %args = @_;
my $file = $args{file} or return [400, "Please specify file to rename"];
my $parents = $args{parents} // 0;
return [404, "File '$file' does not exist"]
unless File::Util::Test::file_exists($file);
my $ext = $file =~ /.(\.\w+)$/ ? $1 : "";
my $cwd = Cwd::getcwd();
return [412, "Please don't run me in root directory as it's meaningless"]
if $cwd eq '/';
my $p = 0;
my $curname;
while (1) {
$cwd =~ s!/([^/]+)$!! or return [400, "Can't use working/parent directory because we already reach root"];
$curname = $1;
last if $p++ >= $parents;
}
my $newfile = "$curname$ext";
return [412, "New name '$newfile' already exists, use --overwrite (-O) to overwrite existing file"]
if File::Util::Test::file_exists($newfile) && !$args{overwrite};
if ($args{-dry_run}) {
log_info "DRY-RUN: Renaming %s to %s ...", $file, $newfile;
return [200, "OK (dry-run)"];
} else {
log_info "Renaming %s to %s ...", $file, $newfile;
rename $file, $newfile
or return [500, "Can't rename $file -> $newfile: $!"];
return [200, "OK"];
}
( run in 1.366 second using v1.01-cache-2.11-cpan-39bf76dae61 )