-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheat
executable file
·86 lines (73 loc) · 2.87 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
#!/bin/sh
ESC_R="\x1b[0m"
ESC_CC="\x1b[38;5;246m\x1b[3m"
ESC_TL="\x1b[48;5;8m"
PGER="less -RF"
CHEAT_PAGES_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/cheat"
TMPDIR="$CHEAT_PAGES_DIR/tmpdir"
PAGEFILE="$CHEAT_PAGES_DIR/page"
update () {
set -e
mkdir -p "$TMPDIR"
[ -f "$PAGEFILE" ] && COUNT="$(grep -c '^\[T\]' "$PAGEFILE")"
#Clone Repos And Extract Pages
#tldr
echo "Cloning: TLDR"
git clone -q https://www.github.com/tldr-pages/tldr "$TMPDIR/tldr"
echo "TLDR: Extracing Pages"
mkdir -p "$TMPDIR/tldr-cv"
mv "$TMPDIR"/tldr/pages/common/* "$TMPDIR/tldr-cv"
mv "$TMPDIR"/tldr/pages/linux/* "$TMPDIR/tldr-cv"
echo "TLDR: Converting Pages"
echo > "$PAGEFILE"
for tldr_page in $(find "$TMPDIR/tldr-cv" ! -name 'tldr-cv' -printf '%f\n' | sed 's/.md$//'); do
printf "[T]%s:\n" "tldr/$tldr_page" >> "$PAGEFILE"
sed 's/{{|}}//g; s/^[->]/#/g; s/^`\|`$//g;' "$TMPDIR/tldr-cv/$tldr_page.md" >> "$PAGEFILE"
done
#cheat
echo "Cloning: cheat"
git clone -q https://github.com/cheat/cheatsheets "$TMPDIR/cheat"
echo "cheat: Extracting Pages"
rm -rf "$TMPDIR"/cheat/.git* "$TMPDIR/cheat/vim-plugins"
find "$TMPDIR/cheat" -maxdepth 1 -type f -exec sh -c \
'printf "[T]%s:\n" "cheat/$(basename $1)" >> '"$PAGEFILE"' && cat "$1" >> '"$PAGEFILE" sh {} \;
#cheat_sheets
echo "Cloning: cheat.sheets"
git clone -q https://github.com/chubin/cheat.sheets "$TMPDIR/cheat_pages"
echo "cheat.sheets: Extracting Pages"
find "$TMPDIR/cheat_pages/sheets/" -maxdepth 1 -type f -exec sh -c \
'printf "[T]%s:\n" "cheat.sheets/$(basename $1)" >> '"$PAGEFILE"' && cat "$1" >> '"$PAGEFILE" sh {} \;
#learnxinyminutes
echo "Cloning: learnxinyminutes"
git clone -q "https://github.com/adambard/learnxinyminutes-docs.git" "$TMPDIR/learn"
echo "learnxinyminutes: Extracting Pages"
find "$TMPDIR/learn/" -maxdepth 1 -name "*.html.markdown" -type f -exec sh -c \
'printf "[T]%s:\n" "learnxinyminutes/learn-$(basename $1 | sed "s/.html.markdown//")" >> '"$PAGEFILE"' && cat "$1" >> '"$PAGEFILE" sh {} \;
#Cleanup, Nuke `tmpdir/`
echo "Cleaning Up"
rm -rf "$TMPDIR"
echo "Update Complete"
printf "%s -> %s\n" "$COUNT" "$(grep -c '^\[T\]' "$PAGEFILE")"
}
while getopts tucp:d: flag; do
case "$flag" in
t) ESC_R=""; ESC_CC=""; ESC_TL="";;
u) update; exit ;;
c) PGER="cat" ;;
p) PAGEFILE="$OPTARG" ;;
d) GETFROM="$OPTARG" ;;
*) echo "cheat, a tool used for printing useful info about commands\n
Usage: cheat [-htuc] [-d SOURCE] [-p PAGE] COMMAND"; exit ;;
esac
done
shift $((OPTIND-1))
: ${1:?Needs Args}
awk "
/^\[T\]/ { j = /$GETFROM\\/$1:$/ }\
j && (\$1 ~ /^#/ || /^\[T\]/) {\
gsub(/^\[T\]/, \"$ESC_TL\")
gsub(/^#/, \"$ESC_CC#\")\
gsub(/$/, \"$ESC_R\")\
} j" "$PAGEFILE" | tr -d '\r' | $PGER
[ "$(stat -c '%Y' "$PAGEFILE" )" -lt "$(date -d '1 week ago' '+%s')" ] && \
printf "It has been over 1 week since you updated your pages, \nrun '%s -u' to update them\n" "$(basename "$0")"