-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemoticon
executable file
·77 lines (67 loc) · 1.41 KB
/
emoticon
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
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
set -o noclobber
EMOTICONS_DIR="$HOME/.local/share/emoticons"
usage() {
echo "Usage: $0 [--help] [--list] [EMOTICON_NAME]"
echo
echo " --help, -h Show this help and exit"
echo " --list, -l List available emoticons"
}
list_emoticons() {
for file in "$EMOTICONS_DIR"/*; do
file="$(basename -- "$file")"
echo "${file%.*}"
done
}
random_emoticon() {
local path
path="$(find "$EMOTICONS_DIR/" -type f | shuf -n 1)"
copy_file "$path"
}
grab_emoticon_by_name() {
local name="$1"
local path="$EMOTICONS_DIR/$name.txt"
if [[ -f "$path" ]]; then
copy_file "$path"
else
echo "$path does not exist" >&2
exit 1
fi
}
copy_file() {
local path="$1"
tee /dev/stderr <"$path" | perl -p -e 'chomp if eof' | clip
}
main() {
local emoticon=""
while [[ $# -gt 0 ]]; do
case "$1" in
--help | -h)
usage
exit 0
;;
--list | -l)
list_emoticons
exit 0
;;
-*)
error "Unexpected option: $1"
usage >&2
exit 1
;;
*)
emoticon="$1"
;;
esac
shift
done
if [[ -z "$emoticon" ]]; then
random_emoticon
else
grab_emoticon_by_name "$emoticon"
fi
}
main "$@"