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

WTF-400 GCal extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-14 12:36:38 -07:00
parent 5e1fdef5d4
commit 12a895b9df
5 changed files with 94 additions and 35 deletions

View File

@ -208,7 +208,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
cfg := datadog.NewSettingsFromYAML(wtf.Config)
widget = datadog.NewWidget(app, cfg)
case "gcal":
widget = gcal.NewWidget(app)
cfg := gcal.NewSettingsFromYAML(wtf.Config)
widget = gcal.NewWidget(app, cfg)
case "gerrit":
widget = gerrit.NewWidget(app, pages)
case "git":

View File

@ -27,10 +27,10 @@ import (
/* -------------------- Exported Functions -------------------- */
func Fetch() ([]*CalEvent, error) {
func (widget *Widget) Fetch() ([]*CalEvent, error) {
ctx := context.Background()
secretPath, _ := wtf.ExpandHomeDir(wtf.Config.UString("wtf.mods.gcal.secretFile"))
secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile)
b, err := ioutil.ReadFile(secretPath)
if err != nil {
@ -48,13 +48,13 @@ func Fetch() ([]*CalEvent, error) {
return nil, err
}
calendarIds, err := getCalendarIdList(srv)
calendarIds, err := widget.getCalendarIdList(srv)
// Get calendar events
var events calendar.Events
startTime := fromMidnight().Format(time.RFC3339)
eventLimit := int64(wtf.Config.UInt("wtf.mods.gcal.eventCount", 10))
eventLimit := int64(widget.settings.eventCount)
for _, calendarId := range calendarIds {
calendarEvents, err := srv.Events.List(calendarId).ShowDeleted(false).TimeMin(startTime).MaxResults(eventLimit).SingleEvents(true).OrderBy("startTime").Do()
@ -122,13 +122,12 @@ func isAuthenticated() bool {
return err == nil
}
func authenticate() {
filename := wtf.Config.UString("wtf.mods.gcal.secretFile")
secretPath, _ := wtf.ExpandHomeDir(filename)
func (widget *Widget) authenticate() {
secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile)
b, err := ioutil.ReadFile(secretPath)
if err != nil {
log.Fatalf("Unable to read secret file. %v", filename)
log.Fatalf("Unable to read secret file. %v", widget.settings.secretFile)
}
config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
@ -188,9 +187,9 @@ func saveToken(file string, token *oauth2.Token) {
json.NewEncoder(f).Encode(token)
}
func getCalendarIdList(srv *calendar.Service) ([]string, error) {
func (widget *Widget) getCalendarIdList(srv *calendar.Service) ([]string, error) {
// Return single calendar if settings specify we should
if !wtf.Config.UBool("wtf.mods.gcal.multiCalendar", false) {
if !widget.settings.multiCalendar {
id, err := srv.CalendarList.Get("primary").Do()
if err != nil {
return nil, err

View File

@ -44,8 +44,8 @@ func (widget *Widget) contentFrom(calEvents []*CalEvent) string {
var str string
var prevEvent *CalEvent
if !wtf.Config.UBool("wtf.mods.gcal.showDeclined", false) {
calEvents = removeDeclined(calEvents)
if !widget.settings.showDeclined {
calEvents = widget.removeDeclined(calEvents)
}
for _, calEvent := range calEvents {
@ -101,7 +101,7 @@ func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string {
if !eventStartDay.Equal(prevStartDay) {
return fmt.Sprintf("[%s::b]",
wtf.Config.UString("wtf.mods.gcal.colors.day", "forestgreen")) +
widget.settings.colors.day) +
event.Start().Format(wtf.FullDateFormat) +
"\n"
}
@ -111,10 +111,10 @@ func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string {
func (widget *Widget) descriptionColor(calEvent *CalEvent) string {
if calEvent.Past() {
return wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
return widget.settings.colors.past
}
return wtf.Config.UString("wtf.mods.gcal.colors.description", "white")
return widget.settings.colors.description
}
func (widget *Widget) eventSummary(calEvent *CalEvent, conflict bool) string {
@ -123,13 +123,13 @@ func (widget *Widget) eventSummary(calEvent *CalEvent, conflict bool) string {
if calEvent.Now() {
summary = fmt.Sprintf(
"%s %s",
wtf.Config.UString("wtf.mods.gcal.currentIcon", "🔸"),
widget.settings.currentIcon,
summary,
)
}
if conflict {
return fmt.Sprintf("%s %s", wtf.Config.UString("wtf.mods.gcal.conflictIcon", "🚨"), summary)
return fmt.Sprintf("%s %s", widget.settings.conflictIcon, summary)
}
return summary
@ -170,9 +170,9 @@ func (widget *Widget) timeUntil(calEvent *CalEvent) string {
}
func (widget *Widget) titleColor(calEvent *CalEvent) string {
color := wtf.Config.UString("wtf.mods.gcal.colors.title", "white")
color := widget.settings.colors.title
for _, untypedArr := range wtf.Config.UList("wtf.mods.gcal.colors.highlights") {
for _, untypedArr := range widget.settings.highlights {
highlightElements := wtf.ToStrs(untypedArr.([]interface{}))
match, _ := regexp.MatchString(
@ -186,14 +186,14 @@ func (widget *Widget) titleColor(calEvent *CalEvent) string {
}
if calEvent.Past() {
color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
color = widget.settings.colors.past
}
return color
}
func (widget *Widget) location(calEvent *CalEvent) string {
if wtf.Config.UBool("wtf.mods.gcal.displayLocation", true) == false {
if widget.settings.withLocation == false {
return ""
}
@ -209,13 +209,13 @@ func (widget *Widget) location(calEvent *CalEvent) string {
}
func (widget *Widget) responseIcon(calEvent *CalEvent) string {
if false == wtf.Config.UBool("wtf.mods.gcal.displayResponseStatus", true) {
if widget.settings.displayResponseStatus == false {
return ""
}
icon := "[gray]"
switch calEvent.ResponseFor(wtf.Config.UString("wtf.mods.gcal.email")) {
switch calEvent.ResponseFor(widget.settings.email) {
case "accepted":
return icon + "✔︎"
case "declined":
@ -229,10 +229,10 @@ func (widget *Widget) responseIcon(calEvent *CalEvent) string {
}
}
func removeDeclined(events []*CalEvent) []*CalEvent {
func (widget *Widget) removeDeclined(events []*CalEvent) []*CalEvent {
var ret []*CalEvent
for _, e := range events {
if e.ResponseFor(wtf.Config.UString("wtf.mods.gcal.email")) != "declined" {
if e.ResponseFor(widget.settings.email) != "declined" {
ret = append(ret, e)
}
}

57
modules/gcal/settings.go Normal file
View File

@ -0,0 +1,57 @@
package gcal
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type colors struct {
day string
description string
past string
title string
highlights []interface{}
}
type Settings struct {
colors
common *cfg.Common
conflictIcon string
currentIcon string
displayResponseStatus bool
email string
eventCount int
multiCalendar bool
secretFile string
showDeclined bool
textInterval int
withLocation bool
}
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.gcal")
settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
conflictIcon: localConfig.UString("conflictIcon", "🚨"),
currentIcon: localConfig.UString("currentIcon", "🔸"),
displayResponseStatus: localConfig.UBool("displayResponseStatus", true),
email: localConfig.UString("email", ""),
eventCount: localConfig.UInt("eventCount", 10),
multiCalendar: localConfig.UBool("multiCalendar", false),
secretFile: localConfig.UString("secretFile", ""),
showDeclined: localConfig.UBool("showDeclined", false),
textInterval: localConfig.UInt("textInterval", 30),
withLocation: localConfig.UBool("withLocation", true),
}
settings.colors.day = localConfig.UString("colors.day", "forestgreen")
settings.colors.description = localConfig.UString("colors.description", "white")
settings.colors.past = localConfig.UString("colors.past", "gray")
settings.colors.title = localConfig.UString("colors.title", "white")
return &settings
}

View File

@ -11,17 +11,20 @@ import (
type Widget struct {
wtf.TextWidget
app *tview.Application
calEvents []*CalEvent
ch chan struct{}
mutex sync.Mutex
app *tview.Application
settings *Settings
}
func NewWidget(app *tview.Application) *Widget {
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Calendar", "gcal", true),
ch: make(chan struct{}),
app: app,
app: app,
ch: make(chan struct{}),
settings: settings,
}
go updateLoop(&widget)
@ -41,14 +44,14 @@ func (widget *Widget) Refresh() {
widget.fetchAndDisplayEvents()
return
}
widget.app.Suspend(authenticate)
widget.app.Suspend(widget.authenticate)
widget.Refresh()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) fetchAndDisplayEvents() {
calEvents, err := Fetch()
calEvents, err := widget.Fetch()
if err != nil {
widget.calEvents = []*CalEvent{}
} else {
@ -58,12 +61,11 @@ func (widget *Widget) fetchAndDisplayEvents() {
}
func updateLoop(widget *Widget) {
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
if interval == 0 {
if widget.settings.textInterval == 0 {
return
}
tick := time.NewTicker(time.Duration(interval) * time.Second)
tick := time.NewTicker(time.Duration(widget.settings.textInterval) * time.Second)
defer tick.Stop()
outer:
for {