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

WTF-400 HackerNews extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-14 20:39:39 -07:00
parent 4c6aacac50
commit 92c2cfc498
3 changed files with 35 additions and 6 deletions

View File

@ -229,7 +229,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
settings := gspreadsheets.NewSettingsFromYAML(wtf.Config)
widget = gspreadsheets.NewWidget(app, settings)
case "hackernews":
widget = hackernews.NewWidget(app, pages)
settings := hackernews.NewSettingsFromYAML(wtf.Config)
widget = hackernews.NewWidget(app, pages, settings)
case "ipapi":
widget = ipapi.NewWidget(app)
case "ipinfo":

View File

@ -0,0 +1,26 @@
package hackernews
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type Settings struct {
common *cfg.Common
numberOfStories int
storyType string
}
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.hackernews")
settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
numberOfStories: localConfig.UInt("numberOfStories", 10),
storyType: localConfig.UString("storyType", "top"),
}
return &settings
}

View File

@ -32,12 +32,15 @@ type Widget struct {
stories []Story
selected int
settings *Settings
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget(app, "Hacker News", "hackernews", true),
settings: settings,
}
widget.HelpfulWidget.SetView(widget.View)
@ -57,7 +60,7 @@ func (widget *Widget) Refresh() {
return
}
storyIds, err := GetStories(wtf.Config.UString("wtf.mods.hackernews.storyType", "top"))
storyIds, err := GetStories(widget.settings.storyType)
if storyIds == nil {
return
}
@ -68,8 +71,7 @@ func (widget *Widget) Refresh() {
widget.View.SetText(err.Error())
} else {
var stories []Story
numberOfStoriesToDisplay := wtf.Config.UInt("wtf.mods.hackernews.numberOfStories", 10)
for idx := 0; idx < numberOfStoriesToDisplay; idx++ {
for idx := 0; idx < widget.settings.numberOfStories; idx++ {
story, e := GetStory(storyIds[idx])
if e != nil {
panic(e)
@ -94,7 +96,7 @@ func (widget *Widget) display() {
widget.View.SetWrap(false)
widget.View.Clear()
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - %sstories", widget.Name(), wtf.Config.UString("wtf.mods.hackernews.storyType", "top"))))
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - %sstories", widget.Name(), widget.settings.storyType)))
widget.View.SetText(widget.contentFrom(widget.stories))
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
}