-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheat
executable file
·94 lines (80 loc) · 1.52 KB
/
cheat
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
#!/usr/bin/env bash
# Deps:
# - bat (https://github.com/sharkdp/bat)
# - curl
: "${EDITOR:=vi}"
baseurl="https://cheat.sh"
CHEAT_DIR=$XDG_DATA_HOME/cheat
edit_flag=false
download_flag=false
delete_flag=false
if [ ! -d "$CHEAT_DIR" ]; then
echo "Creating directory for cheatsheets at $CHEAT_DIR"
mkdir $CHEAT_DIR
fi
usage() {
echo " Usage: cheat [options] <argument>
options:
-h Show this help
-e Edit cheatsheet for <argument>
-d Download cheatsheet for <argument> from https://cheat.sh
-D Delete cheatsheet for <argument>
-l List cheatsheets"
}
_bat() {
$(which bat) -pp -l bash "$@"
}
download() {
cmd=$1
opts=$2
if [ -n "$cmd" ]; then
curl --silent "${baseurl}/${cmd}?T${opts}" > $CHEAT_DIR/${cmd}
else
echo "Please provide cmd"
fi
}
while getopts "edlDh" opt; do
case "${opt}" in
e) edit_flag=true ;;
d) download_flag=true ;;
D) delete_flag=true ;;
l)
ls $CHEAT_DIR
exit 0
;;
h) usage && exit 0 ;;
\?)
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ -z "$1" ]; then
echo "Must provide an argument"
exit 1
fi
if ($delete_flag); then
rm $CHEAT_DIR/$1
exit 0
fi
if ($download_flag); then
download "$1"
retval=$?
case "$retval" in
0) echo "Download successful: $1" ;;
*)
echo "Download not successful: $1"
exit 1
;;
esac
fi
if ($edit_flag); then
$EDITOR $CHEAT_DIR/$1
exit 0
fi
if [ -f "$CHEAT_DIR/$1" ]; then
_bat "$CHEAT_DIR/$1"
else
echo "Cheat not found"
fi