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:
		
							parent
							
								
									4c6aacac50
								
							
						
					
					
						commit
						92c2cfc498
					
				
							
								
								
									
										3
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								main.go
									
									
									
									
									
								
							| @ -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": | ||||
|  | ||||
							
								
								
									
										26
									
								
								modules/hackernews/settings.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								modules/hackernews/settings.go
									
									
									
									
									
										Normal 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 | ||||
| } | ||||
| @ -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() | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user