From 1a898b05e3041c5ff7281b3ec95d3f3e5ea40e7c Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 21 Jun 2018 19:32:32 -0700 Subject: [PATCH] Remove complexity from a lot of string display statements --- bamboohr/widget.go | 4 ++-- circleci/widget.go | 7 +++++-- clocks/display.go | 2 +- cmdrunner/widget.go | 4 ++-- cryptoexchanges/bittrex/display.go | 7 ++----- cryptoexchanges/blockfolio/widget.go | 3 ++- gcal/widget.go | 2 +- git/display.go | 5 ++--- github/display.go | 3 +-- gitlab/display.go | 2 +- gspreadsheets/widget.go | 2 +- ipaddresses/ipapi/widget.go | 6 +++--- ipaddresses/ipinfo/widget.go | 6 +++--- jenkins/widget.go | 16 +++++++--------- jira/widget.go | 9 ++++++--- logger/log.go | 4 ++-- newrelic/widget.go | 7 +++++-- opsgenie/widget.go | 7 +++++-- power/widget.go | 10 +++++----- security/widget.go | 3 +-- security/widget_windows.go | 4 +--- status/widget.go | 10 +--------- textfile/widget.go | 4 ++-- todo/display.go | 2 +- trello/widget.go | 9 ++++++--- weatherservices/prettyweather/widget.go | 14 ++++++++------ weatherservices/weather/display.go | 19 +++++++++---------- 27 files changed, 85 insertions(+), 86 deletions(-) diff --git a/bamboohr/widget.go b/bamboohr/widget.go index 3d38946d..bd97ad7a 100644 --- a/bamboohr/widget.go +++ b/bamboohr/widget.go @@ -29,9 +29,9 @@ func (widget *Widget) Refresh() { ) widget.UpdateRefreshedAt() - widget.View.SetTitle(fmt.Sprintf("%s(%d)", widget.Name, len(todayItems))) + widget.View.SetTitle(fmt.Sprintf("%s (%d)", widget.Name, len(todayItems))) - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(todayItems))) + widget.View.SetText(widget.contentFrom(todayItems)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/circleci/widget.go b/circleci/widget.go index e15cc5fd..f104dc68 100644 --- a/circleci/widget.go +++ b/circleci/widget.go @@ -30,13 +30,16 @@ func (widget *Widget) Refresh() { widget.View.SetTitle(fmt.Sprintf("%s - Builds", widget.Name)) + var content string if err != nil { widget.View.SetWrap(true) - fmt.Fprintf(widget.View, "%v", err) + content = err.Error() } else { widget.View.SetWrap(false) - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(builds))) + content = widget.contentFrom(builds) } + + widget.View.SetText(content) } /* -------------------- Unexported Functions -------------------- */ diff --git a/clocks/display.go b/clocks/display.go index 6cc7d60f..baad6dac 100644 --- a/clocks/display.go +++ b/clocks/display.go @@ -23,5 +23,5 @@ func (widget *Widget) display(clocks []Clock) { ) } - widget.View.SetText(fmt.Sprintf("%s", str)) + widget.View.SetText(str) } diff --git a/cmdrunner/widget.go b/cmdrunner/widget.go index 1a28308e..62183b4d 100644 --- a/cmdrunner/widget.go +++ b/cmdrunner/widget.go @@ -34,9 +34,9 @@ func (widget *Widget) Refresh() { widget.execute() title := wtf.Config.UString("wtf.mods.cmdrunner.title", widget.String()) - widget.View.SetTitle(fmt.Sprintf("%s", title)) + widget.View.SetTitle(title) - widget.View.SetText(fmt.Sprintf("%s", widget.result)) + widget.View.SetText(widget.result) } func (widget *Widget) String() string { diff --git a/cryptoexchanges/bittrex/display.go b/cryptoexchanges/bittrex/display.go index 2718e0f7..3c8fa6e4 100644 --- a/cryptoexchanges/bittrex/display.go +++ b/cryptoexchanges/bittrex/display.go @@ -8,14 +8,11 @@ import ( func (widget *Widget) display() { if ok == false { - widget.View.SetText(fmt.Sprintf("%s", errorText)) + widget.View.SetText(errorText) return } - str := "" - str += summaryText(&widget.summaryList, &widget.TextColors) - - widget.View.SetText(fmt.Sprintf("%s", str)) + widget.View.SetText(summaryText(&widget.summaryList, &widget.TextColors)) } func summaryText(list *summaryList, colors *TextColors) string { diff --git a/cryptoexchanges/blockfolio/widget.go b/cryptoexchanges/blockfolio/widget.go index ac7b2440..72349738 100644 --- a/cryptoexchanges/blockfolio/widget.go +++ b/cryptoexchanges/blockfolio/widget.go @@ -37,7 +37,8 @@ func (widget *Widget) Refresh() { if err != nil { return } - widget.View.SetText(fmt.Sprintf("%s", contentFrom(positions))) + + widget.View.SetText(contentFrom(positions)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/gcal/widget.go b/gcal/widget.go index 526ac5cc..edca17a6 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -55,7 +55,7 @@ func (widget *Widget) display() { widget.mutex.Lock() defer widget.mutex.Unlock() - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(widget.events))) + widget.View.SetText(widget.contentFrom(widget.events)) } // conflicts returns TRUE if this event conflicts with another, FALSE if it does not diff --git a/git/display.go b/git/display.go index 8140615b..33842728 100644 --- a/git/display.go +++ b/git/display.go @@ -9,10 +9,9 @@ import ( ) func (widget *Widget) display() { - repoData := widget.currentData() if repoData == nil { - fmt.Fprintf(widget.View, "%s", " Git repo data is unavailable (1)") + widget.View.SetText(" Git repo data is unavailable ") return } @@ -27,7 +26,7 @@ func (widget *Widget) display() { str = str + "\n" str = str + widget.formatCommits(repoData.Commits) - widget.View.SetText(fmt.Sprintf("%s", str)) + widget.View.SetText(str) } func (widget *Widget) formatChanges(data []string) string { diff --git a/github/display.go b/github/display.go index 6aa31082..2fa4528e 100644 --- a/github/display.go +++ b/github/display.go @@ -7,10 +7,9 @@ import ( ) func (widget *Widget) display() { - repo := widget.currentGithubRepo() if repo == nil { - fmt.Fprintf(widget.View, "%s", " Github repo data is unavailable (1)") + widget.View.SetText(" GitHub repo data is unavailable ") return } diff --git a/gitlab/display.go b/gitlab/display.go index d7783385..4b268044 100644 --- a/gitlab/display.go +++ b/gitlab/display.go @@ -10,7 +10,7 @@ func (widget *Widget) display() { project := widget.currentGitlabProject() if project == nil { - fmt.Fprintf(widget.View, "%s", " Gitlab project data is unavailable (1)") + widget.View.SetText(" Gitlab project data is unavailable ") return } diff --git a/gspreadsheets/widget.go b/gspreadsheets/widget.go index 416523e0..ddd246d9 100644 --- a/gspreadsheets/widget.go +++ b/gspreadsheets/widget.go @@ -26,7 +26,7 @@ func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(cells))) + widget.View.SetText(widget.contentFrom(cells)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/ipaddresses/ipapi/widget.go b/ipaddresses/ipapi/widget.go index e40e4039..42730662 100644 --- a/ipaddresses/ipapi/widget.go +++ b/ipaddresses/ipapi/widget.go @@ -61,20 +61,20 @@ func (widget *Widget) ipinfo() { client := &http.Client{} req, err := http.NewRequest("GET", "http://ip-api.com/json", nil) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } req.Header.Set("User-Agent", "curl") response, err := client.Do(req) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } defer response.Body.Close() var info ipinfo err = json.NewDecoder(response.Body).Decode(&info) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } diff --git a/ipaddresses/ipinfo/widget.go b/ipaddresses/ipinfo/widget.go index f165aa5a..031dc032 100644 --- a/ipaddresses/ipinfo/widget.go +++ b/ipaddresses/ipinfo/widget.go @@ -55,13 +55,13 @@ func (widget *Widget) ipinfo() { client := &http.Client{} req, err := http.NewRequest("GET", "https://ipinfo.io/", nil) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } req.Header.Set("User-Agent", "curl") response, err := client.Do(req) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } defer response.Body.Close() @@ -69,7 +69,7 @@ func (widget *Widget) ipinfo() { var info ipinfo err = json.NewDecoder(response.Body).Decode(&info) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } diff --git a/jenkins/widget.go b/jenkins/widget.go index 943f78c7..262da966 100644 --- a/jenkins/widget.go +++ b/jenkins/widget.go @@ -32,22 +32,20 @@ func (widget *Widget) Refresh() { ) widget.UpdateRefreshedAt() - widget.View.Clear() + //widget.View.Clear() + var content string if err != nil { widget.View.SetWrap(true) widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name)) - fmt.Fprintf(widget.View, "%v", err) + content = err.Error() } else { widget.View.SetWrap(false) - widget.View.SetTitle( - fmt.Sprintf( - " %s: [green] ", - widget.Name, - ), - ) - fmt.Fprintf(widget.View, "%s", widget.contentFrom(view)) + widget.View.SetTitle(fmt.Sprintf(" %s: [green] ", widget.Name)) + content = widget.contentFrom(view) } + + widget.View.SetText(content) } /* -------------------- Unexported Functions -------------------- */ diff --git a/jira/widget.go b/jira/widget.go index 4ae7a4a7..9ca26540 100644 --- a/jira/widget.go +++ b/jira/widget.go @@ -29,10 +29,11 @@ func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() + var content string if err != nil { widget.View.SetWrap(true) - widget.View.SetTitle(fmt.Sprintf("%s", widget.Name)) - fmt.Fprintf(widget.View, "%v", err) + widget.View.SetTitle(widget.Name) + content = err.Error() } else { widget.View.SetWrap(false) widget.View.SetTitle( @@ -42,8 +43,10 @@ func (widget *Widget) Refresh() { wtf.Config.UString("wtf.mods.jira.project"), ), ) - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(searchResult))) + content = widget.contentFrom(searchResult) } + + widget.View.SetText(content) } /* -------------------- Unexported Functions -------------------- */ diff --git a/logger/log.go b/logger/log.go index fba557e8..69b5fceb 100644 --- a/logger/log.go +++ b/logger/log.go @@ -52,10 +52,10 @@ func (widget *Widget) Refresh() { } widget.UpdateRefreshedAt() - widget.View.SetTitle(fmt.Sprintf("%s", widget.Name)) + widget.View.SetTitle(widget.Name) logLines := widget.tailFile() - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(logLines))) + widget.View.SetText(widget.contentFrom(logLines)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/newrelic/widget.go b/newrelic/widget.go index 09cf5f81..2ab2928a 100644 --- a/newrelic/widget.go +++ b/newrelic/widget.go @@ -34,13 +34,16 @@ func (widget *Widget) Refresh() { widget.View.SetTitle(fmt.Sprintf("%s- [green]%s[white]", widget.Name, appName)) widget.View.Clear() + var content string if depErr != nil { widget.View.SetWrap(true) - widget.View.SetText(fmt.Sprintf("%s", depErr)) + content = depErr.Error() } else { widget.View.SetWrap(false) - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(deploys))) + content = widget.contentFrom(deploys) } + + widget.View.SetText(content) } /* -------------------- Unexported Functions -------------------- */ diff --git a/opsgenie/widget.go b/opsgenie/widget.go index 593302aa..7ca23621 100644 --- a/opsgenie/widget.go +++ b/opsgenie/widget.go @@ -27,13 +27,16 @@ func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() widget.View.SetTitle(widget.Name) + var content string if err != nil { widget.View.SetWrap(true) - widget.View.SetText(fmt.Sprintf("%s", err)) + content = err.Error() } else { widget.View.SetWrap(false) - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(data))) + content = widget.contentFrom(data) } + + widget.View.SetText(content) } /* -------------------- Unexported Functions -------------------- */ diff --git a/power/widget.go b/power/widget.go index c191b620..47e7d73c 100644 --- a/power/widget.go +++ b/power/widget.go @@ -27,10 +27,10 @@ func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() widget.Battery.Refresh() - str := "" - str = str + fmt.Sprintf(" %10s: %s\n", "Source", powerSource()) - str = str + "\n" - str = str + widget.Battery.String() + content := "" + content = content + fmt.Sprintf(" %10s: %s\n", "Source", powerSource()) + content = content + "\n" + content = content + widget.Battery.String() - widget.View.SetText(fmt.Sprintf("%s", str)) + widget.View.SetText(content) } diff --git a/security/widget.go b/security/widget.go index a991af49..b95e8354 100644 --- a/security/widget.go +++ b/security/widget.go @@ -28,8 +28,7 @@ func (widget *Widget) Refresh() { data.Fetch() widget.UpdateRefreshedAt() - - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(data))) + widget.View.SetText(widget.contentFrom(data)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/security/widget_windows.go b/security/widget_windows.go index 009e7860..c46dc6f7 100644 --- a/security/widget_windows.go +++ b/security/widget_windows.go @@ -32,9 +32,7 @@ func (widget *Widget) Refresh() { data.Fetch() widget.UpdateRefreshedAt() - widget.View.Clear() - - fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) + widget.View.SetText(widget.contentFrom(data)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/status/widget.go b/status/widget.go index 43f40238..9fb1191b 100644 --- a/status/widget.go +++ b/status/widget.go @@ -1,8 +1,6 @@ package status import ( - "fmt" - "github.com/senorprogrammer/wtf/wtf" ) @@ -25,13 +23,7 @@ func NewWidget() *Widget { func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() - - widget.View.SetText( - fmt.Sprintf( - "\n%s", - widget.animation(), - ), - ) + widget.View.SetText(widget.animation()) } /* -------------------- Unexported Functions -------------------- */ diff --git a/textfile/widget.go b/textfile/widget.go index e17e452a..e9147706 100644 --- a/textfile/widget.go +++ b/textfile/widget.go @@ -55,9 +55,9 @@ func (widget *Widget) Refresh() { } if err != nil { - widget.View.SetText(fmt.Sprintf("%s", err)) + widget.View.SetText(err.Error()) } else { - widget.View.SetText(fmt.Sprintf("%s", string(fileData))) + widget.View.SetText(string(fileData)) } } diff --git a/todo/display.go b/todo/display.go index 6c5c7137..8757bad4 100644 --- a/todo/display.go +++ b/todo/display.go @@ -29,7 +29,7 @@ func (widget *Widget) display() { widget.SetList(&newList) widget.View.Clear() - widget.View.SetText(fmt.Sprintf("%s", str)) + widget.View.SetText(str) } func (widget *Widget) formattedItemLine(item *Item, selectedItem *Item, maxLen int) string { diff --git a/trello/widget.go b/trello/widget.go index 99c1b64a..068e7377 100644 --- a/trello/widget.go +++ b/trello/widget.go @@ -29,10 +29,11 @@ func (widget *Widget) Refresh() { searchResult, err := GetCards(client, getLists()) widget.UpdateRefreshedAt() + var content string if err != nil { widget.View.SetWrap(true) - widget.View.SetTitle(fmt.Sprintf("%s", widget.Name)) - fmt.Fprintf(widget.View, "%v", err) + widget.View.SetTitle(widget.Name) + content = err.Error() } else { widget.View.SetWrap(false) widget.View.SetTitle( @@ -42,8 +43,10 @@ func (widget *Widget) Refresh() { wtf.Config.UString("wtf.mods.trello.board"), ), ) - widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(searchResult))) + content = widget.contentFrom(searchResult) } + + widget.View.SetText(content) } /* -------------------- Unexported Functions -------------------- */ diff --git a/weatherservices/prettyweather/widget.go b/weatherservices/prettyweather/widget.go index 8c47c5c0..5160e68f 100644 --- a/weatherservices/prettyweather/widget.go +++ b/weatherservices/prettyweather/widget.go @@ -1,7 +1,6 @@ package prettyweather import ( - "fmt" "io/ioutil" "net/http" "strings" @@ -29,7 +28,7 @@ func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() widget.prettyWeather() - widget.View.SetText(fmt.Sprintf("%s", widget.result)) + widget.View.SetText(widget.result) } //this method reads the config and calls wttr.in for pretty weather @@ -40,21 +39,24 @@ func (widget *Widget) prettyWeather() { widget.view = wtf.Config.UString("wtf.mods.prettyweather.view", "0") req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?"+widget.view+"?"+widget.unit, nil) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } + req.Header.Set("User-Agent", "curl") response, err := client.Do(req) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return + } defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) + widget.result = err.Error() return } - widget.result = fmt.Sprintf("%s", strings.TrimSpace(string(contents))) + widget.result = strings.TrimSpace(string(contents)) } diff --git a/weatherservices/weather/display.go b/weatherservices/weather/display.go index 33bf9314..9eef59b2 100644 --- a/weatherservices/weather/display.go +++ b/weatherservices/weather/display.go @@ -9,32 +9,31 @@ import ( ) func (widget *Widget) display() { - widget.View.Clear() if widget.apiKeyValid() == false { - fmt.Fprintf(widget.View, "%s", " Environment variable WTF_OWM_API_KEY is not set") + widget.View.SetText(" Environment variable WTF_OWM_API_KEY is not set") return } cityData := widget.currentData() if cityData == nil { - fmt.Fprintf(widget.View, "%s", " Weather data is unavailable (1)") + widget.View.SetText(" Weather data is unavailable: no city data") return } if len(cityData.Weather) == 0 { - fmt.Fprintf(widget.View, "%s", " Weather data is unavailable (2)") + widget.View.SetText(" Weather data is unavailable: no weather data") return } widget.View.SetTitle(widget.title(cityData)) - str := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n" - str = str + widget.description(cityData) + "\n\n" - str = str + widget.temperatures(cityData) + "\n" - str = str + widget.sunInfo(cityData) + content := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n" + content = content + widget.description(cityData) + "\n\n" + content = content + widget.temperatures(cityData) + "\n" + content = content + widget.sunInfo(cityData) - fmt.Fprintf(widget.View, "%s", str) + widget.View.SetText(content) } func (widget *Widget) description(cityData *owm.CurrentWeatherData) string { @@ -73,5 +72,5 @@ func (widget *Widget) temperatures(cityData *owm.CurrentWeatherData) string { } func (widget *Widget) title(cityData *owm.CurrentWeatherData) string { - return fmt.Sprintf(" %s %s ", widget.icon(cityData), cityData.Name) + return fmt.Sprintf(" %s %s ", widget.icon(cityData), cityData.Name) }