1
0
Fork 0

format new perl scripts

This commit is contained in:
Luca Bilke 2024-01-02 19:22:21 +01:00
parent 201d5e9edf
commit 07ff52dfdd
2 changed files with 56 additions and 56 deletions

View File

@ -7,59 +7,61 @@ use File::Copy;
use File::Path("make_path");
use List::Util("max");
sub fs_root($file_name) {
chomp(my $ret = `stat -c %m $file_name`);
sub fs_root ($file_name) {
chomp( my $ret = `stat -c %m $file_name` );
return $ret;
}
sub file_info($file) {
my $file_name = `basename $file`;
my $file_path = `realpath -s $file`;
sub file_info ($file) {
my $file_name = `basename $file`;
my $file_path = `realpath -s $file`;
my $file_fs_root = fs_root($file_name);
chomp($file_name, $file_path, $file_fs_root);
return ($file_name, $file_path, $file_fs_root);
chomp( $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 $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/";
} else {
$trash_dir = "$file_fs_root/.Trash/"
}
else {
$trash_dir = "$file_fs_root/.Trash/";
}
return "${trash_dir}info", "${trash_dir}files";
}
sub target_file_name($file_name, $trash_file_path) {
opendir(my $dir, "$trash_file_path") or die "$!\n";
my $existing_suffix_nums = map(($_ =~ /^${file_name}\.~(\d)~/), readdir $dir);
sub target_file_name ( $file_name, $trash_file_path ) {
opendir( my $dir, "$trash_file_path" ) or die "$!\n";
my $existing_suffix_nums =
map( ( $_ =~ /^${file_name}\.~(\d)~/ ), readdir $dir );
my $suffix_num = max($existing_suffix_nums) + 1;
my $suffix = "~" . $suffix_num . "~";
my $suffix = "~" . $suffix_num . "~";
closedir($dir);
return "${file_name}.${suffix}";
}
sub write_file($file_path, $contents) {
open(FH, '>', $file_path) or die "$!\n";
sub write_file ( $file_path, $contents ) {
open( FH, '>', $file_path ) or die "$!\n";
print FH $contents;
}
# NOTE: Execution starts here
if ($#ARGV + 1 != 1) {
if ( $#ARGV + 1 != 1 ) {
die "Bad Arguments\n";
}
stat $ARGV[0] or die "$!\n";
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 $target_name = target_file_name($file_name, $trash_file_path);
chomp(my $deletion_time = `date -u +%Y%m%dUTC%T`);
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 $target_name = target_file_name( $file_name, $trash_file_path );
chomp( my $deletion_time = `date -u +%Y%m%dUTC%T` );
my $info_contents = <<EOF;
[Trash Info]
@ -67,6 +69,6 @@ Path=$file_src
DeletionDate=$deletion_time
EOF
make_path($trash_info_path, $trash_file_path);
move($file_src, "$trash_file_path/$target_name") or die "$!\n";
write_file("$trash_info_path/$target_name", $info_contents) or die "$!\n";
make_path( $trash_info_path, $trash_file_path );
move( $file_src, "$trash_file_path/$target_name" ) or die "$!\n";
write_file( "$trash_info_path/$target_name", $info_contents ) or die "$!\n";

View File

@ -7,42 +7,37 @@ use File::Basename;
use File::Copy;
use Cwd;
sub fs_root($file_name) {
chomp(my $ret = `stat -c %m $file_name`);
sub fs_root ($file_name) {
chomp( my $ret = `stat -c %m $file_name` );
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";
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/";
}
sub trashed_files($search_path, $trash_path) {
sub trashed_files ( $search_path, $trash_path ) {
my @ret;
opendir(my $dir, "$trash_path/info") or die "$!\n";
for (readdir $dir) {
$_ =~ /\.~\d~$/ or next;
open(FH, "<", "$trash_path/info/$_") or next;
<FH> eq "[Trash Info]\n" or next;
my ($name, $path, $suffix) = fileparse((<FH> =~ /(?<=Path=)(.+)$/)[0]);
opendir( my $dir, "$trash_path/info" ) or die "$!\n";
for ( readdir $dir ) {
$_ =~ /\.~\d~$/ or next;
open( FH, "<", "$trash_path/info/$_" ) or next;
<FH> eq "[Trash Info]\n" or next;
my ( $name, $path, $suffix ) =
fileparse( ( <FH> =~ /(?<=Path=)(.+)$/ )[0] );
$path =~ /^$search_path\/?$/ or next;
my $deletion_date = (<FH> =~ /(?<=DeletionDate=)(.+)$/)[0];
chomp($deletion_date = `date -d ${deletion_date} +'%x %X'`);
push(@ret, [$path, "${name}${suffix}", $deletion_date, $_]);
my $deletion_date = ( <FH> =~ /(?<=DeletionDate=)(.+)$/ )[0];
chomp( $deletion_date = `date -d ${deletion_date} +'%x %X'` );
push( @ret, [ $path, "${name}${suffix}", $deletion_date, $_ ] );
}
return @ret;
}
sub fzf_files(@files) {
sub fzf_files (@files) {
my $n = 0;
my $str;
for my $e (@files) {
$str .= "\n" if $n > 0;
@ -50,32 +45,35 @@ sub fzf_files(@files) {
$n++;
}
chomp($str);
my $sel =`echo '$str' | fzf`;
my $sel = `echo '$str' | fzf`;
!$sel and exit;
my $index = substr($sel, 0, index($sel, ' '));
my $index = substr( $sel, 0, index( $sel, ' ' ) );
return $index;
}
# NOTE: Execution starts here
#
my $target_directory;
if ($#ARGV + 1 > 1) {
if ( $#ARGV + 1 > 1 ) {
die "Bad Arguments\n";
} elsif ($#ARGV + 1 == 1) {
}
elsif ( $#ARGV + 1 == 1 ) {
$target_directory = shift;
chomp($target_directory = `realpath -s $target_directory`);
chomp( $target_directory = `realpath -s $target_directory` );
stat $target_directory or die "$!\n";
} else {
}
else {
$target_directory = getcwd();
}
my $trash_dir = trash_dir(fs_root($target_directory));
my @files = trashed_files($target_directory, $trash_dir);
my $trash_dir = trash_dir( fs_root($target_directory) );
my @files = trashed_files( $target_directory, $trash_dir );
!@files and exit;
my $index = fzf_files(@files);
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];
move($file_src, $file_dest) or die "$!\n";
move( $file_src, $file_dest ) or die "$!\n";
unlink($file_info);