-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode-browser.bash
More file actions
70 lines (60 loc) · 1.66 KB
/
code-browser.bash
File metadata and controls
70 lines (60 loc) · 1.66 KB
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
# Pedro Fonseca, 2018
#
# Script to navigate code quickly
# Requirements: fzf
# https://github.com/rssys/code-browser
# Use highlight instead of cat if available
mycat() {
if hash highlight 2>/dev/null; then
echo "highlight -O ansi"
else
echo "cat"
fi
}
# Edit a file using vim on a given line number
editline() {
EDITOR=${EDITOR-vim}
if [ "$1" != "" ]
then
EDITOR_ARGS=$(echo "$1" | sed -e 's/\([^:]*\):\([^:]*\).*/\1 +\2/')
${EDITOR} $EDITOR_ARGS
fi
}
##########################################################################
# Calls fzf to grep the code and then runs vim
# Receives one optional argument (the initial query)
g(){
QUERY=""
if [ $# -gt 0 ]
then
QUERY="-q ""$@"
fi
COLOR="--color=hl:124"
CAT_COMMAND=$(mycat)
MATCH=$(cat INDEX.txt | grep '\t' | sed -e "s/\([: ]\)$(printf '\t')/\1 /" | fzf --ansi --print-query $COLOR --reverse -n 2.. -e +s -d ":" $QUERY --preview "${CAT_COMMAND} {1} |head -100 ")
MATCH_QUERY=$(echo "$MATCH" | head -n 1)
MATCH_SELECTION=$(echo "$MATCH" | tail -n 1)
editline "$MATCH_SELECTION"
}
# Repeatedly calls fzf and vim
# Receives one optional arguemnt (the initial query)
gg(){
QUERY=""
if [ $# -gt 0 ]
then
QUERY="-q ""$@"
fi
while true;
do
COLOR="--color=hl:124"
CAT_COMMAND=$(mycat)
MATCH=$(cat INDEX.txt | grep '\t' | sed -e "s/\([: ]\)$(printf '\t')/\1 /" | fzf --ansi --print-query $COLOR --reverse -n 2.. -e +s -d ":" $QUERY --preview "${CAT_COMMAND} {1} |head -100 ")
QUERY="-q $(echo "$MATCH" | head -n 1)"
MATCH_SELECTION=$(echo "$MATCH" | tail -n 1)
if [ $(echo "$MATCH" | wc -l) -ne 2 ]
then
return
fi
editline "$MATCH_SELECTION"
done
}