diff --git a/_site/content/posts/glossary.md b/_site/content/posts/glossary.md new file mode 100644 index 00000000..5d7f07b2 --- /dev/null +++ b/_site/content/posts/glossary.md @@ -0,0 +1,17 @@ +--- +title: "Glossary" +date: 2018-04-17T12:34:51-07:00 +draft: false +--- + +### Module +A discreet unit of data collection and display. A data interface +concept. A package inside the app. + +Examples: New Relic, Git, Weather. + +### Widget +The onscreen representation of a Module. The widget is responsible for +being the interface between the app and the data collection. + +Widgets are defined by a required `widget.go` file in a Module. diff --git a/bamboohr/widget.go b/bamboohr/widget.go index 2ac4f3da..19de8f34 100644 --- a/bamboohr/widget.go +++ b/bamboohr/widget.go @@ -8,6 +8,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/gcal/widget.go b/gcal/widget.go index e862895c..ed42f376 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -10,6 +10,7 @@ import ( "google.golang.org/api/calendar/v3" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/git/widget.go b/git/widget.go index 3340574c..973da71d 100644 --- a/git/widget.go +++ b/git/widget.go @@ -10,6 +10,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/github/widget.go b/github/widget.go index 8ddd44f7..a501a704 100644 --- a/github/widget.go +++ b/github/widget.go @@ -9,6 +9,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/jira/widget.go b/jira/widget.go index 633393eb..36a9853a 100644 --- a/jira/widget.go +++ b/jira/widget.go @@ -8,6 +8,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/newrelic/widget.go b/newrelic/widget.go index c94e53a2..4b8a06f2 100644 --- a/newrelic/widget.go +++ b/newrelic/widget.go @@ -9,6 +9,7 @@ import ( nr "github.com/yfronto/newrelic" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { @@ -49,7 +50,7 @@ func (widget *Widget) Refresh() { widget.View.SetWrap(false) fmt.Fprintf(widget.View, "%s", widget.contentFrom(deploys)) } - + widget.RefreshedAt = time.Now() } diff --git a/opsgenie/widget.go b/opsgenie/widget.go index 1b7f7dd5..253c0cdf 100644 --- a/opsgenie/widget.go +++ b/opsgenie/widget.go @@ -9,6 +9,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/security/widget.go b/security/widget.go index 36f2f34b..0ea8c9ef 100644 --- a/security/widget.go +++ b/security/widget.go @@ -8,6 +8,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/status/widget.go b/status/widget.go index f630faa7..d919fc4f 100644 --- a/status/widget.go +++ b/status/widget.go @@ -10,6 +10,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/textfile/widget.go b/textfile/widget.go index 4d2da305..5c76e60d 100644 --- a/textfile/widget.go +++ b/textfile/widget.go @@ -10,6 +10,7 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object var Config *config.Config type Widget struct { diff --git a/weather/client.go b/weather/client.go deleted file mode 100644 index fba36fe5..00000000 --- a/weather/client.go +++ /dev/null @@ -1,38 +0,0 @@ -package weather - -import ( - //"fmt" - "os" - - owm "github.com/briandowns/openweathermap" -) - -/* -------------------- Exported Functions -------------------- */ - -func Fetch(cityids []int) []*owm.CurrentWeatherData { - apiKey := os.Getenv("WTF_OWM_API_KEY") - - data := []*owm.CurrentWeatherData{} - - for _, cityID := range cityids { - result, err := currentWeather(apiKey, cityID) - if err == nil { - data = append(data, result) - } - } - - return data -} - -/* -------------------- Unexported Functions -------------------- */ - -func currentWeather(apiKey string, cityCode int) (*owm.CurrentWeatherData, error) { - weather, err := owm.NewCurrent(Config.UString("wtf.mods.weather.tempUnit", "C"), Config.UString("wtf.mods.weather.language", "EN"), apiKey) - if err != nil { - return nil, err - } - - weather.CurrentByID(cityCode) - - return weather, nil -} diff --git a/weather/widget.go b/weather/widget.go index 993780d7..d76660ac 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -1,6 +1,7 @@ package weather import ( + "os" "time" owm "github.com/briandowns/openweathermap" @@ -9,19 +10,24 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) +// Config is a pointer to the global config object. var Config *config.Config +// Widget is the container for weather data. type Widget struct { wtf.TextWidget - Idx int - Data []*owm.CurrentWeatherData + APIKey string + Data []*owm.CurrentWeatherData + Idx int } +// NewWidget creates and returns a new instance of the weather Widget. func NewWidget() *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(" Weather ", "weather"), - Idx: 0, + APIKey: os.Getenv("WTF_OWM_API_KEY"), + Idx: 0, } widget.View.SetInputCapture(widget.keyboardIntercept) @@ -31,17 +37,37 @@ func NewWidget() *Widget { /* -------------------- Exported Functions -------------------- */ +// Fetch retrieves OpenWeatherMap data from the OpenWeatherMap API. +// It takes a list of OpenWeatherMap city IDs. +// It returns a list of OpenWeatherMap CurrentWeatherData structs, one per valid city code. +func (widget *Widget) Fetch(cityIDs []int) []*owm.CurrentWeatherData { + data := []*owm.CurrentWeatherData{} + + for _, cityID := range cityIDs { + result, err := widget.currentWeather(widget.APIKey, cityID) + if err == nil { + data = append(data, result) + } + } + + return data +} + +// Refresh fetches new data from the OpenWeatherMap API and loads the new data into the. +// widget's view for rendering func (widget *Widget) Refresh() { if widget.Disabled() { return } - widget.Data = Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes()))) + widget.Data = widget.Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes()))) widget.display(widget.Data) widget.RefreshedAt = time.Now() } +// Next displays data for the next city data in the list. If the current city is the last +// city, it wraps to the first city. func (widget *Widget) Next() { widget.Idx = widget.Idx + 1 if widget.Idx == len(widget.Data) { @@ -51,6 +77,8 @@ func (widget *Widget) Next() { widget.display(widget.Data) } +// Prev displays data for the previous city in the list. If the previous city is the first +// city, it wraps to the last city. func (widget *Widget) Prev() { widget.Idx = widget.Idx - 1 if widget.Idx < 0 { @@ -66,10 +94,24 @@ func (widget *Widget) currentCityData(data []*owm.CurrentWeatherData) *owm.Curre return data[widget.Idx] } +func (widget *Widget) currentWeather(apiKey string, cityCode int) (*owm.CurrentWeatherData, error) { + weather, err := owm.NewCurrent(Config.UString("wtf.mods.weather.tempUnit", "C"), Config.UString("wtf.mods.weather.language", "EN"), apiKey) + if err != nil { + return nil, err + } + + err = weather.CurrentByID(cityCode) + if err != nil { + return nil, err + } + + return weather, nil +} + func (widget *Widget) defaultCityCodes() []interface{} { defaultArr := []int{6176823, 360630, 3413829} - var defaults []interface{} = make([]interface{}, len(defaultArr)) + var defaults = make([]interface{}, len(defaultArr)) for i, d := range defaultArr { defaults[i] = d } @@ -95,7 +137,7 @@ func (widget *Widget) icon(data *owm.CurrentWeatherData) string { case "clear": icon = "☀️" case "clear sky": - icon = "☀️ " + icon = "☀️" case "cloudy": icon = "⛅️" case "few clouds": @@ -112,6 +154,8 @@ func (widget *Widget) icon(data *owm.CurrentWeatherData) string { icon = "☔️" case "light rain": icon = "🌦" + case "light shower snow": + icon = "🌦⛄️" case "light snow": icon = "🌨" case "mist": diff --git a/wtf/config_files.go b/wtf/config_files.go index 8e9b8c82..bf377f89 100644 --- a/wtf/config_files.go +++ b/wtf/config_files.go @@ -28,7 +28,7 @@ func LoadConfigFile(filePath string) *config.Config { cfg, err := config.ParseYamlFile(absPath) if err != nil { - fmt.Println("\n\n\033[1m ERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.\n Please add a \033[0;33mconfig.yml\033[0m file to your \033[0;33m~/.wtf\033[0m directory.\n See \033[1;34mhttps://github.com/senorprogrammer/wtf\033[0m for details.\n\n") + fmt.Println("\n\n\033[1m ERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.\n Please add a \033[0;33mconfig.yml\033[0m file to your \033[0;33m~/.wtf\033[0m directory.\n See \033[1;34mhttps://github.com/senorprogrammer/wtf\033[0m for details.") os.Exit(1) } diff --git a/wtf/focus_tracker.go b/wtf/focus_tracker.go index bb008d33..f1c44fc4 100644 --- a/wtf/focus_tracker.go +++ b/wtf/focus_tracker.go @@ -5,6 +5,8 @@ import ( "github.com/senorprogrammer/wtf/color" ) +// FocusTracker is used by the app to track which onscreen widget currently has focus, +// and to move focus between widgets. type FocusTracker struct { App *tview.Application Idx int @@ -13,16 +15,21 @@ type FocusTracker struct { /* -------------------- Exported Functions -------------------- */ +// Next sets the focus on the next widget in the widget list. If the current widget is +// the last widget, sets focus on the first widget. func (tracker *FocusTracker) Next() { tracker.blur(tracker.Idx) tracker.increment() tracker.focus(tracker.Idx) } +// None removes focus from the currently-focused widget. func (tracker *FocusTracker) None() { tracker.blur(tracker.Idx) } +// Prev sets the focus on the previous widget in the widget list. If the current widget is +// the last widget, sets focus on the last widget. func (tracker *FocusTracker) Prev() { tracker.blur(tracker.Idx) tracker.decrement() diff --git a/wtf/utils.go b/wtf/utils.go index d5f4cd11..3db7c142 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -33,7 +33,10 @@ func ExecuteCommand(cmd *exec.Cmd) string { str += string(b) } - cmd.Wait() + err = cmd.Wait() + if err != nil { + return fmt.Sprintf("%v\n", err) + } return str }