From b4bc6d4509a86dfd2f5636203125cf58c05b932c Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 7 Apr 2018 13:55:08 -0700 Subject: [PATCH] Remove a ton of duplication around TextView widget creation --- bamboohr/widget.go | 10 ++------ config.yml | 60 ++++++++++++++++++++++++++++++++++++++++++++++ gcal/widget.go | 10 ++------ git/widget.go | 9 ++----- github/widget.go | 9 ++----- jira/widget.go | 9 ++----- newrelic/widget.go | 10 ++------ opsgenie/widget.go | 9 ++----- security/widget.go | 9 ++----- status/widget.go | 11 +++------ weather/widget.go | 9 ++----- wtf.go | 1 + wtf/base_widget.go | 16 ------------- wtf/text_widget.go | 47 ++++++++++++++++++++++++++++++++++++ 14 files changed, 129 insertions(+), 90 deletions(-) delete mode 100644 wtf/base_widget.go create mode 100644 wtf/text_widget.go diff --git a/bamboohr/widget.go b/bamboohr/widget.go index 19ad1ad7..f3ae9782 100644 --- a/bamboohr/widget.go +++ b/bamboohr/widget.go @@ -13,18 +13,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "BambooHR", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.bamboohr.refreshInterval", 900), - }, + TextWidget: wtf.NewTextWidget("BambooHR", "bamboohr"), } widget.addView() diff --git a/config.yml b/config.yml index a0a5133a..3d44f29b 100644 --- a/config.yml +++ b/config.yml @@ -1,36 +1,96 @@ wtf: refreshInterval: 1 bamboohr: + enabled: false + position: + top: + left: + width: + height: refreshInterval: 900 url: "https://api.bamboohr.com/api/gateway.php" gcal: currentIcon: "💥" + enabled: true eventCount: 10 + position: + top: + left: + width: + height: refreshInterval: 300 secretFile: "~/.wtf/gcal/client_secret.json" git: commitCount: 5 + enabled: true + position: + top: + left: + width: + height: refreshInterval: 8 repository: "/Users/chris/go/src/github.com/senorprogrammer/wtf" github: + enabled: true organization: "BetterOfficeApps" + position: + top: + left: + width: + height: refreshInterval: 300 repo: "core-api" username: "senorprogrammer" jira: + enabled: true + position: + top: + left: + width: + height: refreshInterval: 900 newrelic: applicationId: 10549735 + enabled: true deployCount: 5 + position: + top: + left: + width: + height: refreshInterval: 900 opsgenie: + enabled: true + position: + top: + left: + width: + height: refreshInterval: 21600 security: + enabled: true + position: + top: + left: + width: + height: refreshInterval: 3600 status: + enabled: true + position: + top: + left: + width: + height: refreshInterval: 1 weather: cityId: 6173331 + enabled: true language: "EN" + position: + top: + left: + width: + height: tempUnit: "C" refreshInterval: 900 diff --git a/gcal/widget.go b/gcal/widget.go index cfe393e8..93273472 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -15,18 +15,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "Calendar", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.gcal.refreshInterval", 300), - }, + TextWidget: wtf.NewTextWidget("Calendar", "gcal"), } widget.addView() diff --git a/git/widget.go b/git/widget.go index b7abf22c..25352ec0 100644 --- a/git/widget.go +++ b/git/widget.go @@ -15,17 +15,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "Git", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.git.refreshInterval", 15), - }, + TextWidget: wtf.NewTextWidget("Git", "git"), } widget.addView() diff --git a/github/widget.go b/github/widget.go index 9cc17547..06b349f4 100644 --- a/github/widget.go +++ b/github/widget.go @@ -14,17 +14,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "Github", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.github.refreshInterval", 900), - }, + TextWidget: wtf.NewTextWidget("Github", "github"), } widget.addView() diff --git a/jira/widget.go b/jira/widget.go index 4351da40..249ca710 100644 --- a/jira/widget.go +++ b/jira/widget.go @@ -13,17 +13,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "JIRA", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.jira.refreshInterval", 900), - }, + TextWidget: wtf.NewTextWidget("JIRA", "jira"), } widget.addView() diff --git a/newrelic/widget.go b/newrelic/widget.go index bae81372..6d3890c3 100644 --- a/newrelic/widget.go +++ b/newrelic/widget.go @@ -14,18 +14,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "New Relic", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.newrelic.refreshInterval", 900), - }, + TextWidget: wtf.NewTextWidget("New Relic", "newrelic"), } widget.addView() diff --git a/opsgenie/widget.go b/opsgenie/widget.go index 7a929c37..4beccf3c 100644 --- a/opsgenie/widget.go +++ b/opsgenie/widget.go @@ -14,17 +14,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "OpsGenie", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.opsgenie.refreshInterval", 21600), - }, + TextWidget: wtf.NewTextWidget("OpsGenie", "opsgenie"), } widget.addView() diff --git a/security/widget.go b/security/widget.go index 5a2c0715..c39adfd3 100644 --- a/security/widget.go +++ b/security/widget.go @@ -14,17 +14,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "Security", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.security.refreshInterval", 3600), - }, + TextWidget: wtf.NewTextWidget("Security", "security"), } widget.addView() diff --git a/status/widget.go b/status/widget.go index 310a247a..9484ac14 100644 --- a/status/widget.go +++ b/status/widget.go @@ -13,20 +13,15 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget + wtf.TextWidget Current int - View *tview.TextView } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "Status", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.status.refreshInterval", 1), - }, - Current: 0, + TextWidget: wtf.NewTextWidget("Status", "status"), + Current: 0, } widget.addView() diff --git a/weather/widget.go b/weather/widget.go index 78f6f74b..65508290 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -15,17 +15,12 @@ import ( var Config *config.Config type Widget struct { - wtf.BaseWidget - View *tview.TextView + wtf.TextWidget } func NewWidget() *Widget { widget := Widget{ - BaseWidget: wtf.BaseWidget{ - Name: "Weather", - RefreshedAt: time.Now(), - RefreshInt: Config.UInt("wtf.weather.refreshInterval", 900), - }, + TextWidget: wtf.NewTextWidget("Weather", "weather"), } widget.addView() diff --git a/wtf.go b/wtf.go index 15b97c63..ffe29e03 100644 --- a/wtf.go +++ b/wtf.go @@ -38,6 +38,7 @@ var Config = wtf.LoadConfigFile() /* -------------------- Main -------------------- */ func main() { + wtf.Config = Config bamboohr.Config = Config bamboo := bamboohr.NewWidget() diff --git a/wtf/base_widget.go b/wtf/base_widget.go deleted file mode 100644 index 81566c6d..00000000 --- a/wtf/base_widget.go +++ /dev/null @@ -1,16 +0,0 @@ -package wtf - -import ( - //"fmt" - "time" -) - -type BaseWidget struct { - Name string - RefreshedAt time.Time - RefreshInt int -} - -func (widget *BaseWidget) RefreshInterval() int { - return widget.RefreshInt -} diff --git a/wtf/text_widget.go b/wtf/text_widget.go new file mode 100644 index 00000000..25a059d6 --- /dev/null +++ b/wtf/text_widget.go @@ -0,0 +1,47 @@ +package wtf + +import ( + "fmt" + "time" + + "github.com/olebedev/config" + "github.com/rivo/tview" +) + +var Config *config.Config + +type Position struct { + Top int + Left int + Width int + Height int +} + +type TextWidget struct { + Enabled bool + Name string + Position Position + RefreshedAt time.Time + RefreshInt int + View *tview.TextView +} + +func NewTextWidget(name string, configKey string) TextWidget { + widget := TextWidget{ + Enabled: Config.UBool(fmt.Sprintf("wtf.%s.refreshInterval", configKey), false), + Name: name, + RefreshInt: Config.UInt(fmt.Sprintf("wtf.%s.refreshInterval", configKey)), + } + + return widget +} + +/* -------------------- Exported Functions -------------------- */ + +func (widget *TextWidget) RefreshInterval() int { + return widget.RefreshInt +} + +func (widget *TextWidget) TextView() *tview.TextView { + return widget.View +}