forked from gedex/gohn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (124 loc) · 2.72 KB
/
main.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/nsf/termbox-go"
"strings"
"time"
)
const (
HNURL = "https://news.ycombinator.com"
mainTitle = "Hacker News"
footer = "Press ESC to exit"
maxItems = 10
)
type item struct {
title, url, points string
}
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
event_queue := make(chan termbox.Event)
go func() {
for {
event_queue <- termbox.PollEvent()
}
}()
showLatest()
loop:
for {
select {
case ev := <-event_queue:
switch ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc {
break loop
}
case termbox.EventResize:
showLatest()
}
case <-time.After(time.Second * 5):
showLatest()
}
}
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
}
func showLatest() {
var doc *goquery.Document
var e error
if doc, e = goquery.NewDocument(HNURL); e != nil {
panic(e.Error())
}
items, maxWidth := getItems(doc)
print(items, maxWidth)
}
func getItems(doc *goquery.Document) (items []item, maxWidth int) {
doc.Find("td.title a").EachWithBreak(func(i int, s *goquery.Selection) bool {
if i == maxItems {
return false
}
if s.Text() == "More" {
return true
}
href, _ := s.Attr("href")
title := s.Text()
points := s.Parent().Parent().Next().Find("span").Text()
a, b := len(fmt.Sprintf("%s (%s)", title, points)), len(href)
maxWidth = max(a, b, maxWidth)
items = append(items, item{
title: title,
url: href,
points: points,
})
return true
})
return
}
func print(items []item, maxWidth int) {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
// Print header
printLine(mainTitle, 0, maxWidth)
y := 1
// Items from HN
for _, i := range items {
lenTop := len(fmt.Sprintf("%s(%s)", i.title, i.points))
strTop := fmt.Sprintf("%s%s(%s)", i.title, strings.Repeat(" ", maxWidth-lenTop), i.points)
strURL := fmt.Sprintf("%s%s", i.url, strings.Repeat(" ", maxWidth-len(i.url)))
// Print title and points
for x := 0; x < maxWidth; x++ {
termbox.SetCell(x, y, rune(strTop[x]), termbox.ColorBlack, termbox.ColorWhite)
}
y++
// Print URL
for x := 0; x < maxWidth; x++ {
termbox.SetCell(x, y, rune(strURL[x]), termbox.ColorBlue|termbox.AttrBold, termbox.ColorWhite)
}
y++
}
// Print footer
printLine(footer, y, maxWidth)
termbox.Flush()
}
func printLine(s string, y, maxWidth int) {
var ch rune
l := len(s)
for x := 0; x < maxWidth; x++ {
if x < l {
ch = rune(s[x])
} else {
ch = ' '
}
termbox.SetCell(x, y, ch, termbox.ColorBlack, termbox.ColorMagenta)
}
}
func max(a, b, c int) int {
if a >= b && a >= c {
return a
} else if b >= a && b >= c {
return b
}
return c
}