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:
parent
5e1fdef5d4
commit
12a895b9df
3
main.go
3
main.go
@ -208,7 +208,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
|
|||||||
cfg := datadog.NewSettingsFromYAML(wtf.Config)
|
cfg := datadog.NewSettingsFromYAML(wtf.Config)
|
||||||
widget = datadog.NewWidget(app, cfg)
|
widget = datadog.NewWidget(app, cfg)
|
||||||
case "gcal":
|
case "gcal":
|
||||||
widget = gcal.NewWidget(app)
|
cfg := gcal.NewSettingsFromYAML(wtf.Config)
|
||||||
|
widget = gcal.NewWidget(app, cfg)
|
||||||
case "gerrit":
|
case "gerrit":
|
||||||
widget = gerrit.NewWidget(app, pages)
|
widget = gerrit.NewWidget(app, pages)
|
||||||
case "git":
|
case "git":
|
||||||
|
@ -27,10 +27,10 @@ import (
|
|||||||
|
|
||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
|
||||||
func Fetch() ([]*CalEvent, error) {
|
func (widget *Widget) Fetch() ([]*CalEvent, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
secretPath, _ := wtf.ExpandHomeDir(wtf.Config.UString("wtf.mods.gcal.secretFile"))
|
secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile)
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(secretPath)
|
b, err := ioutil.ReadFile(secretPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -48,13 +48,13 @@ func Fetch() ([]*CalEvent, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
calendarIds, err := getCalendarIdList(srv)
|
calendarIds, err := widget.getCalendarIdList(srv)
|
||||||
|
|
||||||
// Get calendar events
|
// Get calendar events
|
||||||
var events calendar.Events
|
var events calendar.Events
|
||||||
|
|
||||||
startTime := fromMidnight().Format(time.RFC3339)
|
startTime := fromMidnight().Format(time.RFC3339)
|
||||||
eventLimit := int64(wtf.Config.UInt("wtf.mods.gcal.eventCount", 10))
|
eventLimit := int64(widget.settings.eventCount)
|
||||||
|
|
||||||
for _, calendarId := range calendarIds {
|
for _, calendarId := range calendarIds {
|
||||||
calendarEvents, err := srv.Events.List(calendarId).ShowDeleted(false).TimeMin(startTime).MaxResults(eventLimit).SingleEvents(true).OrderBy("startTime").Do()
|
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
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func authenticate() {
|
func (widget *Widget) authenticate() {
|
||||||
filename := wtf.Config.UString("wtf.mods.gcal.secretFile")
|
secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile)
|
||||||
secretPath, _ := wtf.ExpandHomeDir(filename)
|
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(secretPath)
|
b, err := ioutil.ReadFile(secretPath)
|
||||||
if err != nil {
|
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)
|
config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
|
||||||
@ -188,9 +187,9 @@ func saveToken(file string, token *oauth2.Token) {
|
|||||||
json.NewEncoder(f).Encode(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
|
// 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()
|
id, err := srv.CalendarList.Get("primary").Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -44,8 +44,8 @@ func (widget *Widget) contentFrom(calEvents []*CalEvent) string {
|
|||||||
var str string
|
var str string
|
||||||
var prevEvent *CalEvent
|
var prevEvent *CalEvent
|
||||||
|
|
||||||
if !wtf.Config.UBool("wtf.mods.gcal.showDeclined", false) {
|
if !widget.settings.showDeclined {
|
||||||
calEvents = removeDeclined(calEvents)
|
calEvents = widget.removeDeclined(calEvents)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, calEvent := range calEvents {
|
for _, calEvent := range calEvents {
|
||||||
@ -101,7 +101,7 @@ func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string {
|
|||||||
if !eventStartDay.Equal(prevStartDay) {
|
if !eventStartDay.Equal(prevStartDay) {
|
||||||
|
|
||||||
return fmt.Sprintf("[%s::b]",
|
return fmt.Sprintf("[%s::b]",
|
||||||
wtf.Config.UString("wtf.mods.gcal.colors.day", "forestgreen")) +
|
widget.settings.colors.day) +
|
||||||
event.Start().Format(wtf.FullDateFormat) +
|
event.Start().Format(wtf.FullDateFormat) +
|
||||||
"\n"
|
"\n"
|
||||||
}
|
}
|
||||||
@ -111,10 +111,10 @@ func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string {
|
|||||||
|
|
||||||
func (widget *Widget) descriptionColor(calEvent *CalEvent) string {
|
func (widget *Widget) descriptionColor(calEvent *CalEvent) string {
|
||||||
if calEvent.Past() {
|
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 {
|
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() {
|
if calEvent.Now() {
|
||||||
summary = fmt.Sprintf(
|
summary = fmt.Sprintf(
|
||||||
"%s %s",
|
"%s %s",
|
||||||
wtf.Config.UString("wtf.mods.gcal.currentIcon", "🔸"),
|
widget.settings.currentIcon,
|
||||||
summary,
|
summary,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if conflict {
|
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
|
return summary
|
||||||
@ -170,9 +170,9 @@ func (widget *Widget) timeUntil(calEvent *CalEvent) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) titleColor(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{}))
|
highlightElements := wtf.ToStrs(untypedArr.([]interface{}))
|
||||||
|
|
||||||
match, _ := regexp.MatchString(
|
match, _ := regexp.MatchString(
|
||||||
@ -186,14 +186,14 @@ func (widget *Widget) titleColor(calEvent *CalEvent) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if calEvent.Past() {
|
if calEvent.Past() {
|
||||||
color = wtf.Config.UString("wtf.mods.gcal.colors.past", "gray")
|
color = widget.settings.colors.past
|
||||||
}
|
}
|
||||||
|
|
||||||
return color
|
return color
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) location(calEvent *CalEvent) string {
|
func (widget *Widget) location(calEvent *CalEvent) string {
|
||||||
if wtf.Config.UBool("wtf.mods.gcal.displayLocation", true) == false {
|
if widget.settings.withLocation == false {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,13 +209,13 @@ func (widget *Widget) location(calEvent *CalEvent) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) responseIcon(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 ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
icon := "[gray]"
|
icon := "[gray]"
|
||||||
|
|
||||||
switch calEvent.ResponseFor(wtf.Config.UString("wtf.mods.gcal.email")) {
|
switch calEvent.ResponseFor(widget.settings.email) {
|
||||||
case "accepted":
|
case "accepted":
|
||||||
return icon + "✔︎"
|
return icon + "✔︎"
|
||||||
case "declined":
|
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
|
var ret []*CalEvent
|
||||||
for _, e := range events {
|
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)
|
ret = append(ret, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
57
modules/gcal/settings.go
Normal file
57
modules/gcal/settings.go
Normal 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
|
||||||
|
}
|
@ -11,17 +11,20 @@ import (
|
|||||||
type Widget struct {
|
type Widget struct {
|
||||||
wtf.TextWidget
|
wtf.TextWidget
|
||||||
|
|
||||||
|
app *tview.Application
|
||||||
calEvents []*CalEvent
|
calEvents []*CalEvent
|
||||||
ch chan struct{}
|
ch chan struct{}
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
app *tview.Application
|
settings *Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidget(app *tview.Application) *Widget {
|
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
TextWidget: wtf.NewTextWidget(app, "Calendar", "gcal", true),
|
TextWidget: wtf.NewTextWidget(app, "Calendar", "gcal", true),
|
||||||
ch: make(chan struct{}),
|
|
||||||
app: app,
|
app: app,
|
||||||
|
ch: make(chan struct{}),
|
||||||
|
settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
go updateLoop(&widget)
|
go updateLoop(&widget)
|
||||||
@ -41,14 +44,14 @@ func (widget *Widget) Refresh() {
|
|||||||
widget.fetchAndDisplayEvents()
|
widget.fetchAndDisplayEvents()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
widget.app.Suspend(authenticate)
|
widget.app.Suspend(widget.authenticate)
|
||||||
widget.Refresh()
|
widget.Refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *Widget) fetchAndDisplayEvents() {
|
func (widget *Widget) fetchAndDisplayEvents() {
|
||||||
calEvents, err := Fetch()
|
calEvents, err := widget.Fetch()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
widget.calEvents = []*CalEvent{}
|
widget.calEvents = []*CalEvent{}
|
||||||
} else {
|
} else {
|
||||||
@ -58,12 +61,11 @@ func (widget *Widget) fetchAndDisplayEvents() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateLoop(widget *Widget) {
|
func updateLoop(widget *Widget) {
|
||||||
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
|
if widget.settings.textInterval == 0 {
|
||||||
if interval == 0 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tick := time.NewTicker(time.Duration(interval) * time.Second)
|
tick := time.NewTicker(time.Duration(widget.settings.textInterval) * time.Second)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
outer:
|
outer:
|
||||||
for {
|
for {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user