1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/trello/widget.go
Chris Cummer f650bb1698 Remove the app.Draw() gorountine
Prefer to have widgets force a draw when their data changes. This should
reduce draws (unless the user has a module installed that updates >=
1/sec, the old draw default).

This should also remove a source of some of the race conditions that
users were experiencing (though not all, there are still many).
2018-09-24 14:40:57 -07:00

104 lines
2.1 KiB
Go

package trello
import (
"fmt"
"os"
"github.com/adlio/trello"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
}
func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Trello", "trello", false),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
client := trello.NewClient(
widget.apiKey(),
widget.accessToken(),
)
// Get the cards
searchResult, err := GetCards(client, getLists())
widget.UpdateRefreshedAt()
var content string
if err != nil {
widget.View.SetWrap(true)
widget.View.SetTitle(widget.Name)
content = err.Error()
} else {
widget.View.SetWrap(false)
widget.View.SetTitle(
fmt.Sprintf(
"[white]%s: [green]%s ",
widget.Name,
wtf.Config.UString("wtf.mods.trello.board"),
),
)
content = widget.contentFrom(searchResult)
}
widget.View.SetText(content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) accessToken() string {
return wtf.Config.UString(
"wtf.mods.trello.accessToken",
os.Getenv("WTF_TRELLO_ACCESS_TOKEN"),
)
}
func (widget *Widget) apiKey() string {
return wtf.Config.UString(
"wtf.mods.trello.apiKey",
os.Getenv("WTF_TRELLO_APP_KEY"),
)
}
func (widget *Widget) contentFrom(searchResult *SearchResult) string {
str := ""
for list, cardArray := range searchResult.TrelloCards {
str = fmt.Sprintf("%s [red]Cards in %s[white]\n", str, list)
for _, card := range cardArray {
str = fmt.Sprintf("%s [green]%s[white]\n", str, card.Name)
}
str = fmt.Sprintf("%s\n", str)
}
return str
}
func getLists() map[string]string {
list := make(map[string]string)
// see if project is set to a single string
configPath := "wtf.mods.trello.list"
singleList, err := wtf.Config.String(configPath)
if err == nil {
list[singleList] = ""
return list
}
// else, assume list
multiList := wtf.Config.UList(configPath)
for _, proj := range multiList {
if str, ok := proj.(string); ok {
list[str] = ""
}
}
return list
}