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

Merge branch 'add-devto-modules' of github.com:VictorAvelar/wtf into VictorAvelar-add-devto-modules

This commit is contained in:
Chris Cummer
2019-09-22 17:44:07 -07:00
50 changed files with 611 additions and 11710 deletions

16
modules/devto/keyboard.go Normal file
View File

@@ -0,0 +1,16 @@
package devto
import "github.com/gdamore/tcell"
func (widget *Widget) initializeKeyboardControls() {
widget.InitializeCommonControls(widget.Refresh)
widget.SetKeyboardChar("d", widget.Next, "Select next item")
widget.SetKeyboardChar("a", widget.Prev, "Select previous item")
widget.SetKeyboardChar("o", widget.openStory, "Open story in browser")
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")
widget.SetKeyboardKey(tcell.KeyEnter, widget.openStory, "Open story in browser")
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
}

32
modules/devto/settings.go Normal file
View File

@@ -0,0 +1,32 @@
package devto
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
const defaultTitle = "dev.to | News Feed"
// Settings defines the configuration options for this module
type Settings struct {
common *cfg.Common
numberOfArticles int `help:"Number of stories to show. Default is 10" optional:"true"`
contentTag string `help:"List articles from a specific tag. Default is empty" optional:"true"`
contentUsername string `help:"List articles from a specific user. Default is empty" optional:"true"`
contentState string `help:"Order the feed by fresh/rising. Default is rising" optional:"true"`
}
// NewSettingsFromYAML creates and returns an instance of Settings with configuration options populated
func NewSettingsFromYAML(name string, yamlConfig *config.Config, globalConfig *config.Config) *Settings {
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, yamlConfig, globalConfig),
numberOfArticles: yamlConfig.UInt("numberOfArticles", 10),
contentTag: yamlConfig.UString("contentTag", ""),
contentUsername: yamlConfig.UString("contentUsername", ""),
contentState: yamlConfig.UString("contentState", ""),
}
return &settings
}

123
modules/devto/widget.go Normal file
View File

@@ -0,0 +1,123 @@
package devto
import (
"context"
"fmt"
"github.com/VictorAvelar/devto-api-go/devto"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.KeyboardWidget
view.ScrollableWidget
articles []devto.Article
settings *Settings
err error
}
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := &Widget{
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
ScrollableWidget: view.NewScrollableWidget(app, settings.common, true),
settings: settings,
}
widget.SetRenderFunction(widget.Render)
widget.View.SetScrollable(true)
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.KeyboardWidget.SetView(widget.View)
return widget
}
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
ctx := context.Background()
wCfg, _ := devto.NewConfig(false, "")
c, _ := devto.NewClient(ctx, wCfg, nil, devto.BaseURL)
options := devto.ArticleListOptions{
Tags: widget.settings.contentTag,
Username: widget.settings.contentUsername,
State: widget.settings.contentState,
}
articles, err := c.Articles.List(ctx, options)
if err != nil {
widget.err = err
widget.articles = nil
widget.SetItemCount(0)
} else {
var displayArticles []devto.Article
var l int
if len(articles) < widget.settings.numberOfArticles {
l = len(articles)
} else {
l = widget.settings.numberOfArticles - 1
}
for i, art := range articles {
if i > l {
break
}
displayArticles = append(displayArticles, art)
}
widget.articles = displayArticles
widget.SetItemCount(len(displayArticles))
}
widget.Render()
}
// Render sets up the widget data for redrawing to the screen
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
title := fmt.Sprintf("%s - %s stories", widget.CommonSettings().Title, widget.settings.contentTag)
if widget.err != nil {
return title, widget.err.Error(), true
}
articles := widget.articles
if articles == nil || len(articles) == 0 {
return title, "No stories to display", false
}
var str string
for idx, article := range articles {
row := fmt.Sprintf(
`[%s]%2d. %s [lightblue](%s)[white]`,
widget.RowColor(idx),
idx+1,
article.Title,
article.User.Username,
)
str += utils.HighlightableHelper(widget.View, row, idx, len(article.Title))
}
return title, str, false
}
func (widget *Widget) openStory() {
sel := widget.GetSelected()
if sel >= 0 && widget.articles != nil && sel < len(widget.articles) {
article := &widget.articles[sel]
utils.OpenFile(article.URL.String())
}
}