format new perl scripts
This commit is contained in:
parent
201d5e9edf
commit
07ff52dfdd
2 changed files with 56 additions and 56 deletions
|
@ -7,59 +7,61 @@ use File::Copy;
|
||||||
use File::Path("make_path");
|
use File::Path("make_path");
|
||||||
use List::Util("max");
|
use List::Util("max");
|
||||||
|
|
||||||
sub fs_root($file_name) {
|
sub fs_root ($file_name) {
|
||||||
chomp(my $ret = `stat -c %m $file_name`);
|
chomp( my $ret = `stat -c %m $file_name` );
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub file_info($file) {
|
sub file_info ($file) {
|
||||||
my $file_name = `basename $file`;
|
my $file_name = `basename $file`;
|
||||||
my $file_path = `realpath -s $file`;
|
my $file_path = `realpath -s $file`;
|
||||||
my $file_fs_root = fs_root($file_name);
|
my $file_fs_root = fs_root($file_name);
|
||||||
|
|
||||||
chomp($file_name, $file_path, $file_fs_root);
|
chomp( $file_name, $file_path, $file_fs_root );
|
||||||
return ($file_name, $file_path, $file_fs_root);
|
return ( $file_name, $file_path, $file_fs_root );
|
||||||
}
|
}
|
||||||
|
|
||||||
sub trash_dirs($file_fs_root) {
|
sub trash_dirs ($file_fs_root) {
|
||||||
my $data_path = "$ENV{'XDG_DATA_HOME'}" // "$ENV{'HOME'}/.local/share";
|
my $data_path = "$ENV{'XDG_DATA_HOME'}" // "$ENV{'HOME'}/.local/share";
|
||||||
|
|
||||||
my $trash_dir;
|
my $trash_dir;
|
||||||
if ($file_fs_root eq fs_root($data_path)) {
|
if ( $file_fs_root eq fs_root($data_path) ) {
|
||||||
$trash_dir = "$data_path/Trash/";
|
$trash_dir = "$data_path/Trash/";
|
||||||
} else {
|
}
|
||||||
$trash_dir = "$file_fs_root/.Trash/"
|
else {
|
||||||
|
$trash_dir = "$file_fs_root/.Trash/";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "${trash_dir}info", "${trash_dir}files";
|
return "${trash_dir}info", "${trash_dir}files";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub target_file_name($file_name, $trash_file_path) {
|
sub target_file_name ( $file_name, $trash_file_path ) {
|
||||||
opendir(my $dir, "$trash_file_path") or die "$!\n";
|
opendir( my $dir, "$trash_file_path" ) or die "$!\n";
|
||||||
my $existing_suffix_nums = map(($_ =~ /^${file_name}\.~(\d)~/), readdir $dir);
|
my $existing_suffix_nums =
|
||||||
|
map( ( $_ =~ /^${file_name}\.~(\d)~/ ), readdir $dir );
|
||||||
my $suffix_num = max($existing_suffix_nums) + 1;
|
my $suffix_num = max($existing_suffix_nums) + 1;
|
||||||
my $suffix = "~" . $suffix_num . "~";
|
my $suffix = "~" . $suffix_num . "~";
|
||||||
closedir($dir);
|
closedir($dir);
|
||||||
|
|
||||||
return "${file_name}.${suffix}";
|
return "${file_name}.${suffix}";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub write_file($file_path, $contents) {
|
sub write_file ( $file_path, $contents ) {
|
||||||
open(FH, '>', $file_path) or die "$!\n";
|
open( FH, '>', $file_path ) or die "$!\n";
|
||||||
print FH $contents;
|
print FH $contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
# NOTE: Execution starts here
|
# NOTE: Execution starts here
|
||||||
|
|
||||||
if ($#ARGV + 1 != 1) {
|
if ( $#ARGV + 1 != 1 ) {
|
||||||
die "Bad Arguments\n";
|
die "Bad Arguments\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
stat $ARGV[0] or die "$!\n";
|
stat $ARGV[0] or die "$!\n";
|
||||||
my ($file_name, $file_src, $file_fs_root) = file_info($ARGV[0]);
|
my ( $file_name, $file_src, $file_fs_root ) = file_info( $ARGV[0] );
|
||||||
my ($trash_info_path, $trash_file_path) = trash_dirs($file_fs_root);
|
my ( $trash_info_path, $trash_file_path ) = trash_dirs($file_fs_root);
|
||||||
my $target_name = target_file_name($file_name, $trash_file_path);
|
my $target_name = target_file_name( $file_name, $trash_file_path );
|
||||||
chomp(my $deletion_time = `date -u +%Y%m%dUTC%T`);
|
chomp( my $deletion_time = `date -u +%Y%m%dUTC%T` );
|
||||||
|
|
||||||
my $info_contents = <<EOF;
|
my $info_contents = <<EOF;
|
||||||
[Trash Info]
|
[Trash Info]
|
||||||
|
@ -67,6 +69,6 @@ Path=$file_src
|
||||||
DeletionDate=$deletion_time
|
DeletionDate=$deletion_time
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
make_path($trash_info_path, $trash_file_path);
|
make_path( $trash_info_path, $trash_file_path );
|
||||||
move($file_src, "$trash_file_path/$target_name") or die "$!\n";
|
move( $file_src, "$trash_file_path/$target_name" ) or die "$!\n";
|
||||||
write_file("$trash_info_path/$target_name", $info_contents) or die "$!\n";
|
write_file( "$trash_info_path/$target_name", $info_contents ) or die "$!\n";
|
||||||
|
|
|
@ -7,42 +7,37 @@ use File::Basename;
|
||||||
use File::Copy;
|
use File::Copy;
|
||||||
use Cwd;
|
use Cwd;
|
||||||
|
|
||||||
sub fs_root($file_name) {
|
sub fs_root ($file_name) {
|
||||||
chomp(my $ret = `stat -c %m $file_name`);
|
chomp( my $ret = `stat -c %m $file_name` );
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub trash_dir($file_fs_root) {
|
sub trash_dir ($file_fs_root) {
|
||||||
my $data_path = "$ENV{'XDG_DATA_HOME'}" // "$ENV{'HOME'}/.local/share";
|
my $data_path = "$ENV{'XDG_DATA_HOME'}" // "$ENV{'HOME'}/.local/share";
|
||||||
|
|
||||||
return "$data_path/Trash/" if ($file_fs_root eq fs_root($data_path));
|
return "$data_path/Trash/" if ( $file_fs_root eq fs_root($data_path) );
|
||||||
return "$file_fs_root/.Trash/";
|
return "$file_fs_root/.Trash/";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub trashed_files($search_path, $trash_path) {
|
sub trashed_files ( $search_path, $trash_path ) {
|
||||||
my @ret;
|
my @ret;
|
||||||
opendir(my $dir, "$trash_path/info") or die "$!\n";
|
opendir( my $dir, "$trash_path/info" ) or die "$!\n";
|
||||||
for (readdir $dir) {
|
for ( readdir $dir ) {
|
||||||
$_ =~ /\.~\d~$/ or next;
|
$_ =~ /\.~\d~$/ or next;
|
||||||
|
open( FH, "<", "$trash_path/info/$_" ) or next;
|
||||||
open(FH, "<", "$trash_path/info/$_") or next;
|
<FH> eq "[Trash Info]\n" or next;
|
||||||
|
my ( $name, $path, $suffix ) =
|
||||||
<FH> eq "[Trash Info]\n" or next;
|
fileparse( ( <FH> =~ /(?<=Path=)(.+)$/ )[0] );
|
||||||
|
|
||||||
my ($name, $path, $suffix) = fileparse((<FH> =~ /(?<=Path=)(.+)$/)[0]);
|
|
||||||
$path =~ /^$search_path\/?$/ or next;
|
$path =~ /^$search_path\/?$/ or next;
|
||||||
|
my $deletion_date = ( <FH> =~ /(?<=DeletionDate=)(.+)$/ )[0];
|
||||||
my $deletion_date = (<FH> =~ /(?<=DeletionDate=)(.+)$/)[0];
|
chomp( $deletion_date = `date -d ${deletion_date} +'%x %X'` );
|
||||||
chomp($deletion_date = `date -d ${deletion_date} +'%x %X'`);
|
push( @ret, [ $path, "${name}${suffix}", $deletion_date, $_ ] );
|
||||||
push(@ret, [$path, "${name}${suffix}", $deletion_date, $_]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return @ret;
|
return @ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub fzf_files(@files) {
|
sub fzf_files (@files) {
|
||||||
my $n = 0;
|
my $n = 0;
|
||||||
|
|
||||||
my $str;
|
my $str;
|
||||||
for my $e (@files) {
|
for my $e (@files) {
|
||||||
$str .= "\n" if $n > 0;
|
$str .= "\n" if $n > 0;
|
||||||
|
@ -50,32 +45,35 @@ sub fzf_files(@files) {
|
||||||
$n++;
|
$n++;
|
||||||
}
|
}
|
||||||
chomp($str);
|
chomp($str);
|
||||||
my $sel =`echo '$str' | fzf`;
|
my $sel = `echo '$str' | fzf`;
|
||||||
!$sel and exit;
|
!$sel and exit;
|
||||||
my $index = substr($sel, 0, index($sel, ' '));
|
my $index = substr( $sel, 0, index( $sel, ' ' ) );
|
||||||
return $index;
|
return $index;
|
||||||
}
|
}
|
||||||
|
|
||||||
# NOTE: Execution starts here
|
# NOTE: Execution starts here
|
||||||
#
|
#
|
||||||
my $target_directory;
|
my $target_directory;
|
||||||
if ($#ARGV + 1 > 1) {
|
if ( $#ARGV + 1 > 1 ) {
|
||||||
die "Bad Arguments\n";
|
die "Bad Arguments\n";
|
||||||
} elsif ($#ARGV + 1 == 1) {
|
}
|
||||||
|
elsif ( $#ARGV + 1 == 1 ) {
|
||||||
$target_directory = shift;
|
$target_directory = shift;
|
||||||
chomp($target_directory = `realpath -s $target_directory`);
|
chomp( $target_directory = `realpath -s $target_directory` );
|
||||||
stat $target_directory or die "$!\n";
|
stat $target_directory or die "$!\n";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
$target_directory = getcwd();
|
$target_directory = getcwd();
|
||||||
}
|
}
|
||||||
|
|
||||||
my $trash_dir = trash_dir(fs_root($target_directory));
|
my $trash_dir = trash_dir( fs_root($target_directory) );
|
||||||
my @files = trashed_files($target_directory, $trash_dir);
|
my @files = trashed_files( $target_directory, $trash_dir );
|
||||||
!@files and exit;
|
!@files and exit;
|
||||||
my $index = fzf_files(@files);
|
my $index = fzf_files(@files);
|
||||||
|
|
||||||
my $file_dest = $files[$index][0] . $files[$index][1];
|
my $file_dest = $files[$index][0] . $files[$index][1];
|
||||||
my $file_src = $trash_dir . "files/" . $files[$index][3];
|
my $file_src = $trash_dir . "files/" . $files[$index][3];
|
||||||
my $file_info = $trash_dir . "info/" . $files[$index][3];
|
my $file_info = $trash_dir . "info/" . $files[$index][3];
|
||||||
move($file_src, $file_dest) or die "$!\n";
|
|
||||||
|
move( $file_src, $file_dest ) or die "$!\n";
|
||||||
unlink($file_info);
|
unlink($file_info);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue