-
Notifications
You must be signed in to change notification settings - Fork 3
/
.bash_funcs
177 lines (153 loc) · 4.78 KB
/
.bash_funcs
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
# Copyright (C) 2021-2022 Graz University of Technology.
# Search strings in files like
# fxgrep py RecordService
#
# find RecordService in *.py python files and exclude all files comming from
# directories which include invenio-rdm-records or test
# fxgrep py RecordService invenio-rdm-records test
#
# find RecordService in *.py files where RecordService prefix and suffix is not
# a character
# fxgrep py '[^a-z]RecordService[^a-z]'
#
# default is to search case insensitive, but sometimes it is easier to search
# case sensitive. the search string has to be prefixed with "i:"
# fxgrep py i:Owner
#
# FXGREP_EXCLUDE_PACKAGES = list of files/dirs which should be excluded automatically
# FXGREP_EXPLICIT_PACKAGES = list of files/dirs which shold be explicitly searched in
# above variables should be stored in a .fxgrep.conf file. this file
# could be placed everywhere. this file is searched in the parent
# directories of the current directory. the first found is used
function fxgrep() {
xagr="xargs grep -n -P --color=always"
notpaths=()
paths=()
space=$1
pattern=$2
interactive=true
if [[ ${pattern:0:2} == "i:" ]]
then
pattern=${pattern:2}
elif [[ ${pattern:0:2} == "d:" ]]
then
pattern="def\s${pattern:2}"
elif [[ ${pattern:0:2} == "v:" ]]
then
pattern="${pattern:2}\s="
elif [[ ${pattern:0:3} == "nc:" ]]
then
# \K keep left, only the pattern will be highlighted
# (?=) positive lookahead
# (?!) negative lookahead
pattern="^(?=[\s]*[^#])(?![\s]*\"\"\")(?![\s]*>>>)(?![\s]*\.\.\.)[^#]*\K(${pattern:3})"
else
xagr+=" -i"
fi
if [[ ${space:0:2} == "i:" ]]
then
interactive=true
space=${space:2}
elif [[ ${space:0:2} == "c:" ]]
then
space=${space:2}
filename="$(pwd)/.fxgrep.conf"
while [[ ! -f $filename ]]
do
filename=$(sed -E "s|(.*)/.*/.fxgrep.conf|\1/.fxgrep.conf|" <<< $filename)
done
if [[ ! -f $filename ]]
then
echo "no configuration file with name '.fxgrep.conf' found"
fi
. $filename
for item in "${FXGREP_EXCLUDE_PACKAGES[@]}"
do
notpaths+=( -not -path "*${item}*" )
done
for item in "${FXGREP_EXPLICIT_PACKAGES[@]}"
do
paths+=( "*${item}*" )
done
fi
if [[ $3 == "--not" ]]
then
for (( i=4; i <= $#; i++ ))
do
notpaths+=( -not -path "*${!i}*")
done
elif [[ $3 == "--explicit" ]]
then
for (( i=4; i <= $#; i++ ))
do
paths+=( "*${!i}*" )
done
fi
if [[ ${space} == "c++" ]]
then
file_extensions=".*\.(cpp|hpp|cc|c|h)"
elif [[ ${space} == "html" ]]
then
file_extensions=".*\.(html|less|css)"
elif [[ ${space} == "js" ]]
then
file_extensions=".*\.(js|jsx|svelte|ts|vue)"
notpaths+=( -not -path "*build*" )
notpaths+=( -not -path "*node_modules*" )
elif [[ ${space} == "php" ]]
then
file_extensions=".*\.(php|tpl)"
elif [[ ${space} == "xml" ]]
then
file_extensions=".*\.(xml|xsl)"
elif [[ ${space} == "tex" ]]
then
file_extensions=".*\.tex"
elif [[ ${space} == "py" ]]
then
file_extensions=".*\.py"
else
file_extensions=".*${space}"
fi
output=$(find -L ${paths[@]} -type f -regextype posix-egrep -regex ${file_extensions} "${notpaths[@]}" | sed 's/ /\\ /g' | $xagr ${pattern})
readarray -t files <<< $output
if $interactive
then
size=${#files[@]}
base=$(pwd)
prompt="Enter line number to open file: "
for i in "${!files[@]}"
do
printf "%${#size}s: %s\n" "$i" "${files[$i]}"
done
while read -p "$prompt" file_number && [[ "${file_number}" != "q" ]]
do
uncolored_line=$(echo ${files[$file_number]} | sed -E 's/\x1b\[[0-9;]*[mK]//g')
line_number=$(echo ${uncolored_line} | sed -E "s/.*?:([0-9]+):.*/\1/g")
filename=$(echo ${uncolored_line} | sed "s/\.${space}.*$/.${space}/g")
emacsclient -n --alternate-editor="" --no-wait --eval "(progn (find-file \"$base/${filename}\") (goto-line ${line_number}) (hl-line-mode t) (recenter))" > /dev/null 2>&1
done
else
printf "%s\n" "${files[@]}"
fi
}
# show the name of the current git branch
function gitBranch() {
git branch 2> /dev/null | sed -r -e '/^[^*]/d' -e 's/\* (.+)$/(\1) /'
}
# see if there is a open ssh session. the possible name of the session could be
# detected also in the sed command like 's/^(ssh).*(Max|Moritz).*/(\1@\2) /'. it
# would be clearer which session is open
function isSSHActive() {
ssh-add -L 2> /dev/null | sed -r -e 's/^(ssh).*/(\1) /'
}
# the bash prompt could be overriden. to use it like that put the "bash_prompt"
# command into the .bashrc file
function bash_prompt() {
local none="\[\e[00m\]"
local red="\[\e[31m\]"
local green="\[\e[32m\]"
local boldBlue="\[\e[01;34m\]"
local white="\[\e[37m\]"
export PS1="${white}\u ${green}\$(isSSHActive)${boldBlue}\W ${red}\$(gitBranch)${none}\$${none} "
}