-
Notifications
You must be signed in to change notification settings - Fork 1
/
vidir
executable file
·242 lines (192 loc) · 4.76 KB
/
vidir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/perl
=head1 NAME
vidir - edit directories and filenames
=head1 SYNOPSIS
B<vidir> [B<--verbose>] [I<directory>|I<file>|B<->]...
=head1 DESCRIPTION
B<vidir> allows editing of directories and filenames in a text editor. If no
I<directory> is specified, the filenames of the current directory are edited.
When editing a directory, each item in the directory will appear on its own
numbered line. These numbers are how vidir keeps track of what items are
changed. Delete lines to remove files from the directory, or
edit filenames to rename files. You can also switch pairs of numbers to
swap filenames.
Filenames to be edited may be given any combination of I<directory>s (which
will be expanded to the non-recursive list of all files within I<directory>),
I<file>s, or I<->. If the latter is specified, B<vidir> reads a list of
filenames from stdin and displays those for editing.
=head1 OPTIONS
=over 4
=item -v, --verbose
Verbosely display the actions taken by the program.
=back
=head1 EXAMPLES
=over 4
=item vidir
=item vidir *.jpeg
Typical uses.
=item find | vidir -
Edit subdirectory contents too. To delete subdirectories,
delete all their contents and the subdirectory itself in the editor.
=item find -type f | vidir -
Edit all files under the current directory and subdirectories.
=back
=head1 ENVIRONMENT VARIABLES
=over 4
=item EDITOR
Editor to use.
=item VISUAL
Also supported to determine what editor to use.
=back
=head1 AUTHOR
Copyright 2006 by Joey Hess <id@joeyh.name>
Licensed under the GNU GPL.
=cut
use File::Basename;
use File::Path qw(make_path);
use File::Spec;
use File::Temp;
use Getopt::Long;
my $error=0;
my $verbose=0;
if (! GetOptions("verbose|v" => \$verbose)) {
die "Usage: $0 [--verbose] [directory|file|-]\n";
}
my @dir;
if (! @ARGV) {
push @ARGV, "."
}
foreach my $item (@ARGV) {
if ($item eq "-") {
push @dir, map { chomp; $_ } <STDIN>;
close STDIN;
open(STDIN, "/dev/tty") || die "reopen: $!\n";
}
elsif (-d $item) {
$item =~ s{/?$}{/};
opendir(DIR, $item) || die "$0: cannot read $item: $!\n";
push @dir, map { "$item$_" } sort readdir(DIR);
closedir DIR;
}
else {
push @dir, $item;
}
}
if (grep(/[[:cntrl:]]/, @dir)) {
die "$0: control characters in filenames are not supported\n";
}
my $tmp=File::Temp->new(TEMPLATE => "dirXXXXX", DIR => File::Spec->tmpdir);
open (OUT, ">".$tmp->filename) || die "$0: cannot create ".$tmp->filename.": $!\n";
my %item;
my $c=0;
foreach (@dir) {
next if /^(.*\/)?\.$/ || /^(.*\/)?\.\.$/;
$item{++$c}=$_;
print OUT "$c\t$_\n";
}
@dir=();
close OUT || die "$0: cannot write ".$tmp->filename.": $!\n";
my @editor="vi";
if (-x "/usr/bin/editor") {
@editor="/usr/bin/editor";
}
if (exists $ENV{EDITOR}) {
@editor=split(' ', $ENV{EDITOR});
}
if (exists $ENV{VISUAL}) {
@editor=split(' ', $ENV{VISUAL});
}
$ret=system(@editor, $tmp);
if ($ret != 0) {
die "@editor exited nonzero, aborting\n";
}
open (IN, $tmp->filename) || die "$0: cannot read ".$tmp->filename.": $!\n";
while (<IN>) {
chomp;
if (/^(\d+)\t{0,1}(.*)/) {
my $num=int($1);
my $name=$2;
if (! exists $item{$num}) {
die "$0: unknown item number $num\n";
}
elsif ($name ne $item{$num}) {
next unless length $name;
my $src=$item{$num};
my $dir=dirname($name);
if (! (-e $src || -l $src) ) {
print STDERR "$0: $src does not exist\n";
delete $item{$num};
next;
}
# deal with swaps
if (-e $name || -l $name) {
my $tmp=$name."~";
my $c=0;
while (-e $tmp || -l $tmp) {
$c++;
$tmp=$name."~$c";
}
if (! rename($name, $tmp)) {
print STDERR "$0: failed to rename $name to $tmp: $!\n";
$error=1;
}
elsif ($verbose) {
print "'$name' -> '$tmp'\n";
}
foreach my $item (keys %item) {
if ($item{$item} eq $name) {
$item{$item}=$tmp;
}
}
}
if ((! -d $dir) && (! make_path($dir, {
verbose => $verbose,
}))) {
print STDERR "$0: failed to create directory tree $dir: $!\n";
$error=1;
}
elsif (! rename($src, $name)) {
print STDERR "$0: failed to rename $src to $name: $!\n";
$error=1;
}
else {
if (-d $name) {
foreach (values %item) {
s,^\Q$src\E($|/),$name$1,;
}
}
if ($verbose) {
print "'$src' => '$name'\n";
}
}
}
delete $item{$num};
}
elsif (/^\s*$/) {
# skip empty line
}
else {
die "$0: unable to parse line \"$_\", aborting\n";
}
}
close IN || die "$0: cannot read ".$tmp->filename.": $!\n";
unlink($tmp.'~') if -e $tmp.'~';
sub rm {
my $file = shift;
if (-d $file && ! -l $file) {
return rmdir $file;
}
else {
return unlink $file;
}
}
foreach my $item (reverse sort values %item) {
if (! rm($item)) {
print STDERR "$0: failed to remove $item: $!\n";
$error=1;
}
if ($verbose) {
print "removed '$item'\n";
}
}
exit $error;