1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Make hackernews widget scrollable

* Add ability to scroll through the list of stories when they dont fit into the view
* Handle empty results gracefully
* Some code cleanup
This commit is contained in:
Anand Sudhir Prayaga 2018-08-07 18:03:13 +02:00
parent de4e1e38dc
commit 3544eb8736
2 changed files with 19 additions and 12 deletions

View File

@ -12,19 +12,19 @@ import (
) )
func GetStories(storyType string) ([]int, error) { func GetStories(storyType string) ([]int, error) {
var stories []int var storyIds []int
switch strings.ToLower(storyType) { switch strings.ToLower(storyType) {
case "new", "top", "job", "ask": case "new", "top", "job", "ask":
resp, err := apiRequest(storyType + "stories") resp, err := apiRequest(storyType + "stories")
if err != nil { if err != nil {
return stories, err return storyIds, err
} }
parseJson(&stories, resp.Body) parseJson(&storyIds, resp.Body)
} }
return stories, nil return storyIds, nil
} }
func GetStory(id int) (Story, error) { func GetStory(id int) (Story, error) {

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/logger"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
"net/url" "net/url"
"strconv"
"strings" "strings"
) )
@ -41,6 +41,8 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget.HelpfulWidget.SetView(widget.View) widget.HelpfulWidget.SetView(widget.View)
widget.unselect() widget.unselect()
widget.View.SetScrollable(true)
widget.View.SetRegions(true)
widget.View.SetInputCapture(widget.keyboardIntercept) widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget return &widget
@ -54,8 +56,9 @@ func (widget *Widget) Refresh() {
} }
storyIds, err := GetStories(wtf.Config.UString("wtf.mods.hackernews.storyType", "top")) storyIds, err := GetStories(wtf.Config.UString("wtf.mods.hackernews.storyType", "top"))
if storyIds == nil {
widget.UpdateRefreshedAt() return
}
if err != nil { if err != nil {
widget.View.SetWrap(true) widget.View.SetWrap(true)
@ -64,10 +67,8 @@ func (widget *Widget) Refresh() {
} else { } else {
var stories []Story var stories []Story
numberOfStoriesToDisplay := wtf.Config.UInt("wtf.mods.hackernews.numberOfStories", 10) numberOfStoriesToDisplay := wtf.Config.UInt("wtf.mods.hackernews.numberOfStories", 10)
for idx := 0; idx <= numberOfStoriesToDisplay; idx++ { for idx := 0; idx < numberOfStoriesToDisplay; idx++ {
story, e := GetStory(storyIds[idx]) story, e := GetStory(storyIds[idx])
logger.Log(fmt.Sprintf("stories %s", story.Title))
if e != nil { if e != nil {
panic(e) panic(e)
} else { } else {
@ -78,6 +79,7 @@ func (widget *Widget) Refresh() {
widget.stories = stories widget.stories = stories
} }
widget.UpdateRefreshedAt()
widget.display() widget.display()
} }
@ -90,8 +92,10 @@ func (widget *Widget) display() {
widget.View.SetWrap(false) widget.View.SetWrap(false)
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - Stories", widget.Name))) widget.View.Clear()
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - %sstories", widget.Name, wtf.Config.UString("wtf.mods.hackernews.storyType", "top"))))
widget.View.SetText(widget.contentFrom(widget.stories)) widget.View.SetText(widget.contentFrom(widget.stories))
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
} }
func (widget *Widget) contentFrom(stories []Story) string { func (widget *Widget) contentFrom(stories []Story) string {
@ -99,13 +103,16 @@ func (widget *Widget) contentFrom(stories []Story) string {
for idx, story := range stories { for idx, story := range stories {
u, _ := url.Parse(story.URL) u, _ := url.Parse(story.URL)
str = str + fmt.Sprintf( str = str + fmt.Sprintf(
"[%s] [yellow]%d. [%s]%s [blue](%s)\n", `["%d"][""][%s] [yellow]%d. [%s]%s [blue](%s)`,
idx,
widget.rowColor(idx), widget.rowColor(idx),
idx+1, idx+1,
widget.rowColor(idx), widget.rowColor(idx),
story.Title, story.Title,
strings.TrimPrefix(u.Host, "www."), strings.TrimPrefix(u.Host, "www."),
) )
str = str + "\n"
} }
return str return str