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

2
go.mod
View File

@ -56,6 +56,7 @@ require (
github.com/spf13/cobra v0.0.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.6.1 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/stretchr/testify v1.6.1
github.com/wtfutil/spotigopher v0.0.0-20191127141047-7d8168fe103a
github.com/wtfutil/todoist v0.0.2-0.20191216004217-0ec29ceda61a
@ -69,6 +70,7 @@ require (
gopkg.in/jarcoal/httpmock.v1 v1.0.0-20181110093347-3be5f16b70eb // indirect
gopkg.in/yaml.v2 v2.3.0
gotest.tools v2.2.0+incompatible
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7
k8s.io/apimachinery v0.0.0-20190223094358-dcb391cde5ca
k8s.io/client-go v10.0.0+incompatible
)

4
go.sum
View File

@ -707,6 +707,8 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.6.1 h1:VPZzIkznI1YhVMRi6vNFLHSwhnhReBfgTxIPccpfdZk=
github.com/spf13/viper v1.6.1/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
github.com/streadway/amqp v0.0.0-20180528204448-e5adc2ada8b8 h1:l6epF6yBwuejBfhGkM5m8VSNM/QAm7ApGyH35ehA7eQ=
github.com/streadway/amqp v0.0.0-20180528204448-e5adc2ada8b8/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@ -1128,6 +1130,8 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt
honnef.co/go/tools v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7 h1:mub0MmFLOn8XLikZOAhgLD1kXJq8jgftSrrv7m00xFo=
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7/go.mod h1:OxvTsCwKosqQ1q7B+8FwXqg4rKZ/UG9dUW+g/VL2xH4=
k8s.io/api v0.0.0-20181204000039-89a74a8d264d h1:HQoGWsWUe/FmRcX9BU440AAMnzBFEf+DBo4nbkQlNzs=
k8s.io/api v0.0.0-20181204000039-89a74a8d264d/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA=
k8s.io/apimachinery v0.0.0-20190223094358-dcb391cde5ca h1:rOczZwIfERvL7YLSQ96uI3NUYamzdK/MmSDIfChQRBk=

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()
}