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

WTF-400 GSpreadsheets extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-14 20:31:04 -07:00
parent 6e053fc117
commit 4c6aacac50
4 changed files with 50 additions and 11 deletions

View File

@ -226,7 +226,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
settings := gitter.NewSettingsFromYAML(wtf.Config) settings := gitter.NewSettingsFromYAML(wtf.Config)
widget = gitter.NewWidget(app, pages, settings) widget = gitter.NewWidget(app, pages, settings)
case "gspreadsheets": case "gspreadsheets":
widget = gspreadsheets.NewWidget(app) settings := gspreadsheets.NewSettingsFromYAML(wtf.Config)
widget = gspreadsheets.NewWidget(app, settings)
case "hackernews": case "hackernews":
widget = hackernews.NewWidget(app, pages) widget = hackernews.NewWidget(app, pages)
case "ipapi": case "ipapi":

View File

@ -26,10 +26,10 @@ import (
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func Fetch() ([]*sheets.ValueRange, error) { func (widget *Widget) Fetch() ([]*sheets.ValueRange, error) {
ctx := context.Background() ctx := context.Background()
secretPath, _ := wtf.ExpandHomeDir(wtf.Config.UString("wtf.mods.gspreadsheets.secretFile")) secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile)
b, err := ioutil.ReadFile(secretPath) b, err := ioutil.ReadFile(secretPath)
if err != nil { if err != nil {
@ -51,14 +51,13 @@ func Fetch() ([]*sheets.ValueRange, error) {
return nil, err return nil, err
} }
cells := wtf.ToStrs(wtf.Config.UList("wtf.mods.gspreadsheets.cells.addresses")) cells := wtf.ToStrs(widget.settings.cellAddresses)
documentId := wtf.Config.UString("wtf.mods.gspreadsheets.sheetId")
addresses := strings.Join(cells[:], ";") addresses := strings.Join(cells[:], ";")
responses := make([]*sheets.ValueRange, len(cells)) responses := make([]*sheets.ValueRange, len(cells))
for i := 0; i < len(cells); i++ { for i := 0; i < len(cells); i++ {
resp, err := srv.Spreadsheets.Values.Get(documentId, cells[i]).Do() resp, err := srv.Spreadsheets.Values.Get(widget.settings.sheetID, cells[i]).Do()
if err != nil { if err != nil {
log.Fatalf("Error fetching cells %s", addresses) log.Fatalf("Error fetching cells %s", addresses)
return nil, err return nil, err

View File

@ -0,0 +1,36 @@
package gspreadsheets
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type colors struct {
values string
}
type Settings struct {
colors
common *cfg.Common
cellAddresses []interface{}
cellNames []interface{}
secretFile string
sheetID string
}
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.gspreadsheets")
settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
cellNames: localConfig.UList("cells.names"),
secretFile: localConfig.UString("secretFile"),
sheetID: localConfig.UString("sheetId"),
}
settings.colors.values = localConfig.UString("colors.values", "green")
return &settings
}

View File

@ -10,11 +10,15 @@ import (
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
settings *Settings
} }
func NewWidget(app *tview.Application) *Widget { func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Google Spreadsheets", "gspreadsheets", false), TextWidget: wtf.NewTextWidget(app, "Google Spreadsheets", "gspreadsheets", false),
settings: settings,
} }
return &widget return &widget
@ -23,7 +27,7 @@ func NewWidget(app *tview.Application) *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
cells, _ := Fetch() cells, _ := widget.Fetch()
widget.View.SetText(widget.contentFrom(cells)) widget.View.SetText(widget.contentFrom(cells))
} }
@ -35,12 +39,11 @@ func (widget *Widget) contentFrom(valueRanges []*sheets.ValueRange) string {
return "error 1" return "error 1"
} }
valuesColor := wtf.Config.UString("wtf.mods.gspreadsheets.colors.values", "green")
res := "" res := ""
cells := wtf.ToStrs(wtf.Config.UList("wtf.mods.gspreadsheets.cells.names")) cells := wtf.ToStrs(widget.settings.cellNames)
for i := 0; i < len(valueRanges); i++ { for i := 0; i < len(valueRanges); i++ {
res = res + fmt.Sprintf("%s\t[%s]%s\n", cells[i], valuesColor, valueRanges[i].Values[0][0]) res = res + fmt.Sprintf("%s\t[%s]%s\n", cells[i], widget.settings.colors.values, valueRanges[i].Values[0][0])
} }
return res return res