Finilize version version 0.1.

This commit is contained in:
Kalle Carlbark 2019-08-12 23:25:11 +02:00
parent f1465a26a1
commit a92846a8eb
No known key found for this signature in database
GPG key ID: 3FC0C93C5A5A0670

View file

@ -9,7 +9,10 @@ import (
"log" "log"
// "math" // "math"
"net/http" "net/http"
//"sort"
"os/exec"
"strconv" "strconv"
"sync"
"time" "time"
) )
@ -30,13 +33,11 @@ type HnItem struct {
} }
const apiUrl string = "https://hacker-news.firebaseio.com/v0" const apiUrl string = "https://hacker-news.firebaseio.com/v0"
const numstories int = 30
func (h *HnStories) populateStories(numstories int, done chan bool, widget *widgets.Gauge) { func (h *HnStories) fetchHNStories(item string, wg *sync.WaitGroup) {
var item string defer wg.Done()
var itemUrl string var itemUrl string
var counter int = 1
for _, id := range h.Items[:numstories] {
item = strconv.Itoa(id)
itemUrl = apiUrl + "/item/" + item + ".json" itemUrl = apiUrl + "/item/" + item + ".json"
timeout := time.Duration(5 * time.Second) timeout := time.Duration(5 * time.Second)
client := &http.Client{ client := &http.Client{
@ -55,29 +56,21 @@ func (h *HnStories) populateStories(numstories int, done chan bool, widget *widg
log.Println(err) log.Println(err)
} }
var item HnItem var story HnItem
json.Unmarshal(storydata, &item) json.Unmarshal(storydata, &story)
h.Stories = append(h.Stories, HnItem{ h.Stories = append(h.Stories, HnItem{
By: item.By, By: story.By,
Id: item.Id, Id: story.Id,
Added: item.Added, Added: story.Added,
Title: item.Title, Title: story.Title,
Type: item.Type, Type: story.Type,
Url: item.Url, Url: story.Url,
Kids: item.Kids, Kids: story.Kids,
}) })
counter++
fcounter := float64(counter)
fnumstories := float64(numstories)
percentage := (fcounter / fnumstories) * 100
widget.Percent = int(percentage)
termui.Render(widget)
}
widget.Percent = 100
done <- true
} }
func (h *HnStories) httpHNFetchIds(url string, done chan bool) { func (h *HnStories) httpHNFetchIds(url string, done chan bool) {
timeout := time.Duration(5 * time.Second) timeout := time.Duration(5 * time.Second)
client := &http.Client{ client := &http.Client{
@ -99,7 +92,7 @@ func (h *HnStories) httpHNFetchIds(url string, done chan bool) {
done <- true done <- true
} }
func (h *HnStories) httpHNDisplayStories(numstories int, done chan []string) { func (h *HnStories) httpHNDisplayStories(numstories int) []string {
counter := 1 counter := 1
var termuiitems []string var termuiitems []string
for _, item := range h.Stories[:numstories] { for _, item := range h.Stories[:numstories] {
@ -107,7 +100,7 @@ func (h *HnStories) httpHNDisplayStories(numstories int, done chan []string) {
termuiitems = append(termuiitems, itemstring) termuiitems = append(termuiitems, itemstring)
counter++ counter++
} }
done <- termuiitems return termuiitems
} }
func main() { func main() {
@ -127,26 +120,43 @@ func main() {
g0.BorderStyle.Fg = termui.ColorWhite g0.BorderStyle.Fg = termui.ColorWhite
hnurl := apiUrl + "/topstories.json" hnurl := apiUrl + "/topstories.json"
var a HnStories var a HnStories
var numstories = 10 var counter = 1
var wg sync.WaitGroup
done1 := make(chan bool, 10) done1 := make(chan bool, 10)
done2 := make(chan bool, 10)
g0.Percent = 1 g0.Percent = 1
termui.Render(g0) termui.Render(g0)
go a.httpHNFetchIds(hnurl, done1) go a.httpHNFetchIds(hnurl, done1)
<-done1 <-done1
go a.populateStories(numstories, done2, g0) for _, item := range a.Items[:numstories] {
<-done2 wg.Add(1)
go a.fetchHNStories(strconv.Itoa(item), &wg)
fcounter := float64(counter)
fnumstories := float64(numstories)
percentage := (fcounter / fnumstories) * 100
g0.Percent = int(percentage)
termui.Render(g0)
counter++
}
wg.Wait()
termui.Clear() termui.Clear()
l := widgets.NewList() l := widgets.NewList()
chanitems := make(chan []string, numstories)
var items []string
//l.Title = "[Y] hacker news" //l.Title = "[Y] hacker news"
termuiitems := make(chan []string, numstories)
go a.httpHNDisplayStories(numstories, termuiitems)
l.Border = false l.Border = false
x, y = termui.TerminalDimensions()
l.SetRect(0, 1, x, y) l.SetRect(0, 1, x, y)
l.Rows = <-termuiitems
l.TextStyle = termui.NewStyle(termui.ColorYellow) l.TextStyle = termui.NewStyle(termui.ColorYellow)
l.WrapText = true l.WrapText = true
x, y = termui.TerminalDimensions() //sort.SliceStable(a.Stories, func(i, j int) bool {
// return a.Stories[i].Score > a.Stories[j].Score
//})
go func(items []string) {
chanitems <- a.httpHNDisplayStories(numstories)
}(items)
select {
case items := <-chanitems:
l.Rows = items
renderTab := func() { renderTab := func() {
switch tabpane.ActiveTabIndex { switch tabpane.ActiveTabIndex {
case 0: case 0:
@ -172,6 +182,8 @@ func main() {
renderTab() renderTab()
case "q", "<C-c>": case "q", "<C-c>":
return return
case "r":
case "j", "<Down>": case "j", "<Down>":
l.ScrollDown() l.ScrollDown()
case "k", "<Up>": case "k", "<Up>":
@ -192,6 +204,12 @@ func main() {
l.ScrollTop() l.ScrollTop()
case "G", "<End>": case "G", "<End>":
l.ScrollBottom() l.ScrollBottom()
case "<Enter>":
url := a.Stories[l.SelectedRow].Url
err := exec.Command("open", "-a", "Safari", url).Start()
if err != nil {
log.Fatal(err)
}
case "<Resize>": case "<Resize>":
payload := e.Payload.(termui.Resize) payload := e.Payload.(termui.Resize)
l.SetRect(0, 1, payload.Width, payload.Height) l.SetRect(0, 1, payload.Width, payload.Height)
@ -207,4 +225,5 @@ func main() {
termui.Render(l, tabpane) termui.Render(l, tabpane)
} }
}
} }