-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcd.pl
executable file
·588 lines (510 loc) · 14.5 KB
/
fcd.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#!/usr/bin/perl -w
# Copyright (C) Torbjorn Hedqvist - All Rights Reserved
# You may use, distribute and modify this code under the
# terms of the MIT license. See LICENSE file in the project
# root for full license information.
##############################################################################
# fcd.pl (F)ast (C)ange (D)irectory
#
# Known bugs:
# - None, but I'm sure they are hidden inside ;)
# All feedback is appreciated.
#
##############################################################################
use strict;
use Term::Complete;
use File::Basename;
# Version
my $versionShort = "1.7.2";
my $version = "fcd.pl Version $versionShort\n" .
"Author: Torbjorn Hedqvist\n\n";
# Some colors
my $reset = "\033[0m";
my $fgRed = "\033[31m";
my $fgBlue = "\033[34m";
my $fgyellow = "\033[33m";
# Catch SIGINT and use own function for cleanup.
$SIG{INT} = \&SigintHandler;
# environments
my $vobRoot = $ENV{'VOB_ROOT'};
my $homeDir = $ENV{'HOME'};
my $currentDir = $ENV{'PWD'};
my $myName = basename($0);
# files
my $dirsFile = "$homeDir/.fcd_dirs";
my $cdDirFile = "$homeDir/.fcd_dir";
my $commandFile = "$homeDir/.fcd_cmd";
# global defaults
my @dirs = {};
my $savedArgument = "";
### START ###
# clean out result from previous calls to fcd
if (-e $commandFile)
{
unlink $commandFile;
}
if (-e $cdDirFile)
{
unlink $cdDirFile;
}
ReadInputFile();
if (@ARGV == 0)
{
# If no cmd-line argument was given this is treated as a read.
# Read entry or entries.
ReadEntry("short");
}
elsif (@ARGV >= 1)
{
SWITCH:
{
$ARGV[0] =~ /^-w$/ && do
{
$savedArgument = "write";
WriteNewEntry();
last SWITCH;
};
$ARGV[0] =~ /^-d$/ && do
{
$savedArgument = "delete";
# Delete entry or entries.
DeleteEntry();
last SWITCH;
};
$ARGV[0] =~ /^-c$/ && do
{
$savedArgument = "modify";
# Add, replace or delete a command to an existing entry
ModifyCommand();
last SWITCH;
};
($ARGV[0] =~ /^--help$/ || $ARGV[0] =~ /^-h$/) && do
{
PrintHelp();
last SWITCH;
};
$ARGV[0] =~ /^-v$/ && do
{
print "$versionShort\n";
last SWITCH;
};
$ARGV[0] =~ /^--version$/ && do
{
print "$version\n";
last SWITCH;
};
$ARGV[0] =~ /^-l$/ && do
{
pop; # remove argument, else it will be treated as a tag
# Read entry or entries with full listing of applied commands
ReadEntry("long");
last SWITCH;
};
# Default:
# If no cmd-line argument matched this is treated as a read.
# Read entry or entries.
ReadEntry("short");
}
}
exit(0);
### END ###
### Functions ###
sub WriteNewEntry
{
my $tag = $ARGV[1] if @ARGV == 2;
open DIRS_OUTFILE, ">>$dirsFile"
or die "Error: Can't open $dirsFile: $!\n";
# If tag was provided, check if tag already exist in input file
if($tag)
{
my $countTags = 0;
foreach my $dir (@dirs)
{
# Skip comment lines
next if $dir =~ /^\s*#.*/;
$countTags++ if $dir =~ /^$tag\s+/;
}
$countTags != 0 && do
{
print $fgyellow, "Tag \"$tag\" already exists, no entry added...\n", $reset;
exit(1);
};
}
foreach my $dir (@dirs)
{
# Skip comment lines
next if $dir =~ /^\s*#.*/;
# remove trainling command if it exists.
$dir =~ s%(.*)\s+cmd:.*$%$1%;
# If $VOB_ROOT is defined replace the tag $VOB_ROOT in saved path before
# comparison.
$dir =~ s%\$VOB_ROOT(.*)%$vobRoot$1% if $vobRoot;
if ($dir =~ /^.*?$currentDir$/)
{
print $fgyellow, "This path already exists, no entry added...\n", $reset;
exit(1);
}
}
# If environment $VOB_ROOT is defined and is part of the current directory,
# replace it with keyword $VOB_ROOT
if ($vobRoot)
{
my $vobPattern = '$VOB_ROOT';
if ($currentDir eq "$vobRoot")
{
# This is an exact match of VOB_ROOT and the next substitution
# can't handle that. Just replace it.
$currentDir = "$vobPattern";
}
else
{
$currentDir =~ s%/local/tmp/views/.*?src_.*?(/.*)%$vobPattern$1%;
}
}
if ($tag)
{
print DIRS_OUTFILE "$tag $currentDir\n";
}
else
{
print DIRS_OUTFILE "$currentDir\n";
}
}
sub DeleteEntry
{
my $tag = "";
if (@ARGV == 2)
{
$tag = $ARGV[1];
}
open DIRS_OUTFILE, ">$dirsFile" or
die "Error: Can't open $dirsFile: $!\n";
if ($tag eq "all")
{
print $fgyellow, "Do you really want to delete all entries (yes/no)? ", $reset;
my $answer = <STDIN>;
if ($answer =~ /[Yy][Ee][Ss]/)
{
print $fgyellow, "Swoosh, all entries deleted...\n", $reset;
unlink $dirsFile;
}
else
{
print $fgyellow, "No entries deleted...\n", $reset;
foreach my $dir (@dirs)
{
print DIRS_OUTFILE $dir;
}
}
}
elsif ($tag)
{
open DIRS_OUTFILE, ">$dirsFile" or
die "Error: Can't open $dirsFile: $!\n";
my $entryFound = 0;
foreach my $dir (@dirs)
{
if ($dir =~ /^$tag\s+/)
{
$entryFound = 1;
next;
}
print DIRS_OUTFILE $dir;
}
if (! $entryFound)
{
print $fgyellow, "Can't find tag \"$tag\", no entry deleted...\n", $reset;
}
}
else # no tag provided
{
PrintDirs("short");
print "delete item(s): ";
my $answer = <STDIN>;
my @deleteItems = split(/,/, $answer) if $answer =~ /,/;
$answer = shift @deleteItems if @deleteItems;
if ($answer && $answer =~ /^\d+$/)
{
my $counter = 0;
foreach my $dir (@dirs)
{
# Skip comment lines
next if $dir =~ /^\s*#.*/;
$counter++;
if ($counter == $answer)
{
$answer = shift @deleteItems if @deleteItems;
next;
}
print DIRS_OUTFILE $dir;
}
}
else
{
# No valid option given put all back to file
print $fgyellow, "Invalid input, no entry deleted...\n", $reset;
foreach my $dir (@dirs)
{
print DIRS_OUTFILE $dir;
}
}
}
}
sub ReadEntry
{
my $viewCommands = $_[0];
my $tag = $ARGV[0];
my $answer = "";
my $command = "";
my $output = "";
if ($tag)
{
FindDirBasedOnTag($tag, \$command, \$output);
}
else
{
# No tag provided, print the complete list and let the user select.
my @completion_list = PrintDirs($viewCommands);
my $answer = Complete('Select (number or tag): ', \@completion_list);
my $length = @dirs;
if ($answer =~ /^\d+$/ && $answer <= $length)
{
$output = $dirs[$answer -1];
$command = $1 if $output =~ /^.*?cmd:(.*)/;
if ($output =~ /^\s*\//)
{
# No tag at start of line
$output =~ s%^\s*(.*?)\s+.*%$1%;
}
else
{
# has tag, remove it...
if ($output =~ /\$VOB_ROOT/)
{
$output =~ s%^\s*.*?(\$VOB_ROOT.*?)\s+.*%$1%;
}
else
{
$output =~ s%^\s*.*?(\/.*?)\s+.*%$1%;
}
}
if ($output =~ /VOB_ROOT/ && !$vobRoot)
{
print $fgyellow, 'Entry contains $VOB_ROOT but $VOB_ROOT not defined', "\n", $reset;
}
}
elsif ($answer !~ /^$/)
{
# Answer not empty and not digits, assume its a tag
FindDirBasedOnTag($answer, \$command, \$output);
}
}
if ($output)
{
if ($vobRoot)
{
# Replace VOB_ROOT keyword with current environment
$output =~ s%\$VOB_ROOT(.*)%$vobRoot$1%;
}
open OUTFILE, ">$cdDirFile"
or die "Error: Can't open $cdDirFile: $!\n";
print OUTFILE $output;
close OUTFILE;
if ($command)
{
open CMDFILE, ">$commandFile"
or die "Error: Can't open $commandFile: $!\n";
print CMDFILE "$command\n";
close CMDFILE;
}
}
}
sub FindDirBasedOnTag
{
my $tag = shift;
my $command = shift; # Passed as reference
my $output = shift; # Passed as reference
foreach my $dir (@dirs)
{
# Skip comment lines
next if $dir =~ /^\s*#.*/;
if ($dir =~ /^$tag\s/)
{
$$command = $1 if $dir =~ /^.*?cmd:(.*)/;
$dir =~ s%$tag\s+(.*?)\s+.*%$1%;
$$output = $dir; # row found, pick dir
last;
}
}
if (! $$output)
{
print $fgyellow,
"Can't find specified tag \"$tag\" in list. ",
"Try with the option -h or --help for more information\n", $reset;
}
elsif ($$output =~ /VOB_ROOT/ && !$vobRoot)
{
print $fgyellow, 'Entry contains $VOB_ROOT but $VOB_ROOT not defined', "\n", $reset;
}
}
sub ReadInputFile
{
# slurp the input file
`touch $dirsFile` if ! -e "$dirsFile";
open DIRS_INFILE, "$dirsFile" or die "Error: Can't open $dirsFile: $!\n";
@dirs = <DIRS_INFILE>;
close DIRS_INFILE;
}
sub ModifyCommand
{
PrintDirs("long");
print "select entry (number) to add or replace a command to: ";
my $answer = <STDIN>;
my $length = @dirs;
if ($answer =~ /^\d+$/ && $answer <= $length)
{
chomp $answer;
my $entry = $answer;
print
"Insert your command, if no input is given any existing ",
"command will be deleted.\n",
"Selected entry [$entry] Command: ";
my $command = <STDIN>;
chomp $command;
if ($command eq "")
{
print "No command provided...\n";
}
open DIRS_OUTFILE, ">$dirsFile"
or die "Error: Can't open $dirsFile: $!\n";
my $position = 0;
foreach my $dir (@dirs)
{
# Skip comment lines
next if $dir =~ /^\s*#.*/;
$position++;
if ($position != $entry)
{
print DIRS_OUTFILE $dir;
}
else
{
$dir =~ s/(.*?)\s+cmd:.*/$1/;
if ($command ne "")
{
chomp $dir;
print DIRS_OUTFILE "$dir cmd:$command\n";
print "Modified entry [$position] $dir cmd:$command\n";
}
else
{
# Remove existing command
print DIRS_OUTFILE $dir;
print "If existing command it's removed.\n";
}
}
}
}
elsif ($answer !~ /^$/)
{
print $fgyellow, "Invalid selection...\n", $reset;
}
}
sub PrintDirs
{
my $viewCommands = $_[0];
my @localDirs = @dirs; # local copy since we modify it.
my @completion_list;
my $counter = 0;
my $tag = "";
foreach my $dir (@localDirs)
{
# toggle line color to enhance readability
$counter++;
if ($counter % 2 == 0)
{
print $fgBlue;
}
else
{
print $fgRed;
}
my $command = "";
if ($dir =~ /(cmd:.*)/)
{
$command = $1 if $viewCommands eq "long";
}
# Remove the command keyword and all commands
$dir =~ s/^(.*?)\s+cmd:.*$/$1/g;
if ($dir !~ /^\// && $dir !~ /^\$/)
{
my @list = split(/\s+/, $dir);
my $tag = shift @list;
push @completion_list, $tag;
if ($command eq "")
{
printf "[%2s, %s] %s%s\n", $counter, $tag, @list, $reset;
}
else
{
printf "[%2s, %s] %s%s %s\n",
$counter, $tag, @list, $reset, $command;
}
}
else
{
# $dir starts with / as in a path e.g /bin/... or
# $dir starts with $ as in $VOB_ROOT.
if ($command eq "")
{
printf "[%2s] %s%s", $counter, $dir, $reset;
}
else
{
chomp $dir;
printf "[%2s] %s%s %s\n", $counter, $dir, $reset, $command;
}
}
}
return @completion_list;
}
sub SigintHandler
{
$SIG{INT} = \&SigintHandler;
print $reset, "\n";
if ($savedArgument eq "delete")
{
# Restore orignal file since it might have been opened for writing
open DIRS_OUTFILE, ">$dirsFile" or
die "Error: Can't open $dirsFile: $!\n";
# put all back to file
foreach my $dir (@dirs)
{
print DIRS_OUTFILE $dir;
}
}
exit(0);
}
sub GetCommandLineOptions
{
}
sub PrintHelp
{
print
"Usage: $myName [OPTION] [TAG]\n",
"Options:\n",
" -w - write current directory to list\n",
" -d - delete entry or multiple entries (comma separated)\n",
" -c - add, replace or delete a command to an existing entry\n",
" -l - print added commands to entries\n",
" -v, --version - output version information and exit\n",
" -h, --help - display this help and exit\n\n",
"Examples:\n",
"> $myName -w mytag - add current directory with shortcut TAG <mytag> to list\n",
"> $myName mytag - change to the directory mapped to TAG <mytag>.\n",
"> $myName -w - add current dir to the list\n",
"> $myName - show all entries and user can select from list\n",
"> $myName -d mytag - delete entry with TAG <mytag>.\n",
"> $myName -d - show all entries and user can delete entry or entries\n",
"> $myName -d all - reserved tag-keyword \"all\" deletes all entries in list.\n\n",
}