-
Notifications
You must be signed in to change notification settings - Fork 1
/
subgoto.sh
executable file
·154 lines (142 loc) · 4.57 KB
/
subgoto.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
#!/usr/bin/env sh
#This content is released under the (https://github.com/horttanainen/goto/blob/master/LICENSE) MIT License.
subgoto() {
keyword="$*"
gotofolder() {
if [ -d "$*" ]; then
cd "$*"
else
src="$*"
base=${src##*/}
dir=${src%"$base"}
dirtest=$(echo "$dir" | tr -d '[:space:]') #fix for issue #2
if [ -z $dirtest ]; then
cd $(dirname $src)
else
cd $dir
fi
fi
}
scrollsearchresults() {
printf "Press (space) to goto, (n) next result, (p) previous.\n\n"
line=1
maxlines=$(echo "$result" | wc -l)
while true; do
folder="$(echo "$result" | tail -n+$line | head -n1)"
echo $folder
old_stty_cfg=$(stty -g)
stty raw -echo ; key=$(head -c 1) ; stty $old_stty_cfg
if [ "$key" = "n" ]; then
if [ "$line" -eq "$maxlines" ]; then
line=1
else
line=`expr $line + 1`
fi
elif [ "$key" = "p" ]; then
if [ "$line" -eq 1 ]; then
line=$maxlines
else
line=`expr $line - 1`
fi
elif [ "$key" = " " ]; then
echo "$folder" | cat - "$lines" > $tmpfile && mv $tmpfile "$lines"
gotofolder $folder
break
else
break
fi
done
}
findfromroot() {
printf "No match with ${keyword}. Look from root? (space)\n"
old_stty_cfg=$(stty -g)
stty raw -echo ; key=$(head -c 1) ; stty $old_stty_cfg
if [ "$key" = " " ]; then
result=$(find / -name "${keyword}*" -printf "%T@ %p\n" 2> /dev/null | sort -r -k 1 -n | sed 's/^[^ ]* //')
if [ -z "$result" ]; then
printf "No match with $keyword \n"
else
scrollsearchresults
fi
else
return
fi
}
findfromcurrent() {
result=$(find "$workdir" -name "${keyword}*" -printf "%T@ %p\n" 2> /dev/null | sort -r -k 1 -n | sed 's/^[^ ]* //')
if [ -z "$result" ]; then
findfromroot
else
scrollsearchresults
fi
}
scrollgotos() {
printf "Press (space) to goto, (n) next result, (p) previous.\n"
printf "Press (s) to do a search from this directory.\n\n"
line=1
maxlines=$(echo "$result" | wc -l)
while true; do
folders=$(echo "$result" | tail -n+$line | head -n1)
folder="$(echo "$folders" | cut -d':' -f2)"
deleteline="$(echo "$folders" | cut -d':' -f1)"
echo "$folder"
old_stty_cfg=$(stty -g)
stty raw -echo ; key=$(head -c 1) ; stty $old_stty_cfg
if [ "$key" = "n" ]; then
if [ "$line" -eq "$maxlines" ]; then
line=1
else
line=`expr $line + 1`
fi
elif [ "$key" = "p" ]; then
if [ "$line" -eq 1 ]; then
line=$maxlines
else
line=`expr $line - 1`
fi
elif [ "$key" = " " ]; then
sed "${deleteline}d" "$lines" > $tmpfile; mv $tmpfile "$lines"
echo "$folder" | cat - "$lines" > $tmpfile && mv $tmpfile "$lines"
gotofolder $folder
break
elif [ "$key" = "s" ]; then
findfromcurrent
break
else
break
fi
done
}
lookfromgotos() {
matches=$(grep -n -o "/[^/]*$" $lines )
if echo "$matches" | grep -q ".*${keyword}.*" ; then
echo "$matches" | grep ".*${keyword}.*" | cut -f1 -d":" > $linenum
cat $linenum | xargs -I{} -d"\n" sed -n "{}p" $lines > $paths
result=$(paste $linenum $paths -d ":")
scrollgotos
else
findfromcurrent
fi
}
checkforroot() {
if [ $(echo "${#keyword}") = 1 ]; then
if [ $keyword = "/" ]; then
gotofolder $keyword
else
removeslash
fi
else
removeslash
fi
}
removeslash() {
lastchar=${keyword#"${keyword%?}"}
if [ $lastchar = "/" ]; then
keyword=${keyword%?}
lookfromgotos
else
lookfromgotos
fi
}
checkforroot
}