1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/subreddit/widget.go
Chris Cummer fd794707cd
☢️ WTF-1031 Support multiple simultaneous configurations (#1032)
* WTF-1031 Rename WtfApp.app to WtfApp.tviewApp

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 Add scaffolding for main to support multiple WtfApp instances

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 WIP

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove common functionality from KeyboardWidget and into Base

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Augment with some descriptive comments

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Add full support for multiple app instances via the AppManager.

Still to do:

* Config support for multiple apps/multiple config files
* The ability to switch between apps

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Move SetTerminal out of main and into its own file

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-12-21 03:25:41 -08:00

97 lines
2.2 KiB
Go

package subreddit
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.ScrollableWidget
settings *Settings
err error
links []Link
}
func NewWidget(tviewApp *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := &Widget{
ScrollableWidget: view.NewScrollableWidget(tviewApp, pages, settings.Common),
settings: settings,
}
widget.SetRenderFunction(widget.Render)
widget.initializeKeyboardControls()
return widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
links, err := GetLinks(widget.settings.subreddit, widget.settings.sortOrder, widget.settings.topTimePeriod)
if err != nil {
widget.err = err
widget.links = nil
widget.SetItemCount(0)
} else {
if len(links) <= widget.settings.numberOfPosts {
widget.links = links
widget.SetItemCount(len(widget.links))
widget.err = nil
} else {
widget.links = links[:widget.settings.numberOfPosts]
widget.SetItemCount(len(widget.links))
widget.err = nil
}
}
widget.Render()
}
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
title := "/r/" + widget.settings.subreddit + " - " + widget.settings.sortOrder
if widget.err != nil {
return title, widget.err.Error(), true
}
var content string
for idx, link := range widget.links {
row := fmt.Sprintf(
`[%s]%2d. %s`,
widget.RowColor(idx),
idx+1,
tview.Escape(link.Title),
)
content += utils.HighlightableHelper(widget.View, row, idx, len(link.Title))
}
return title, content, false
}
func (widget *Widget) openLink() {
sel := widget.GetSelected()
if sel >= 0 && widget.links != nil && sel < len(widget.links) {
story := &widget.links[sel]
utils.OpenFile(story.ItemURL)
}
}
func (widget *Widget) openReddit() {
sel := widget.GetSelected()
if sel >= 0 && widget.links != nil && sel < len(widget.links) {
story := &widget.links[sel]
fullLink := "http://reddit.com" + story.Permalink
utils.OpenFile(fullLink)
}
}