Skip to content

Commit

Permalink
Merge branch 'anandsudhir-scrollable-hackernews'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Cummer authored and Chris Cummer committed Aug 12, 2018
2 parents 37befca + 6691ec0 commit b1f3b70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions hackernews/client.go
Expand Up @@ -12,19 +12,19 @@ import (
)

func GetStories(storyType string) ([]int, error) {
var stories []int
var storyIds []int

switch strings.ToLower(storyType) {
case "new", "top", "job", "ask":
resp, err := apiRequest(storyType + "stories")
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) {
Expand Down
23 changes: 15 additions & 8 deletions hackernews/widget.go
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/logger"
"github.com/senorprogrammer/wtf/wtf"
"net/url"
"strconv"
"strings"
)

Expand Down Expand Up @@ -41,6 +41,8 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget.HelpfulWidget.SetView(widget.View)
widget.unselect()

widget.View.SetScrollable(true)
widget.View.SetRegions(true)
widget.View.SetInputCapture(widget.keyboardIntercept)

return &widget
Expand All @@ -54,8 +56,9 @@ func (widget *Widget) Refresh() {
}

storyIds, err := GetStories(wtf.Config.UString("wtf.mods.hackernews.storyType", "top"))

widget.UpdateRefreshedAt()
if storyIds == nil {
return
}

if err != nil {
widget.View.SetWrap(true)
Expand All @@ -64,10 +67,8 @@ func (widget *Widget) Refresh() {
} else {
var stories []Story
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])
logger.Log(fmt.Sprintf("stories %s", story.Title))

if e != nil {
panic(e)
} else {
Expand All @@ -78,6 +79,7 @@ func (widget *Widget) Refresh() {
widget.stories = stories
}

widget.UpdateRefreshedAt()
widget.display()
}

Expand All @@ -90,22 +92,27 @@ func (widget *Widget) display() {

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.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
}

func (widget *Widget) contentFrom(stories []Story) string {
var str string
for idx, story := range stories {
u, _ := url.Parse(story.URL)
str = str + fmt.Sprintf(
"[%s] [yellow]%d. [%s]%s [blue](%s)\n",
`["%d"][""][%s] [yellow]%d. [%s]%s [blue](%s)`,
idx,
widget.rowColor(idx),
idx+1,
widget.rowColor(idx),
story.Title,
strings.TrimPrefix(u.Host, "www."),
)

str = str + "\n"
}

return str
Expand Down

0 comments on commit b1f3b70

Please sign in to comment.