-
Notifications
You must be signed in to change notification settings - Fork 1
/
aur_talk.sh
executable file
·126 lines (118 loc) · 3.55 KB
/
aur_talk.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
#!/bin/sh -
set -o errexit -o noclobber -o nounset
OPTIONS=an:plw:h
LONGOPTS=all,num-comments:,pinned-only,latest-only,width,help
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") || exit 1
eval set -- "$PARSED"
unset PARSED
print_help () {
printf -- 'Usage: %s [-h] [-a | -n NUM_COMMENTS] [-p | -l] [-w WIDTH | -f] <PACKAGE-NAME>\n' "$0"
printf -- 'Display AUR comments for PACKAGE-NAME.\n\n'
printf -- '-h, --help show this help message and exit\n'
printf -- '-a, --all Fetch all comments.\n'
printf -- '-n NUM_COMMENTS, --num-comments NUM_COMMENTS\n'
printf -- ' Number of comments to fetch. Pinned comments are\n'
printf -- ' always fetched. Default is 10.\n'
printf -- '-p, --pinned-only Display the pinned comments only.\n'
printf -- '-l, --latest-only Display the latest comments only.\n'
printf -- '-w WIDTH, --width WIDTH\n'
printf -- ' Number of columns for formatting comments. Default\n'
printf -- ' is full available width.\n'
}
a=n n=10 p=n l=n w=1024
while true; do
case "$1" in
-a|--all)
a=y
n=1000
shift
;;
-n|--num-comments)
n="$2"
shift 2
;;
-p|--pinned-only)
p=y
shift;
;;
-l|--latest-only)
l=y
shift;
;;
-w|--width)
w="$2"
shift 2;
;;
-h|--help)
print_help
exit
;;
--)
shift
break
;;
*)
exit 2
;;
esac
done
if [ "$#" -lt 1 ]; then
print_help
exit 1
elif [ "$p" = "y" ] && [ "$l" = "y" ]; then
printf 'Only one of -p and -l can be used at the same time.\n'
exit 1
elif [ "$a" = "y" ] && [ "$n" != "1000" ]; then
printf 'Only one of -a and -n can be used at the same time.\n'
exit 1
fi
print_section () {
i=1
while [ $i -le 1000 ]; do
author=$(printf '%s' "$page" | hq "div.comments:nth-child($1) > .comment-header:nth-of-type($i)" text)
[ -z "$author" ] && break || printf '\033[1m%s\033[0m\n' "$author"
comment=$(printf '%s' "$page" | hq "div.comments:nth-child($1) > div.article-content:nth-of-type($((i+1)))" text md)
printf '\033[2m%s\033[0m\n\n' "$comment"
i=$((i+1))
done
}
print_line() {
i=1
while [ $i -le "$1" ]; do
printf '—'
i=$((i+1))
done
}
print_title() {
[ "$1" -eq 1024 ] && lw=80 || lw="$1"
line_width=$(((lw - ${#2}) / 2 - 2))
printf '\033[1m'
print_line $line_width
printf ' %s ' "$2"
print_line $line_width
printf '\033[0m\n\n'
}
[ "$p" = "y" ] && n=1
page=$(curl 2>/dev/null "https://aur.archlinux.org/packages/$1/?O=0&PP=$n")
has_comments=$(printf '%s' "$page" | hq "div.comments:nth-child(7)" data)
if [ "$p" = "n" ] && [ -z "$has_comments" ]; then
printf 'No comments.\n'
exit
fi
is_pinned=$(printf '%s' "$page" | hq "div.comments:nth-child(9)" data)
if [ "$p" = "y" ] || [ "$l" = "n" ]; then
if [ -z "$is_pinned" ]; then
printf 'No pinned comments.\n'
exit
fi
[ "$p" = "n" ] && print_title "$w" "PINNED COMMENTS"
print_section 7 "$w"
fi
if [ "$p" != "y" ] || [ "$l" = "y" ]; then
if [ -n "$is_pinned" ]; then
[ "$l" = "n" ] && print_title "$w" "LATEST COMMENTS"
print_section 9 "$w"
else
print_section 7 "$w"
fi
fi