-
Notifications
You must be signed in to change notification settings - Fork 3
/
rofi-ag.sh
executable file
·204 lines (187 loc) · 4.83 KB
/
rofi-ag.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
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
#!/bin/bash
# use AG search with rofi
#
#------------ CONFIG ----------------#
# It support search text in symlink folder so you can add your symlink to this folder
SEARCH_DIRECTORY="$HOME/Desktop"
DIRECTORY_SHORTCUT=(
"~/Downloads"
"~/Documents"
)
OPENER=xdg-open
# load terminal with zsh shell.
# I need load shell because my nodejs config and pywall theme for vifm :)
# change to kitty by `kitty -e`
TERM_SHEL="alacritty -e /bin/zsh -i -c"
TEXT_EDITOR=nvim
# change to ranger or lf or vifm i need it open in tmux
FILE_MANAGER="$HOME/.config/my_scripts/m_tmux_fm.sh "
VIM_OPEN_EXT=(
"html"
"md"
"py"
"go"
"rb"
"php"
"lua"
"sh"
"cs"
"txt"
"ts"
"js"
"jsx"
)
#------------ CONFIG ----------------#
AG_TEXT_QUERY="--column --noheading --follow --depth 6"
AG_FILE_QUERY='-g "" --follow'
# MY_PATH="$(dirname "${0}")"
TMP_DIR="/tmp/rofi/${USER}/"
HIST_FILE="${TMP_DIR}/history.txt"
if [ ! -d "${TMP_DIR}" ]
then
mkdir -p "${TMP_DIR}";
fi
# Create hist file if it doesn't exist
if [ ! -f "${HIST_FILE}" ]
then
touch "${HIST_FILE}"
fi
function mExit(){
exec 1>&-
exit 1;
}
function searchAgText(){
isValid=0
query=$@
printf -v search_text "%q\n" "$@"
if [[ ${#query} -ge 3 ]]; then
query="ag $search_text $AG_TEXT_QUERY $SEARCH_DIRECTORY "
mapfile -t AG_RESULT < <(eval $query)
index=1
cat /dev/null > $HIST_FILE
for s in "${AG_RESULT[@]}"; do
if [[ ${#s} -ge 4 ]]; then
printf -v j "%02d" $index
COMMAND="$j:${s//$SEARCH_DIRECTORY/''}:t"
echo $COMMAND >> $HIST_FILE
echo $COMMAND
index=$((index + 1))
isValid=1
fi
done
fi
if [[ isValid -eq 0 ]]; then
echo "01:Not found:q"
echo "01:Not found:q" >> $HIST_FILE
return 0
else
return 0
fi
}
function searchAgFile(){
query="find $SEARCH_DIRECTORY "
mapfile -t AG_RESULT < <(eval $query)
index=1
cat /dev/null > $HIST_FILE
for folder in "${DIRECTORY_SHORTCUT[@]}"; do
printf -v j "%02d" $index
COMMAND="$j:${folder}:a"
echo $COMMAND >> $HIST_FILE
echo $COMMAND
index=$((index + 1))
done
for s in "${AG_RESULT[@]}"; do
if [[ ${#s} -ge ${#SEARCH_DIRECTORY}+3 ]]; then
printf -v j "%02d" $index
COMMAND="$j:${s//$SEARCH_DIRECTORY/''}:f"
echo $COMMAND >> $HIST_FILE
echo $COMMAND
index=$((index + 1))
fi
done
}
function checkNumber(){
re='^[0-9]+$'
if ! [[ $@ =~ $re ]] ; then
return 1
fi
return 0
}
function excute(){
readarray -t ARR < $HIST_FILE
for s in "${ARR[@]}"; do
if [[ "$1" == "${s:0:2}" ]]; then
if [[ "$2" == "q" ]]; then
exit 0
elif [[ "$2" == "t" ]]; then
IFS=':' read -r -a array <<< "$s"
file=${array[1]}
line=${array[2]}
column=${array[3]}
checkNumber $column
isLineNumber=$?
command="$TERM_SHEL '$TEXT_EDITOR \"+normal ${line}G${column}|\" $SEARCH_DIRECTORY${file}' "
coproc (eval $command)
mExit
elif [[ "$2" == "f" ]]; then
IFS=':' read -r -a array <<< "$s"
fileOpen $SEARCH_DIRECTORY${array[1]}
elif [[ "$2" == "a" ]]; then
IFS=':' read -r -a array <<< "$s"
fileOpen ${array[1]}
else
echo "01:not action:q"
fi
fi
done
}
function fileOpen(){
file=$@
filename="${file##*/}"
extension="${filename##*.}"
if [[ "$filename" == "$extension" ]]; then
coproc (eval "$TERM_SHEL '$FILE_MANAGER ${file}'" > /dev/null 2>&1 )
mExit
elif [[ -x "$file" ]]; then
coproc ($file> /dev/null 2>&1 )
mExit
elif [[ " ${VIM_OPEN_EXT[@]} " =~ " ${extension} " ]]; then
coproc ( eval "$TERM_SHEL '$TEXT_EDITOR ${file}'" > /dev/null 2>&1 )
mExit
else
coproc (eval "$OPENER ${file} " > /dev/null 2>&1 )
mExit
fi
}
if [[ -z $@ ]]; then
searchAgFile ""
elif [[ "$@" == "quit" ]]; then
exit 0
elif [ "$@" == "--testt" ]
then
echo "Search text"
read query
searchAgText $query
read query
./rofi-ag.sh $query
elif [ "$@" == "--testf" ]
then
echo "Search file"
searchAgFile ""
read query
./rofi-ag.sh $query
else
query=$@
COMMAND=${query:0:3}
last=${COMMAND:2:1}
action="${query: -1}"
if [[ $last == ":" ]]; then
excute "${COMMAND:0:2}" $action
else
if [[ ${query:0:1} == "'" ]]; then
searchAgText "${query:1}"
else
searchAgText "$query"
fi
fi
fi