-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpin.pl
executable file
·305 lines (251 loc) · 5.75 KB
/
dpin.pl
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd qw(getcwd);
use Getopt::Long qw(GetOptions);
use Scalar::Util qw(looks_like_number);
my $BLACK="\033[30m";
my $RED="\033[31m";
my $GREEN="\033[32m";
my $YELLOW="\033[33m";
my $BLUE="\033[34m";
my $PINK="\033[35m";
my $CYAN="\033[36m";
my $WHITE="\033[37m";
my $NORMAL="\033[0;39m";
my $GRAY="\033[90m";
my $VERSION = '1.0.0';
my $prog = "dpin";
my $data_file = "$ENV{HOME}/.config/$prog";
my $data_file_tmp = "$ENV{HOME}/.config/$prog.tmp";
if (not system("test $data_file")) {
system("touch $data_file");
}
open(FHCACHE, '>', "$ENV{HOME}/.cache/dpin") or die $!;
my $command = '';
my $num = '';
my $alias = '';
my $dir = '';
GetOptions(
'pin' => sub { $command = 'pin'; },
'list' => sub { $command = 'list'; },
'remove' => sub { $command = 'remove'; },
'clean' => sub { $command = 'clean'; },
'move' => sub { $command = 'move'; },
'alias=s' => \$alias,
'dir=s' => \$dir,
'num=i' => \$num,
'version' => sub { print "$prog: version $VERSION\n"; exit; },
'help' => sub { usage(); exit; },
);
if (looks_like_number($alias)) {
print STDERR "$prog: alias cannot be a number.\n";
exit 1;
}
if (not $command) {
cdpin();
} elsif ($command eq 'pin') {
pin($num, $alias, $dir);
} elsif ($command eq 'list') {
list($num, $alias);
} elsif ($command eq 'remove') {
remove($num, $alias, $dir);
} elsif ($command eq 'clean') {
clean();
}
sub cdpin {
unless (scalar(@ARGV) == 1) { print STDERR "$prog: missing argument\n"; exit 1; }
open(FH, '<', $data_file) or die $!;
if (looks_like_number($ARGV[0])) {
my $line = 0;
while (<FH>) {
if ($line == $ARGV[0]) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
chomp($fdir);
if (system("test -e $fdir -a -d $fdir")) {
print STDERR "$prog: directory '$fdir' missing, unpining it.\n";
close(FH);
remove($fnum, '', '');
} else {
print FHCACHE "$fdir\n";
}
exit;
close(FHCACHE);
}
$line++;
}
} else {
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
if ($falias =~ /^$ARGV[0]/) {
chomp($fdir);
if (system("test -e $fdir -a -d $fdir")) {
print "$prog: directory '$fdir' missing, unpining it.\n";
close(FH);
remove($fnum, '', '');
} else {
print FHCACHE "$fdir";
}
exit;
}
}
}
print "dpin: alias or number '$ARGV[0]' not found.\n";
}
sub pin {
my ($num, $alias, $dir) = @_;
my $lines = 0;
if (not $num) {
open(FH, '<', $data_file) or die $!;
while (<FH>) { $lines++; }
close(FH);
} else {
print '--pin and --num are not yet supported.\n';
exit 1;
}
if (not $dir) {
$dir = getcwd();
} else {
if (system("test -e $dir -a -d $dir")) {
print "$prog: directory doesn't exit.\n";
exit 1;
}
}
open(FH, '>>', $data_file) or die $!;
print FH "$lines:$alias:$dir\n";
close(FH);
}
sub list {
my ($num, $alias) = @_;
open(FH, '<', $data_file) or die $!;
if ($alias) {
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
if ($falias =~ /^$alias/) {
print $fdir;
}
}
} elsif ($num) {
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
if ($fnum == $num) {
print $fdir;
}
}
} else {
print "${BLUE}n ${NORMAL}alias ${GRAY}directory\n\n";
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
if ($falias eq '') {
$falias = "\t";
}
print "$BLUE$fnum $NORMAL$falias $GRAY$fdir";
}
}
print "${NORMAL}";
close(FH);
}
sub remove {
my ($num, $alias, $dir) = @_;
if ((!$alias && !$dir && !$num or $alias && $dir or $alias && $num or $dir && $num) and ($num == 0 and $alias || $dir)) {
print STDERR 'dpin: use exactly one of --alias, --dir, --num\n';
exit 1;
}
my $line;
my $count = 0;
open(FH, '<', $data_file) or die $!;
open(FHTMP, '>', $data_file_tmp) or die $!;
if ($alias) {
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
if (not $falias eq $alias){
print FHTMP "$count:$falias:$fdir";
$count++;
}
}
}
if ($dir) {
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
if (not $fdir eq $dir){
print FHTMP "$count:$falias:$fdir";
$count++;
}
}
}
if ($num) {
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
if (not $fnum == $num){
print FHTMP "$count:$falias:$fdir";
$count++;
}
}
}
close FH;
close FHTMP;
system("cp $data_file_tmp $data_file");
system("rm $data_file_tmp");
}
sub clean {
print '$prog: are you sure you want to remove all pins ? [yes/no] > ';
my $confirm = <>;
chomp($confirm);
if ($confirm eq 'yes') {
print '$prog: Removing all pins...\n';
system("rm -f $data_file");
}
}
sub move {
unless (scalar(@ARGV) == 2) { print STDERR "$prog: missing arguments\n"; exit 1; }
open(FH, '<', "$data_file") or die $!;
# if (looks_like_number(
while (<FH>) {
my ($fnum, $falias, $fdir) = split(/:/, $_);
}
}
sub usage {
printf
"$prog : Usage
$prog index|alias : cd to the pin with the right index or alias.
$prog --pin [--alias STRING] [--dir STRING]
$prog --list [--alias STRING] [--num INT]
$prog --remove --alias STRING | --num INT
$prog --clean
Commands:
-p, --pin
adds a pin to to the list
-l, --list
prints the list of pins
-r, --remove
remove pin
-c, --clean
remove all pins
Options:
-a, --alias = STRING
use STRING as alias
-d, --dir = STRING
use STRING as directory
-n, --num = INT
use INT as index
Examples:
Pin current directory at the end of the list.
> $prog -p
Pin a specified directory with -d, give it an alias with -a.
> $prog -p -a myalias -d \$HOME/mydir
Cd to pin index 0 (no options needed).
> $prog 0
Cd to pin with alias 'myalias' (no options needed).
> $prog myalias
> $prog myal
> $prog m
List all pins.
> $prog -l
Remove pin index 0.
> $prog -r -n 0
Remove pin with alias 'myalias'.
> $prog -r -a myalias
Remove all pins.
> $prog -c
"
}