-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheat.sh
executable file
·158 lines (127 loc) · 2.93 KB
/
cheat.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
#!/usr/bin/env bash
set -o pipefail # propagate errors
set -u # exit on undefined
set -e # exit on non-zero return value
#set -f # disable globbing/filename expansion
shopt -s failglob # error on unexpaned globs
source ~/Documents/scripts/source-me/posix-compliant-shells.sh
find_path="$1"
shift
# snatched from https://stackoverflow.com/a/11396899
declare -A map
for name in pdf png jpg gif; do
map["$name"]=1
done
_help() {
cat <<EOF
USAGE: cheat [--edit|--find|--grep] [-d] [FILENAME]
Displays files in less, image viewer, pdf viewer or browser by default.
If \`edit\` is specified, the file will be openend for editing in vim (if not one of $(for key in "${!map[@]}"; do echo -n "$key, "; done)html).
If \`find\` is specified, find will be run in the cheatsheets dir.
If \`grep\` is specified, grep will be run in the cheatsheets dir.
COMMANDS:
edit
find OPTIONS FIND_STRING Run find in cheatsheets DIR
$ cheat find -name '*find*'
+ find $find_path -name '*find*'
grep OPTIONS GREP_STRING Run grep in cheatsheets DIR
$ cheat grep -n ' find '
+ grep -n ' find ' --color=always -r $find_path
$ cheat grep -d darwin -n 'tmutil'
+ grep -n 'tmutil' --color=always -r $find_path/darwin
EOF
}
if [ $# -eq 0 ]; then
_help
exit
fi
command=('lessc')
subdir='.'
while [ $# -gt 0 ]; do
key="$1"
case "$key" in
edit)
command=('nvim')
shift
;;
find)
command=('find')
shift
;;
grep)
command=('grep' --exclude='*.html')
shift
;;
-d)
subdir="$2"
shift 2
;;
-h|--help)
_help
exit 0
;;
--)
shift
break
;;
*)
break
;;
esac
done
if [ "${command[0]}" = 'find' ]; then
set -x
"${command[@]}" "$find_path" -path "*/.git" -prune -o "$@"
set +x
exit
elif [ "${command[0]}" = 'grep' ]; then
set -x
(
cd "$find_path" && \
"${command[@]}" --exclude-dir=.git "$@" --color=always -r "$subdir"
)
set +x
exit
fi
filename="${*:$#}" # last argument
file="$find_path"/"$filename"
# shellcheck disable=SC2001
extension="$(echo "$filename" | sed 's#.*\.##')"
use_system_open=""
set +u
if [[ ${map["$extension"]} ]] ; then
use_system_open="true"
fi
set -u
if [ -n "$use_system_open" ]; then
if [ "$(uname)" = Darwin ]; then
open "$file"
else
xdg-open "$file" &
fi
elif [ "$extension" = html ] && [ "${command[0]}" != 'nvim' ] ; then
if [ "$(uname)" = Darwin ]; then
set -x
base_temp_dir="$(mktemp -d)"
cleanup () {
rm -r "$base_temp_dir"
}
trap cleanup EXIT
set +x
else
set -x
base_temp_dir=~/Downloads
set +x
fi
temp_file="$base_temp_dir"/"$(basename "$filename")"
cp "$file" "$temp_file" # for firejail on Linux
call_browser "file://$temp_file"
else
# not html and either `lessc` or `nvim`
if [ $# -eq 0 ]; then
set -- "$find_path"
else
set -- "${@:1:$(($#-1))}" # all except last = filename
fi
"${command[@]}" "$@" "$file"
fi