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) | 		settings := gspreadsheets.NewSettingsFromYAML(wtf.Config) | ||||||
| 		widget = gspreadsheets.NewWidget(app, settings) | 		widget = gspreadsheets.NewWidget(app, settings) | ||||||
| 	case "hackernews": | 	case "hackernews": | ||||||
| 		widget = hackernews.NewWidget(app, pages) | 		settings := hackernews.NewSettingsFromYAML(wtf.Config) | ||||||
|  | 		widget = hackernews.NewWidget(app, pages, settings) | ||||||
| 	case "ipapi": | 	case "ipapi": | ||||||
| 		widget = ipapi.NewWidget(app) | 		widget = ipapi.NewWidget(app) | ||||||
| 	case "ipinfo": | 	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 | 	stories  []Story | ||||||
| 	selected int | 	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{ | 	widget := Widget{ | ||||||
| 		HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), | 		HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), | ||||||
| 		TextWidget:    wtf.NewTextWidget(app, "Hacker News", "hackernews", true), | 		TextWidget:    wtf.NewTextWidget(app, "Hacker News", "hackernews", true), | ||||||
|  | 
 | ||||||
|  | 		settings: settings, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	widget.HelpfulWidget.SetView(widget.View) | 	widget.HelpfulWidget.SetView(widget.View) | ||||||
| @ -57,7 +60,7 @@ func (widget *Widget) Refresh() { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	storyIds, err := GetStories(wtf.Config.UString("wtf.mods.hackernews.storyType", "top")) | 	storyIds, err := GetStories(widget.settings.storyType) | ||||||
| 	if storyIds == nil { | 	if storyIds == nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| @ -68,8 +71,7 @@ func (widget *Widget) Refresh() { | |||||||
| 		widget.View.SetText(err.Error()) | 		widget.View.SetText(err.Error()) | ||||||
| 	} else { | 	} else { | ||||||
| 		var stories []Story | 		var stories []Story | ||||||
| 		numberOfStoriesToDisplay := wtf.Config.UInt("wtf.mods.hackernews.numberOfStories", 10) | 		for idx := 0; idx < widget.settings.numberOfStories; idx++ { | ||||||
| 		for idx := 0; idx < numberOfStoriesToDisplay; idx++ { |  | ||||||
| 			story, e := GetStory(storyIds[idx]) | 			story, e := GetStory(storyIds[idx]) | ||||||
| 			if e != nil { | 			if e != nil { | ||||||
| 				panic(e) | 				panic(e) | ||||||
| @ -94,7 +96,7 @@ func (widget *Widget) display() { | |||||||
| 	widget.View.SetWrap(false) | 	widget.View.SetWrap(false) | ||||||
| 
 | 
 | ||||||
| 	widget.View.Clear() | 	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.SetText(widget.contentFrom(widget.stories)) | ||||||
| 	widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight() | 	widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight() | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user