-
Notifications
You must be signed in to change notification settings - Fork 12
/
hn
executable file
·109 lines (93 loc) · 2.38 KB
/
hn
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
#!/usr/bin/env sh
help()
{
cat <<EOF
NAME:
hn -- Hacker News without leaving your terminal.
SYNOPSIS:
hn [-c] [-h] [-l LIMIT]
OPTIONS:
-c Colorize the output.
-l Limit to LIMIT articles.
-h prints a summary of the help options.
EXAMPLES:
hn
GoDaddy supports SOPA, redditor proposes "Move your Domain Day"
http://www.reddit.com/r/politics/comments/nmnie/godaddy_supports_sopa_im_transferring_51_domains/
StackOverflow also planning to switch from GoDaddy due to SOPA concerns.
http://meta.stackoverflow.com/q/116891/1588
GoDaddy has not withdrawn its official congressional support for SOPA
http://www.reddit.com/r/technology/comments/npair/godaddy_has_not_withdrawn_its_official/
...
hn -l 1
GoDaddy supports SOPA, redditor proposes "Move your Domain Day"
http://www.reddit.com/r/politics/comments/nmnie/godaddy_supports_sopa_im_transferring_51_domains/
EOF
}
hackernews()
{
# Set the default limit to 90 (3 lines * 30 articles) lines or use 3 times
# the given value.
if [ "$limit" = "" ]
then
limit=90
else
limit=$((3 * $limit))
fi
# GNU sed uses -r for expanded regular expressions but OS X does not use GNU
# sed. Instead it relies on -E argument.
sed_attributes="-re"
if [ "`uname`" = "Darwin" ]
then
sed_attributes="-Ee"
fi
attributes="title|link"
curl -s "https://news.ycombinator.com/rss" |
grep -Eio "<item>.*</item>" |
grep -Eio "<($attributes)>[^<>]*</($attributes)>" |
sed $sed_attributes "s/<title>([^<>]*)<\/title>/$highlight_title\1/" \
-e "s/<link>([^<>]*)<\/link>/$highlight_link\1★/" \
-e "s///\//g" \
-e "s/'/'/g" |
tr "★" "\n" |
head -n $limit
printf "\033[0m"
}
set_color()
{
highlight_title=`echo "\033[0;1;38;5;202m"`
if [ "$highlight_title" = "\033[0;1;38;5;202m" ]
then
highlight_title=""
fi
highlight_link=`echo "\033[0;38;5;68m"`
if [ "$highlight_link" = "\033[0;38;5;68m" ]
then
highlight_link=""
fi
}
while getopts "chl:" OPTION
do
case "$OPTION" in
h)
help
exit 1;
;;
c)
set_color
;;
l)
limit=$OPTARG
if ! [ $limit -ge 1 ] || ! [ $limit -le 30 ]
then
echo "hackernews: valid range is [1-30] for -- l"
exit 1;
fi
;;
?)
help
exit 1;
;;
esac
done
hackernews