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

Add capability to switch content between title, link and title+content

This commit is contained in:
Ying Fan Chong
2020-10-01 22:12:31 +08:00
parent e56b8693a2
commit c4ca3d9b2a
4 changed files with 52 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ func (widget *Widget) initializeKeyboardControls() {
widget.SetKeyboardChar("j", widget.Next, "Select next item")
widget.SetKeyboardChar("k", widget.Prev, "Select previous item")
widget.SetKeyboardChar("o", widget.openStory, "Open story in browser")
widget.SetKeyboardChar("t", widget.toggleDisplayText, "Toggle between title and URL")
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")

View File

@@ -3,13 +3,23 @@ package feedreader
import (
"fmt"
"sort"
"strings"
"jaytaylor.com/html2text"
"github.com/mmcdole/gofeed"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
type ShowType int
const (
SHOW_TITLE ShowType = iota
SHOW_LINK
SHOW_CONTENT
)
// FeedItem represents an item returned from an RSS or Atom feed
type FeedItem struct {
item *gofeed.Item
@@ -25,6 +35,32 @@ type Widget struct {
parser *gofeed.Parser
settings *Settings
err error
showType ShowType
}
func rotateShowType(showtype ShowType) ShowType {
returnValue := SHOW_TITLE
switch showtype {
case SHOW_TITLE:
returnValue = SHOW_LINK
case SHOW_LINK:
returnValue = SHOW_CONTENT
case SHOW_CONTENT:
returnValue = SHOW_TITLE
}
return returnValue
}
func getShowText(feedItem *FeedItem, showType ShowType) string {
returnValue := feedItem.item.Title
switch showType {
case SHOW_LINK:
returnValue = feedItem.item.Link
case SHOW_CONTENT:
text, _ := html2text.FromString(feedItem.item.Content, html2text.Options{PrettyTables: true})
returnValue = feedItem.item.Title + "\n" + strings.TrimSpace(text)
}
return returnValue
}
// NewWidget creates a new instance of a widget
@@ -35,6 +71,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
parser: gofeed.NewParser(),
settings: settings,
showType: SHOW_TITLE,
}
widget.SetRenderFunction(widget.Render)
@@ -137,11 +174,13 @@ func (widget *Widget) content() (string, string, bool) {
}
}
displayText := getShowText(feedItem, widget.showType)
row := fmt.Sprintf(
"[%s]%2d. %s[white]",
rowColor,
idx+1,
feedItem.item.Title,
displayText,
)
str += utils.HighlightableHelper(widget.View, row, idx, len(feedItem.item.Title))
@@ -169,3 +208,8 @@ func (widget *Widget) openStory() {
utils.OpenFile(story.item.Link)
}
}
func (widget *Widget) toggleDisplayText() {
widget.showType = rotateShowType(widget.showType)
widget.Render()
}