forked from sampson-chen/sack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsack
executable file
·556 lines (496 loc) · 19.8 KB
/
sack
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
#!/bin/bash
#
# @filename: sack
#
# @author: Sampson L. Chen (sampson-chen / slchen)
# @date: 2012-10-14
#
# @description:
# s(hortcut)-ack - a faster way to use ack (or grep)!
#
# For more details:
# https://github.com/sampson-chen/sack
#
# @dependencies:
# - ack (http://betterthangrep.com/)
# - tee
# - awk
# - sed (GNU sed)
#
# @version:
# 1.0
#
# @license:
# - MIT as of this version.
#
# @usage:
# - for additional usage information, see README.md
# ==============================================================
# ==============================================================
# Script Functions
# Print information about the current profile
print_current_profile_info() {
echo ""
echo "===> Current Profile: $sack__profile_name"
echo "===> Using flags: $sack__profile_preset_flags"
echo "===> Searching under: $sack__profile_preset_directory"
}
# Print help / usage information about sack
print_help() {
sp=" " # space
echo ""
echo "===============> sack Usage Information <==============="
echo ""
echo "===> To print this message: <==="
echo "$sp sack"
echo "$sp sack -h"
echo "$sp sack --help"
echo "$sp sack --info"
echo ""
echo "===> To learn more about ack: <==="
echo "$sp ack --help"
echo "$sp man ack-grep"
echo ""
echo "========> sack-specific commands <========"
echo ""
echo "===> Which Profile? <==="
echo "To find out which profile you are currently on:"
echo "$sp sack -wp"
echo "$sp sack --whichprofile"
echo ""
echo "===> Switch Profile <==="
echo "To switch to a different profile:"
echo "$sp sack -sp PROFILE_NAME"
echo "$sp sack --switchprofile no_profile"
echo ""
echo "===> Rename Profile <==="
echo "To rename the current profile:"
echo "$sp sack -rp NEW_PROFILE_NAME"
echo "$sp sack --renameprofile reviewboard"
echo ""
echo "===> Set Flags <==="
echo "To set new preset flags to use for the current profile:"
echo "(All searches run using this profile will use these flags)"
echo "$sp sack -sf NEW_FLAGS"
echo "$sp sack --setflags -ia -A 2 -B 3"
echo ""
echo "===> Set Directory <==="
echo "To set a new preset directory to use for the current profile:"
echo "(All searches will be run under this directory with this profile)"
echo "$sp sack -sd NEW_DIRECTORY"
echo "$sp sack --setdirectory ~/src/reviewboard"
echo ""
echo "===> Add New Profile <==="
echo "To add a new empty profile:"
echo "$sp sack -anp PROFILE_NAME"
echo "$sp sack --addnewprofile ReviewBoard"
echo ""
echo "===> List Profiles <==="
echo "To show the current available profiles:"
echo "$sp sack -lp"
echo "$sp sack --listprofiles"
print_current_profile_info
}
# Prefixes a shortcut tag to relevant output lines.
display_shortcuts() {
# Note that by default ack uses the --nogroup -H option by default when
# output is piped or redirected, so we do get a file name on each line as $1
# We could have restored the default look for ack with the --group option, but
# it turns out that this is actually simpler to parse with --nogroup and then
# reformat the output to match the --group formats.
awk -v f_name=$sack__file_name -F':' '
BEGIN {x=1};
{
if ($1 != f_name) {
printf("\n%s\n", $1);
f_name=$1;
};
printf("[%s] ", x);
printf("%s", $2);
for (i=3; i<=NF; i++) {
printf(":%s", $i);
}
printf("\n");
x++;
};'
}
# Processes the output that goes into the shortcut file:
# Format: line_number:full_file_path
process_shorcut_paths() {
# Using : as the delimiter here should be fine, because : is not used in file names
awk -F':' '
{
print $2 " " $1;
};'
}
# Remove the escaped characters that get piped from ack in order to preserve
# colored output to stdout
remove_escaped_chars() {
# Need to do a check for the OS, because Linux uses a different sed
# than OS X
# Linux
if [[ $sack__OS == "Linux" ]]; then
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
# OS X
elif [[ $sack__OS == "Darwin" ]]; then
sed -E "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
fi
# @todo: implement support for other OSes at a later date
}
# Create the actual shortcut command to be used across terminals / shell sessions
create_shortcut_cmd() {
# @todo: add support for other editors as well
# The following literally writes the shorcut command and makes it executable
sack__shortcut_cmd_path=~/bin/$sack__shortcut_cmd
echo "#!/bin/bash" > $sack__shortcut_cmd_path
echo "sack__vim_shortcut=\$(sed -n \"\$1p\" < $sack__shortcut_file)" >> $sack__shortcut_cmd_path
echo "$sack__default_editor +\$sack__vim_shortcut" >> $sack__shortcut_cmd_path
chmod +x $sack__shortcut_cmd_path
}
# Switch to a different profile
switchprofile() {
# Get the specified profile name from cmdline
sack__selected_profile=$1
# Try to find the corresponding profile entry that matches the name EXACTLY
# with an entry in config file
# If there are multiple profile names that match exactly,
# (i.e. in the case where currrent profile is already the one we want
# to switch to, use the first match)
# then use only the first entry (i.e. the first 3 lines)
sack__switch_to_profile_raw=$(cat $sack__config_path | grep --line-regexp -A 2 "sack__profile_name=$sack__selected_profile" | head -3)
# If we can't find an exact match with grep, bail
if [[ -z $sack__switch_to_profile_raw ]]; then
echo ""
echo "===> Error: profile \"$sack__selected_profile\" is not found"
echo ""
echo "To create a new profile, do:"
echo " sack --anp PROFILE_NAME"
echo " sack --addnewprofile ReviewBoard"
return 1
fi
# Get the flags for this profile entry
sack__new_profile_preset_flags=$(echo "$sack__switch_to_profile_raw" | grep "sack__profile_preset_flags" | sed -e "s/sack__profile_preset_flags=//")
# Get the search directory for this profile entry
sack__new_profile_preset_directory=$(echo "$sack__switch_to_profile_raw" | grep "sack__profile_preset_directory" | sed -e "s/sack__profile_preset_directory=//")
# Process the information that need to be written back to the config file
# into a temporary file so we don't have I/O conflict
cat $sack__config_path | awk -v new_profile_name="$sack__selected_profile" -v new_profile_preset_flags="$sack__new_profile_preset_flags" -v new_profile_preset_directory="$sack__new_profile_preset_directory" 'BEGIN {in_cur_profile_block=0};
{
if ( in_cur_profile_block==0 ) {
print $0;
}
if ( $2=="sack__beacon_tag_current_profile" ) {
if ( $3=="BEGIN" ) {
in_cur_profile_block=1;
print "#"
print "sack__profile_name=" new_profile_name;
print "sack__profile_preset_flags=" new_profile_preset_flags;
print "sack__profile_preset_directory=" new_profile_preset_directory;
} else if ( $3=="END" ) {
in_cur_profile_block=0;
print "#"
print $0
}
}
};' > $sack__config_path.tmp
# Swap the temp file into the config file to avoid I/O conflict
rm $sack__config_path
mv $sack__config_path.tmp $sack__config_path
# Tell user that which profile sack has switched to
echo ""
echo "===> sack: switched profile <==="
echo "Now using profile: $sack__selected_profile"
echo "Profile preset flags: $sack__new_profile_preset_flags"
echo "Profile preset directory: $sack__new_profile_preset_directory"
}
renameprofile() {
sack__new_profile_name=$1
# Validation for the new profile name: it must not be empty
if [[ -z "$sack__new_profile_name" ]]; then
echo ""
echo "===> Must specify a new profile name!"
return 1
elif [[ "$sack__new_profile_name" == "no_profile" ]]; then
echo ""
echo "===> Must not rename current profile to \"no_profile\"!"
return 1
fi
# Read the current profile directly from the config file to avoid sync issues
sack__current_profile_raw=$(cat $sack__config_path | grep -A 3 "# sack__beacon_tag_current_profile BEGIN")
# Get the current profile name
sack__profile_name=$(echo "$sack__current_profile_raw" | grep "sack__profile_name=" | sed -e "s/sack__profile_name=//")
# Disallow users to rename no_profile so they can always quickly
# access default behaviour of sack
if [[ "$sack__profile_name" == "no_profile" ]]; then
echo ""
echo "===> Renaming for \"no_profile\" is disallowed!"
return 1
fi
# Process the information that need to be written back to the config file
# into a temporary file so we don't have I/O conflict
cat $sack__config_path | awk -v replace_flag="sack__profile_name=$sack__profile_name" -v new_profile_name=$sack__new_profile_name '{
if ($0==replace_flag) {
print "sack__profile_name=" new_profile_name
} else {
print $0
}
};' > $sack__config_path.tmp
# Swap the temp file into the config file to avoid I/O conflict
rm $sack__config_path
mv $sack__config_path.tmp $sack__config_path
echo ""
echo "===> sack: renamed profile <==="
echo "Profile renamed from \"$sack__profile_name\" to: \"$sack__new_profile_name\""
}
setflags() {
sack__new_flags=$@
# Read the current profile directly from the config file to avoid sync issues
sack__current_profile_raw=$(cat $sack__config_path | grep -A 4 "# sack__beacon_tag_current_profile BEGIN")
# Get the current profile name
sack__profile_name=$(echo "$sack__current_profile_raw" | grep "sack__profile_name=" | sed -e "s/sack__profile_name=//")
# Disallow users to change flags for no_profile so they can always quickly
# access default behaviour of sack
if [[ "$sack__profile_name" == "no_profile" ]]; then
echo ""
echo "===> Preset flags are disabled for \"no_profile\"!"
echo "First switch to a profile before setting preset flags:"
echo " sack -sp PROFILE_NAME"
return 1
fi
# Get the search directory for this profile entry
sack__profile_preset_directory=$(echo "$sack__current_profile_raw" | grep "sack__profile_preset_directory" | sed -e "s/sack__profile_preset_directory=//")
# Process the information that need to be written back to the config file
# into a temporary file so we don't have I/O conflict
cat $sack__config_path | awk -v replace_flag="sack__profile_name=$sack__profile_name" -v new_profile_preset_flags="$sack__new_flags" -v profile_preset_directory="$sack__profile_preset_directory" 'BEGIN {do_replace=0};
{
if ($0==replace_flag) {
do_replace=1;
} else if ($0=="#"){
do_replace=0;
}
if (do_replace==1 && $0==replace_flag) {
print replace_flag
print "sack__profile_preset_flags=\"" new_profile_preset_flags "\"";
print "sack__profile_preset_directory=" profile_preset_directory;
} else if (do_replace==0) {
print $0
}
};' > $sack__config_path.tmp
# Swap the temp file into the config file to avoid I/O conflict
rm $sack__config_path
mv $sack__config_path.tmp $sack__config_path
echo ""
echo "===> sack: set flags <==="
echo "Preset flags for profile \"$sack__profile_name\" now set to: $sack__new_flags"
}
setdirectory() {
sack__new_directories=$@
# Validate the list of all directories passed in
for sack__new_directory in $sack__new_directories
do
# Validate that it is a valid directory path
if [[ ! -d "$sack__new_directory" ]]; then
echo ""
echo "===> Invalid directory path: $sack__new_directory does not exist!"
return 1
fi
done
# Read the current profile directly from the config file to avoid sync issues
sack__current_profile_raw=$(cat $sack__config_path | grep -A 4 "# sack__beacon_tag_current_profile BEGIN")
# Get the current profile name
sack__profile_name=$(echo "$sack__current_profile_raw" | grep "sack__profile_name=" | sed -e "s/sack__profile_name=//")
# Disallow users to change flags for no_profile so they can always quickly
# access default behaviour of sack
if [[ "$sack__profile_name" == "no_profile" ]]; then
echo ""
echo "===> Preset directories are disabled for \"no_profile\"!"
echo "First switch to a profile before setting preset directories:"
echo " sack -sp PROFILE_NAME"
return 1
fi
# Get the preset flags for this profile entry
sack__profile_preset_flags=$(echo "$sack__current_profile_raw" | grep "sack__profile_preset_flags" | sed -e "s/sack__profile_preset_flags=//")
# Process the information that need to be written back to the config file
# into a temporary file so we don't have I/O conflict
cat $sack__config_path | awk -v replace_flag="sack__profile_name=$sack__profile_name" -v profile_preset_flags="$sack__profile_preset_flags" -v new_profile_preset_directories="$sack__new_directories" 'BEGIN {do_replace=0};
{
if ($0==replace_flag) {
do_replace=1;
} else if ($0=="#"){
do_replace=0;
}
if (do_replace==1 && $0==replace_flag) {
print replace_flag
print "sack__profile_preset_flags=" profile_preset_flags;
print "sack__profile_preset_directory=\"" new_profile_preset_directories "\"";
} else if (do_replace==0) {
print $0
}
};' > $sack__config_path.tmp
# Swap the temp file into the config file to avoid I/O conflict
rm $sack__config_path
mv $sack__config_path.tmp $sack__config_path
echo ""
echo "===> sack: set directory(s) <==="
echo "Preset directory(s) for profile \"$sack__profile_name\" now set to: \"$sack__new_directories\""
}
addnewprofile() {
sack__add_profile_name=$1
# Validation for the new profile name: it must not be empty
if [[ -z "$sack__add_profile_name" ]]; then
echo ""
echo "===> Must specify a name when adding a new profile!"
return 1
elif [[ "$sack__add_profile_name" == "no_profile" ]]; then
echo ""
echo "===> Must not add new profile with name \"no_profile\"!"
return 1
fi
# Process the information that need to be written back to the config file
# into a temporary file so we don't have I/O conflict
cat $sack__config_path | awk -v add_profile_name=$sack__add_profile_name '{
if ($0=="# sack__beacon_tag_new_profiles") {
print "sack__profile_name=" add_profile_name
print "sack__profile_preset_flags=\"\""
print "sack__profile_preset_directory=\"\""
print "#"
print "# sack__beacon_tag_new_profiles"
} else {
print $0
}
};' > $sack__config_path.tmp
# Swap the temp file into the config file to avoid I/O conflict
rm $sack__config_path
mv $sack__config_path.tmp $sack__config_path
echo ""
echo "===> sack: added new profile <==="
echo "New Profile: $sack__add_profile_name"
}
listprofiles() {
# print the profiles to stdout
echo ""
echo "===> sack: list profiles <==="
echo "The following profiles are available for use:"
cat $sack__config_path | awk 'BEGIN {do_print=0};
{
if ($0=="# sack__beacon_tag_profiles") {
do_print=1
} else if ($0=="# sack__beacon_tag_new_profiles") {
do_print=0
}
if (do_print==1) {
if ($1=="#") {
if ($2=="") {
print " "
}
} else {
print $0
}
}
};'
}
# =============================================
# ================ Script Main ================
# =============================================
# Where to find the config file
sack__config_path=~/.sackrc
# Initializing the variable for checking different file names
sack__file_name=" "
# Deal with the options that only have to do with sack instead of ack
sack__option=$1
# By default, use ack:
sack__default_tool=ack
# Color parameter is different for ack / ag than for grep:
sack__color_param='--color'
echo "sack__option is: $sack__option"
# Determine which search tool to use
if [[ "$sack__option" == "-ag" ]]; then
sack__default_tool=ag
shift
# Determine which search tool to use
elif [[ "$sack__option" == "-grep" ]]; then
sack__default_tool='grep'
sack__color_param='--color=always'
shift
# sack profiles - allow switching between different profiles
# Show help printout
elif [[ -z "$sack__option" || "$sack__option" == "-h" || "$sack__option" == "--help" || "$sack__option" == "--info" ]]; then
. $sack__config_path
print_help
echo ""
exit 0
elif [[ "$sack__option" == "-wp" || "$sack__option" == "--whichprofile" ]]; then
. $sack__config_path
print_current_profile_info
echo ""
exit 0
# Switch profiles
elif [[ "$sack__option" == "-sp" || "$sack__option" == "--switchprofile" ]]; then
switchprofile $2
echo ""
exit 0
# Rename the current profile
elif [[ "$sack__option" == "-rp" || "$sack__option" == "--renameprofile" ]]; then
renameprofile $2
echo ""
exit 0
# Set new preset flags for current profile
elif [[ "$sack__option" == "-sf" || "$sack__option" == "--setflags" ]]; then
shift
setflags $@
echo ""
exit 0
# Set new preset search directory for current profile
elif [[ "$sack__option" == "-sd" || "$sack__option" == "--setdirectory" ]]; then
shift
setdirectory $@
echo ""
exit 0
# Add a new profile
elif [[ "$sack__option" == "-anp" || "$sack__option" == "--addnewprofile" ]]; then
addnewprofile $2
echo ""
exit 0
# Print the current profiles that are available
elif [[ "$sack__option" == "-lp" || "$sack__option" == "--listprofiles" ]]; then
listprofiles
exit 0
fi
# Set up the variables to use for sack
. $sack__config_path
# Check to see if sack should be running with a particular profile:
if [[ "$sack__profile_name" == "no_profile" ]]; then
# We add the pwd as last argument to ack so the file names of output
# are displayed as absolute paths, so that they can be shared across
# multiple terminal / shell sessions
sack__flags=""
sack__cwd=$(pwd)
else
sack__flags="$sack__profile_preset_flags"
sack__cwd="$sack__profile_preset_directory"
# Add automatic tilda expansion if it's not in already
sack__cwd=$(eval "echo $sack__cwd")
fi
# Announce start of sack:
echo ""
echo "============> running $sack__default_tool! <============"
# Note that this is different from print_current_profile_info()
echo ""
echo "===> Current Profile: $sack__profile_name"
echo "===> Using flags: $sack__flags"
echo "===> Searching under: $sack__cwd"
echo "===> Searching parameters: $@"
echo ""
# The actual wrapper around ack
$sack__default_tool $sack__color_param $sack__flags $@ $sack__cwd | tee >$sack__dev_null >(display_shortcuts) >(process_shorcut_paths | remove_escaped_chars > $sack__shortcut_file)
# Show usage instructions for first-time users; this can be turned off
if [[ $sack__show_instructions -eq 0 ]]; then
# do nothing
echo ""
else
echo ""
echo "===== Use: \"$sack__shortcut_cmd #\" to go directly to the search result!"
echo "===== Example: ( user@linux:~$ $sack__shortcut_cmd 25 )"
echo ""
fi
create_shortcut_cmd