-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xkcd
executable file
·77 lines (68 loc) · 1.67 KB
/
xkcd
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
#!/usr/bin/env bash
#
# XKCD
#
# Display a XCKD comic on kitty terminalxkcd-command.png
#
# Requires: - Kitty terminal (https://sw.kovidgoyal.net/kitty/) with icat kitten
# - ImageMagick (https://www.imagemagick.org/),
# - Common utilities found in unix-like systems: curl, grep, cut, perl.
#
# Usage: Run 'xkcd --help' to find out
#
# Author: Rob O'Leary
# Date: 24/02/22
URL=https://xkcd.com/
img=""
title=""
function parse() {
temp_file=$(mktemp)
curl -s -L $URL > "$temp_file"
img="https:$(grep 'img src.* title=".*' "$temp_file" | cut -d '"' -f 2)"
# we need to decode HTML entities e.g " to display text correctly
title="$(grep 'img src.* title=".*' "$temp_file" | cut -d '"' -f 4 | perl -MHTML::Entities -pe 'decode_entities($_);')"
rm "${temp_file}"
}
function show(){
parse
echo ""
kitty +kitten icat --align left "$img"
echo ""
echo "$title"
}
function show_random(){
URL=https://c.xkcd.com/random/comic
show
}
function help() {
fmt_help=" %-20s\t%-54s\n"
echo "Description: Display XKCD comic in the terminal."
echo ""
echo "Usage: xkcd [-l|--latest] [-r|--random] [-h|--help]"
printf "$fmt_help" \
"-h, --help" "Print the help page." \
"-l, --latest" "Show the latest comic." \
"-r, --random" "Show a random comic."
}
case "$#" in
0)
help
;;
1)
case "$1" in
-h | --help)
help
;;
-l | --latest)
show
;;
-r | --random )
show_random
;;
*)
echo "Input error."
exit 1
;;
esac
;;
esac