-
Notifications
You must be signed in to change notification settings - Fork 0
/
easygrep.sh
74 lines (69 loc) · 1.81 KB
/
easygrep.sh
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
#set to 1 if easygrep should ignore case
EASYGREP_IGNORE_CASE=1
#TODO: allow multiple parameters for finding files that contain two or more words
#command to run for files
EASYGREP_FILE_COMMAND="$EDITOR"
if [[ -z $EASYGREP_FILE_COMMAND ]]; then
EASYGREP_FILE_COMMAND="vim"
fi
#options for grep
EASYGREP_GREP_OPTIONS="--color=always"
#ez find files which contain text
easyGrep() {
local QUERY
if [[ $EASYGREP_IGNORE_CASE -eq 1 ]]; then
QUERY="-riI"
else
QUERY="-rI"
fi
local DIR
local STR
if [[ -z $1 || $1 = "-h" || $1 = "--help" ]]; then
echo "Usage: [directory] <search-string>" >&2
echo "Example: easyGrep ~/projects somefunctionname" >&2
echo "use -h or --help to see this help message again" >&2
return 1;
elif [[ -z $2 ]]; then
DIR=.
STR=$1
else
DIR=$1
STR=$2
fi
grep $EASYGREP_GREP_OPTIONS $QUERY $STR $DIR
}
easyGrepOpen() {
local NUM=1
local RED_BOLD_FONT=$'\e[1m\e[91m'
local NORMAL_FONT=$'\e[0m\e[39m'
#find our files
local FILES=""
while IFS= read -r LINE || [[ -n "$LINE" ]]; do
FILES=$FILES$LINE$'\n'
printf "%s%s %s%s\n" $RED_BOLD_FONT $NUM $NORMAL_FONT $LINE
((NUM++))
done < <(easyGrep $1 $2)
if [[ ${#FILES} -eq 0 ]]; then
echo "No results found." >&2
return 1;
fi
printf %s "Enter a number to open or 0 to quit: "
read N
re='^[0-9]+$'
if ! [[ $N =~ $re ]] ; then
echo "error: Not a number" >&2; return 1
fi
if [[ $N = "0" ]] ; then
return 0
fi
LINE=$(printf "%s" "$FILES" | sed "${N}q;d")
#strip color if we have it
if [[ $OSTYPE = "darwin"* ]]; then
LINE=$(printf "%s" "$LINE" | sed -E 's/\E\[[0-9;]*[mK]//g')
else
LINE=$(printf "%s" "$LINE" | sed -r 's/\x1B\[[0-9;]*[mK]//g')
fi
local INDEX=$(expr index "$LINE" ":")
$EASYGREP_FILE_COMMAND ${LINE:0:($INDEX-1)} < /dev/tty
return 0;
}