diff --git a/modules/bamboohr/widget.go b/modules/bamboohr/widget.go index 48a21329..b4762273 100644 --- a/modules/bamboohr/widget.go +++ b/modules/bamboohr/widget.go @@ -16,6 +16,7 @@ type Widget struct { view.TextWidget settings *Settings + items []Item } func NewWidget(app *tview.Application, settings *Settings) *Widget { @@ -37,28 +38,28 @@ func (widget *Widget) Refresh() { widget.settings.subdomain, ) - todayItems := client.Away( + widget.items = client.Away( "timeOff", time.Now().Local().Format(wtf.DateFormat), time.Now().Local().Format(wtf.DateFormat), ) - widget.Redraw(widget.CommonSettings().Title, widget.contentFrom(todayItems), false) + widget.RedrawFunc(widget.content) } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) contentFrom(items []Item) string { - if len(items) == 0 { - return fmt.Sprintf("\n\n\n\n\n\n\n\n%s", utils.CenterText("[grey]no one[white]", 50)) - } - +func (widget *Widget) content() (string, string, bool) { str := "" - for _, item := range items { - str += widget.format(item) + if len(widget.items) == 0 { + str = fmt.Sprintf("\n\n\n\n\n\n\n\n%s", utils.CenterText("[grey]no one[white]", 50)) + } else { + for _, item := range widget.items { + str += widget.format(item) + } } - return str + return widget.CommonSettings().Title, str, false } func (widget *Widget) format(item Item) string { diff --git a/modules/circleci/widget.go b/modules/circleci/widget.go index e10a216f..e6d619e8 100644 --- a/modules/circleci/widget.go +++ b/modules/circleci/widget.go @@ -32,41 +32,38 @@ func (widget *Widget) Refresh() { return } - builds, err := widget.Client.BuildsFor() - - title := fmt.Sprintf("%s - Builds", widget.CommonSettings().Title) - var content string - wrap := false - if err != nil { - wrap = true - content = err.Error() - } else { - content = widget.contentFrom(builds) - } - - widget.Redraw(title, content, wrap) + widget.RedrawFunc(widget.content) } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) contentFrom(builds []*Build) string { - var str string - for idx, build := range builds { - if idx > 10 { - return str - } +func (widget *Widget) content() (string, string, bool) { + builds, err := widget.Client.BuildsFor() - str += fmt.Sprintf( - "[%s] %s-%d (%s) [white]%s\n", - buildColor(build), - build.Reponame, - build.BuildNum, - build.Branch, - build.AuthorName, - ) + title := fmt.Sprintf("%s - Builds", widget.CommonSettings().Title) + var str string + wrap := false + if err != nil { + wrap = true + str = err.Error() + } else { + for idx, build := range builds { + if idx > 10 { + break + } + + str += fmt.Sprintf( + "[%s] %s-%d (%s) [white]%s\n", + buildColor(build), + build.Reponame, + build.BuildNum, + build.Branch, + build.AuthorName, + ) + } } - return str + return title, str, wrap } func buildColor(build *Build) string { diff --git a/modules/cmdrunner/widget.go b/modules/cmdrunner/widget.go index 367ed76e..41daa412 100644 --- a/modules/cmdrunner/widget.go +++ b/modules/cmdrunner/widget.go @@ -33,8 +33,7 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { return &widget } -// Refresh executes the command and updates the view with the results -func (widget *Widget) Refresh() { +func (widget *Widget) content() (string, string, bool) { result := widget.execute() ansiTitle := tview.TranslateANSI(widget.CommonSettings().Title) @@ -43,7 +42,12 @@ func (widget *Widget) Refresh() { } ansiResult := tview.TranslateANSI(result) - widget.Redraw(ansiTitle, ansiResult, false) + return ansiTitle, ansiResult, false +} + +// Refresh executes the command and updates the view with the results +func (widget *Widget) Refresh() { + widget.RedrawFunc(widget.content) } // String returns the string representation of the widget diff --git a/modules/cryptoexchanges/bittrex/display.go b/modules/cryptoexchanges/bittrex/display.go index a69fc467..256df503 100644 --- a/modules/cryptoexchanges/bittrex/display.go +++ b/modules/cryptoexchanges/bittrex/display.go @@ -7,16 +7,15 @@ import ( ) func (widget *Widget) display() { - if ok == false { - widget.Redraw(widget.CommonSettings().Title, errorText, true) - return - } - summaryText := widget.summaryText(&widget.summaryList) - widget.Redraw(widget.CommonSettings().Title, summaryText, false) + widget.RedrawFunc(widget.content) } -func (widget *Widget) summaryText(list *summaryList) string { +func (widget *Widget) content() (string, string, bool) { + if ok == false { + return widget.CommonSettings().Title, errorText, true + } + list := &widget.summaryList str := "" for _, baseCurrency := range list.items { @@ -62,7 +61,7 @@ func (widget *Widget) summaryText(list *summaryList) string { } - return str + return widget.CommonSettings().Title, str, true } diff --git a/modules/cryptoexchanges/blockfolio/widget.go b/modules/cryptoexchanges/blockfolio/widget.go index a4079607..4ad5e1cf 100644 --- a/modules/cryptoexchanges/blockfolio/widget.go +++ b/modules/cryptoexchanges/blockfolio/widget.go @@ -32,19 +32,18 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - positions, err := Fetch(widget.device_token) - if err != nil { - widget.Redraw(widget.CommonSettings().Title, err.Error(), true) - return - } - content := widget.contentFrom(positions) - - widget.Redraw(widget.CommonSettings().Title, content, false) + widget.RedrawFunc(widget.content) } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) contentFrom(positions *AllPositionsResponse) string { +func (widget *Widget) content() (string, string, bool) { + positions, err := Fetch(widget.device_token) + title := widget.CommonSettings().Title + if err != nil { + return title, err.Error(), true + } + res := "" totalFiat := float32(0.0) @@ -86,7 +85,7 @@ func (widget *Widget) contentFrom(positions *AllPositionsResponse) string { res += fmt.Sprintf("\n[%s]Total value: $%.3fk", "green", totalFiat/1000) } - return res + return title, res, true } //always the same diff --git a/modules/cryptoexchanges/cryptolive/widget.go b/modules/cryptoexchanges/cryptolive/widget.go index 5cc01e6a..98691d1d 100644 --- a/modules/cryptoexchanges/cryptolive/widget.go +++ b/modules/cryptoexchanges/cryptolive/widget.go @@ -46,15 +46,15 @@ func (widget *Widget) Refresh() { widget.toplistWidget.Refresh(&wg) wg.Wait() - widget.display() + widget.RedrawFunc(widget.content) } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) display() { +func (widget *Widget) content() (string, string, bool) { str := "" str += widget.priceWidget.Result str += widget.toplistWidget.Result - widget.Redraw(widget.CommonSettings().Title, fmt.Sprintf("\n%s", str), false) + return widget.CommonSettings().Title, fmt.Sprintf("\n%s", str), false } diff --git a/modules/datadog/widget.go b/modules/datadog/widget.go index b1cf5e23..e3619802 100644 --- a/modules/datadog/widget.go +++ b/modules/datadog/widget.go @@ -60,8 +60,7 @@ func (widget *Widget) Refresh() { } func (widget *Widget) Render() { - content := widget.contentFrom(widget.monitors) - widget.Redraw(widget.CommonSettings().Title, content, false) + widget.RedrawFunc(widget.content) } func (widget *Widget) HelpText() string { @@ -70,7 +69,8 @@ func (widget *Widget) HelpText() string { /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) contentFrom(triggeredMonitors []datadog.Monitor) string { +func (widget *Widget) content() (string, string, bool) { + triggeredMonitors := widget.monitors var str string if len(triggeredMonitors) > 0 { @@ -93,7 +93,7 @@ func (widget *Widget) contentFrom(triggeredMonitors []datadog.Monitor) string { ) } - return str + return widget.CommonSettings().Title, str, false } func (widget *Widget) openItem() { diff --git a/modules/feedreader/widget.go b/modules/feedreader/widget.go index 85eb77d8..b700d517 100644 --- a/modules/feedreader/widget.go +++ b/modules/feedreader/widget.go @@ -91,8 +91,7 @@ func (widget *Widget) Render() { return } - title := widget.CommonSettings().Title - widget.Redraw(title, widget.contentFrom(widget.stories), false) + widget.RedrawFunc(widget.content) } /* -------------------- Unexported Functions -------------------- */ @@ -123,7 +122,9 @@ func (widget *Widget) fetchForFeed(feedURL string) ([]*FeedItem, error) { return feedItems, nil } -func (widget *Widget) contentFrom(data []*FeedItem) string { +func (widget *Widget) content() (string, string, bool) { + data := widget.stories + title := widget.CommonSettings().Title var str string for idx, feedItem := range data { @@ -147,7 +148,7 @@ func (widget *Widget) contentFrom(data []*FeedItem) string { str += utils.HighlightableHelper(widget.View, row, idx, len(feedItem.item.Title)) } - return str + return title, str, true } // feedItems are sorted by published date diff --git a/modules/gcal/display.go b/modules/gcal/display.go index 4d1d45ed..85874c9e 100644 --- a/modules/gcal/display.go +++ b/modules/gcal/display.go @@ -25,16 +25,14 @@ func (widget *Widget) sortedEvents() ([]*CalEvent, []*CalEvent) { } func (widget *Widget) display() { - if widget.calEvents == nil || len(widget.calEvents) == 0 { - return - } - - widget.TextWidget.Redraw(widget.settings.common.Title, widget.contentFrom(widget.calEvents), false) + widget.RedrawFunc(widget.content) } -func (widget *Widget) contentFrom(calEvents []*CalEvent) string { +func (widget *Widget) content() (string, string, bool) { + title := widget.settings.common.Title + calEvents := widget.calEvents if (calEvents == nil) || (len(calEvents) == 0) { - return "No calendar events" + return title, "No calendar events", false } var str string @@ -76,7 +74,7 @@ func (widget *Widget) contentFrom(calEvents []*CalEvent) string { prevEvent = calEvent } - return str + return title, str, false } func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string { diff --git a/modules/gerrit/display.go b/modules/gerrit/display.go index c09226c1..e7ba5161 100644 --- a/modules/gerrit/display.go +++ b/modules/gerrit/display.go @@ -5,11 +5,14 @@ import ( ) func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { project := widget.currentGerritProject() if project == nil { - widget.Redraw(widget.CommonSettings().Title, "Gerrit project data is unavailable", true) - return + return widget.CommonSettings().Title, "Gerrit project data is unavailable", true } title := fmt.Sprintf("%s- %s", widget.CommonSettings().Title, widget.title(project)) @@ -25,7 +28,7 @@ func (widget *Widget) display() { str += " [red]My Outgoing Reviews[white]\n" str += widget.displayMyOutgoingReviews(project, widget.settings.username) - widget.Redraw(title, str, false) + return title, str, false } func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username string) string { diff --git a/modules/git/display.go b/modules/git/display.go index 263f1f3d..f7d9a30e 100644 --- a/modules/git/display.go +++ b/modules/git/display.go @@ -7,10 +7,13 @@ import ( ) func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { repoData := widget.currentData() if repoData == nil { - widget.Redraw(widget.CommonSettings().Title, " Git repo data is unavailable ", false) - return + return widget.CommonSettings().Title, " Git repo data is unavailable ", false } title := fmt.Sprintf("%s - [green]%s[white]", widget.CommonSettings().Title, repoData.Repository) @@ -24,7 +27,7 @@ func (widget *Widget) display() { str += "\n" str += widget.formatCommits(repoData.Commits) - widget.Redraw(title, str, false) + return title, str, false } func (widget *Widget) formatChanges(data []string) string { diff --git a/modules/github/display.go b/modules/github/display.go index 3fd10508..94634648 100644 --- a/modules/github/display.go +++ b/modules/github/display.go @@ -7,11 +7,14 @@ import ( ) func (widget *Widget) display() { + widget.TextWidget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { repo := widget.currentGithubRepo() title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.title(repo)) if repo == nil { - widget.TextWidget.Redraw(title, " GitHub repo data is unavailable ", false) - return + return title, " GitHub repo data is unavailable ", false } _, _, width, _ := widget.View.GetRect() @@ -27,7 +30,7 @@ func (widget *Widget) display() { str += widget.displayCustomQuery(repo, customQuery.filter, customQuery.perPage) } - widget.TextWidget.Redraw(title, str, false) + return title, str, false } func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) string { diff --git a/modules/gitlab/display.go b/modules/gitlab/display.go index fca1796d..11b8d98d 100644 --- a/modules/gitlab/display.go +++ b/modules/gitlab/display.go @@ -5,11 +5,14 @@ import ( ) func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { project := widget.currentGitlabProject() if project == nil { - widget.Redraw(widget.CommonSettings().Title, " Gitlab project data is unavailable ", true) - return + return widget.CommonSettings().Title, " Gitlab project data is unavailable ", true } title := fmt.Sprintf("%s- %s", widget.CommonSettings().Title, widget.title(project)) @@ -24,7 +27,8 @@ func (widget *Widget) display() { str += "\n" str += " [red]My Merge Requests[white]\n" str += widget.displayMyMergeRequests(project, widget.settings.username) - widget.Redraw(title, str, false) + + return title, str, false } func (widget *Widget) displayMyMergeRequests(project *GitlabProject, username string) string { diff --git a/modules/gitter/widget.go b/modules/gitter/widget.go index db8c55e4..38624819 100644 --- a/modules/gitter/widget.go +++ b/modules/gitter/widget.go @@ -72,18 +72,16 @@ func (widget *Widget) HelpText() string { /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) display() { - if widget.messages == nil { - return - } - - title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.settings.roomURI) - - widget.Redraw(title, widget.contentFrom(widget.messages), true) + widget.RedrawFunc(widget.content) } -func (widget *Widget) contentFrom(messages []Message) string { +func (widget *Widget) content() (string, string, bool) { + title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.settings.roomURI) + if widget.messages == nil || len(widget.messages) == 0 { + return title, "No Messages To Display", false + } var str string - for idx, message := range messages { + for idx, message := range widget.messages { row := fmt.Sprintf( `[%s] [blue]%s [lightslategray]%s: [%s]%s [aqua]%s`, widget.RowColor(idx), @@ -97,7 +95,7 @@ func (widget *Widget) contentFrom(messages []Message) string { str += utils.HighlightableHelper(widget.View, row, idx, len(message.Text)) } - return str + return title, str, true } func (widget *Widget) openMessage() { diff --git a/modules/hackernews/widget.go b/modules/hackernews/widget.go index d25b357c..c49fafe3 100644 --- a/modules/hackernews/widget.go +++ b/modules/hackernews/widget.go @@ -67,17 +67,17 @@ func (widget *Widget) Refresh() { // Render sets up the widget data for redrawing to the screen func (widget *Widget) Render() { - if widget.stories == nil { - return - } - - title := fmt.Sprintf("%s - %s stories", widget.CommonSettings().Title, widget.settings.storyType) - widget.Redraw(title, widget.contentFrom(widget.stories), false) + widget.RedrawFunc(widget.content) } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) contentFrom(stories []Story) string { +func (widget *Widget) content() (string, string, bool) { + title := fmt.Sprintf("%s - %s stories", widget.CommonSettings().Title, widget.settings.storyType) + stories := widget.stories + if stories == nil || len(stories) == 0 { + return title, "No stories to display", false + } var str string for idx, story := range stories { @@ -94,7 +94,7 @@ func (widget *Widget) contentFrom(stories []Story) string { str += utils.HighlightableHelper(widget.View, row, idx, len(story.Title)) } - return str + return title, str, false } func (widget *Widget) openStory() { diff --git a/modules/ipaddresses/ipapi/widget.go b/modules/ipaddresses/ipapi/widget.go index 158adc1f..d25b4c01 100644 --- a/modules/ipaddresses/ipapi/widget.go +++ b/modules/ipaddresses/ipapi/widget.go @@ -52,7 +52,7 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { func (widget *Widget) Refresh() { widget.ipinfo() - widget.Redraw(widget.CommonSettings().Title, widget.result, false) + widget.RedrawFunc(func() (string, string, bool) { return widget.CommonSettings().Title, widget.result, false }) } //this method reads the config and calls ipinfo for ip information diff --git a/modules/ipaddresses/ipinfo/widget.go b/modules/ipaddresses/ipinfo/widget.go index 8f09f919..d6797cd5 100644 --- a/modules/ipaddresses/ipinfo/widget.go +++ b/modules/ipaddresses/ipinfo/widget.go @@ -44,7 +44,7 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { func (widget *Widget) Refresh() { widget.ipinfo() - widget.TextWidget.Redraw(widget.CommonSettings().Title, widget.result, false) + widget.RedrawFunc(func() (string, string, bool) { return widget.CommonSettings().Title, widget.result, false }) } //this method reads the config and calls ipinfo for ip information diff --git a/modules/jenkins/widget.go b/modules/jenkins/widget.go index 74c39f88..334d3957 100644 --- a/modules/jenkins/widget.go +++ b/modules/jenkins/widget.go @@ -2,7 +2,6 @@ package jenkins import ( "fmt" - "github.com/rivo/tview" "github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/view" @@ -60,18 +59,18 @@ func (widget *Widget) Refresh() { /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) Render() { - if widget.view == nil { - return - } - - title := fmt.Sprintf("%s: [red]%s", widget.CommonSettings().Title, widget.view.Name) - - widget.Redraw(title, widget.contentFrom(widget.view), false) + widget.RedrawFunc(widget.content) } -func (widget *Widget) contentFrom(view *View) string { +func (widget *Widget) content() (string, string, bool) { + title := fmt.Sprintf("%s: [red]%s", widget.CommonSettings().Title, widget.view.Name) + if widget.view == nil { + return title, "No content to display", false + } + var str string - for idx, job := range view.Jobs { + jobs := widget.view.Jobs + for idx, job := range jobs { row := fmt.Sprintf( `[%s] [%s]%-6s[white]`, @@ -83,7 +82,7 @@ func (widget *Widget) contentFrom(view *View) string { str += utils.HighlightableHelper(widget.View, row, idx, len(job.Name)) } - return str + return title, str, false } func (widget *Widget) jobColor(job *Job) string { diff --git a/modules/mercurial/display.go b/modules/mercurial/display.go index 18b1a45d..28ffdc7e 100644 --- a/modules/mercurial/display.go +++ b/modules/mercurial/display.go @@ -7,10 +7,13 @@ import ( ) func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { repoData := widget.currentData() if repoData == nil { - widget.Redraw(widget.CommonSettings().Title, " Mercurial repo data is unavailable ", false) - return + return widget.CommonSettings().Title, " Mercurial repo data is unavailable ", false } title := fmt.Sprintf("%s - [green]%s[white]", widget.CommonSettings().Title, repoData.Repository) @@ -24,7 +27,7 @@ func (widget *Widget) display() { str += "\n" str += widget.formatCommits(repoData.Commits) - widget.Redraw(title, str, false) + return title, str, false } func (widget *Widget) formatChanges(data []string) string { diff --git a/modules/nbascore/widget.go b/modules/nbascore/widget.go index 68bc476f..b01b5750 100644 --- a/modules/nbascore/widget.go +++ b/modules/nbascore/widget.go @@ -45,36 +45,37 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * } func (widget *Widget) Refresh() { - widget.Redraw(widget.CommonSettings().Title, widget.nbascore(), false) + widget.RedrawFunc(widget.nbascore) } func (widget *Widget) HelpText() string { return widget.KeyboardWidget.HelpText() } -func (widget *Widget) nbascore() string { +func (widget *Widget) nbascore() (string, string, bool) { + title := widget.CommonSettings().Title cur := time.Now().AddDate(0, 0, offset) // Go back/forward offset days curString := cur.Format("20060102") // Need 20060102 format to feed to api client := &http.Client{} req, err := http.NewRequest("GET", "http://data.nba.net/10s/prod/v1/"+curString+"/scoreboard.json", nil) if err != nil { - return err.Error() + return title, err.Error(), true } req.Header.Set("Accept-Language", widget.language) req.Header.Set("User-Agent", "curl") response, err := client.Do(req) if err != nil { - return err.Error() + return title, err.Error(), true } defer response.Body.Close() if response.StatusCode != 200 { - return err.Error() + return title, err.Error(), true } // Get data from data.nba.net and check if successful contents, err := ioutil.ReadAll(response.Body) if err != nil { - return err.Error() + return title, err.Error(), true } result := map[string]interface{}{} json.Unmarshal(contents, &result) @@ -135,5 +136,5 @@ func (widget *Widget) nbascore() string { } allGame += fmt.Sprintf("%s%5s%v[white] %s %3s [white]vs %s%-3s %s\n", qColor, "Q", quarter, vTeam, vScore, hColor, hScore, hTeam) // Format the score and store in allgame } - return allGame + return title, allGame, false } diff --git a/modules/newrelic/widget.go b/modules/newrelic/widget.go index c35b1d69..210e82e3 100644 --- a/modules/newrelic/widget.go +++ b/modules/newrelic/widget.go @@ -7,7 +7,6 @@ import ( "github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/wtf" - nr "github.com/yfronto/newrelic" ) type Widget struct { @@ -32,6 +31,12 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { + widget.RedrawFunc(widget.content) +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *Widget) content() (string, string, bool) { app, appErr := widget.client.Application() deploys, depErr := widget.client.Deployments() @@ -47,49 +52,41 @@ func (widget *Widget) Refresh() { wrap = true content = depErr.Error() } else { - content = widget.contentFrom(deploys) - } + content += fmt.Sprintf( + " %s\n", + "[red]Latest Deploys[white]", + ) - widget.Redraw(title, content, wrap) -} + revisions := []string{} -/* -------------------- Unexported Functions -------------------- */ + for _, deploy := range deploys { + if (deploy.Revision != "") && utils.DoesNotInclude(revisions, deploy.Revision) { + lineColor := "white" + if wtf.IsToday(deploy.Timestamp) { + lineColor = "lightblue" + } -func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string { - str := fmt.Sprintf( - " %s\n", - "[red]Latest Deploys[white]", - ) + revLen := 8 + if revLen > len(deploy.Revision) { + revLen = len(deploy.Revision) + } - revisions := []string{} + content += fmt.Sprintf( + " [green]%s[%s] %s %-.16s[white]\n", + deploy.Revision[0:revLen], + lineColor, + deploy.Timestamp.Format("Jan 02 15:04 MST"), + utils.NameFromEmail(deploy.User), + ) - for _, deploy := range deploys { - if (deploy.Revision != "") && utils.DoesNotInclude(revisions, deploy.Revision) { - lineColor := "white" - if wtf.IsToday(deploy.Timestamp) { - lineColor = "lightblue" - } + revisions = append(revisions, deploy.Revision) - revLen := 8 - if revLen > len(deploy.Revision) { - revLen = len(deploy.Revision) - } - - str += fmt.Sprintf( - " [green]%s[%s] %s %-.16s[white]\n", - deploy.Revision[0:revLen], - lineColor, - deploy.Timestamp.Format("Jan 02 15:04 MST"), - utils.NameFromEmail(deploy.User), - ) - - revisions = append(revisions, deploy.Revision) - - if len(revisions) == widget.settings.deployCount { - break + if len(revisions) == widget.settings.deployCount { + break + } } } } - return str + return title, content, wrap } diff --git a/modules/opsgenie/widget.go b/modules/opsgenie/widget.go index 33ec957d..ef0ca66f 100644 --- a/modules/opsgenie/widget.go +++ b/modules/opsgenie/widget.go @@ -28,10 +28,17 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - data, err := widget.Fetch( + widget.RedrawFunc(widget.content) +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *Widget) content() (string, string, bool) { + onCallResponses, err := widget.Fetch( widget.settings.scheduleIdentifierType, widget.settings.schedule, ) + title := widget.CommonSettings().Title var content string wrap := false @@ -39,34 +46,25 @@ func (widget *Widget) Refresh() { wrap = true content = err.Error() } else { - content = widget.contentFrom(data) + + for _, data := range onCallResponses { + if (len(data.OnCallData.Recipients) == 0) && (widget.settings.displayEmpty == false) { + continue + } + + var msg string + if len(data.OnCallData.Recipients) == 0 { + msg = " [gray]no one[white]\n\n" + } else { + msg = fmt.Sprintf(" %s\n\n", strings.Join(utils.NamesFromEmails(data.OnCallData.Recipients), ", ")) + } + + content += widget.cleanScheduleName(data.OnCallData.Parent.Name) + content += msg + } } - widget.Redraw(widget.CommonSettings().Title, content, wrap) -} - -/* -------------------- Unexported Functions -------------------- */ - -func (widget *Widget) contentFrom(onCallResponses []*OnCallResponse) string { - str := "" - - for _, data := range onCallResponses { - if (len(data.OnCallData.Recipients) == 0) && (widget.settings.displayEmpty == false) { - continue - } - - var msg string - if len(data.OnCallData.Recipients) == 0 { - msg = " [gray]no one[white]\n\n" - } else { - msg = fmt.Sprintf(" %s\n\n", strings.Join(utils.NamesFromEmails(data.OnCallData.Recipients), ", ")) - } - - str += widget.cleanScheduleName(data.OnCallData.Parent.Name) - str += msg - } - - return str + return title, content, wrap } func (widget *Widget) cleanScheduleName(schedule string) string { diff --git a/modules/power/widget.go b/modules/power/widget.go index 3254ab65..15d0bd5b 100644 --- a/modules/power/widget.go +++ b/modules/power/widget.go @@ -29,12 +29,14 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { return &widget } -func (widget *Widget) Refresh() { - widget.Battery.Refresh() - +func (widget *Widget) content() (string, string, bool) { content := fmt.Sprintf(" %10s: %s\n", "Source", powerSource()) content += "\n" content += widget.Battery.String() - - widget.Redraw(widget.CommonSettings().Title, content, true) + return widget.CommonSettings().Title, content, true +} + +func (widget *Widget) Refresh() { + widget.Battery.Refresh() + widget.RedrawFunc(widget.content) } diff --git a/modules/rollbar/widget.go b/modules/rollbar/widget.go index 64ca8bca..700a3442 100644 --- a/modules/rollbar/widget.go +++ b/modules/rollbar/widget.go @@ -65,13 +65,14 @@ func (widget *Widget) Render() { return } - title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.settings.projectName) - widget.Redraw(title, widget.contentFrom(widget.items), false) + widget.RedrawFunc(widget.content) } -func (widget *Widget) contentFrom(result *Result) string { +func (widget *Widget) content() (string, string, bool) { + title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.settings.projectName) + result := widget.items if result == nil { - return "No results" + return title, "No results", false } var str string if len(result.Items) > widget.settings.count { @@ -94,7 +95,7 @@ func (widget *Widget) contentFrom(result *Result) string { str += utils.HighlightableHelper(widget.View, row, idx, len(item.Title)) } - return str + return title, str, false } func statusColor(item *Item) string { diff --git a/modules/security/widget.go b/modules/security/widget.go index 0ae6ac9c..ed18f2e3 100644 --- a/modules/security/widget.go +++ b/modules/security/widget.go @@ -32,15 +32,14 @@ func (widget *Widget) Refresh() { return } - data := NewSecurityData() - data.Fetch() - - widget.Redraw(widget.CommonSettings().Title, widget.contentFrom(data), false) + widget.RedrawFunc(widget.content) } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) contentFrom(data *SecurityData) string { +func (widget *Widget) content() (string, string, bool) { + data := NewSecurityData() + data.Fetch() str := " [red]WiFi[white]\n" str += fmt.Sprintf(" %8s: %s\n", "Network", data.WifiName) str += fmt.Sprintf(" %8s: %s\n", "Crypto", data.WifiEncryption) @@ -60,7 +59,7 @@ func (widget *Widget) contentFrom(data *SecurityData) string { str += fmt.Sprintf(" %12s\n", data.DnsAt(1)) str += "\n" - return str + return widget.CommonSettings().Title, str, false } func (widget *Widget) labelColor(label string) string { diff --git a/modules/spotify/widget.go b/modules/spotify/widget.go index 1e957326..afaa5aa8 100644 --- a/modules/spotify/widget.go +++ b/modules/spotify/widget.go @@ -51,28 +51,23 @@ func (w *Widget) refreshSpotifyInfos() error { } func (w *Widget) Refresh() { - w.render() + w.RedrawFunc(w.createOutput) } func (widget *Widget) HelpText() string { return widget.KeyboardWidget.HelpText() } -func (w *Widget) render() { - err := w.refreshSpotifyInfos() +func (w *Widget) createOutput() (string, string, bool) { var content string + err := w.refreshSpotifyInfos() if err != nil { content = err.Error() } else { - content = w.createOutput() + content = utils.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width) + content += utils.CenterText(fmt.Sprintf("[green]Title:[white] %v\n ", w.Info.Title), w.CommonSettings().Width) + content += utils.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artist), w.CommonSettings().Width) + content += utils.CenterText(fmt.Sprintf("[green]%v:[white] %v\n", w.Info.TrackNumber, w.Info.Album), w.CommonSettings().Width) } - w.Redraw(w.CommonSettings().Title, content, true) -} - -func (w *Widget) createOutput() string { - output := utils.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width) - output += utils.CenterText(fmt.Sprintf("[green]Title:[white] %v\n ", w.Info.Title), w.CommonSettings().Width) - output += utils.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artist), w.CommonSettings().Width) - output += utils.CenterText(fmt.Sprintf("[green]%v:[white] %v\n", w.Info.TrackNumber, w.Info.Album), w.CommonSettings().Width) - return output + return w.CommonSettings().Title, content, true } diff --git a/modules/spotifyweb/widget.go b/modules/spotifyweb/widget.go index 00149488..f707cd9a 100644 --- a/modules/spotifyweb/widget.go +++ b/modules/spotifyweb/widget.go @@ -154,27 +154,28 @@ func (w *Widget) refreshSpotifyInfos() error { // Refresh refreshes the current view of the widget func (w *Widget) Refresh() { - err := w.refreshSpotifyInfos() - if err != nil { - w.Redraw(w.CommonSettings().Title, err.Error(), true) - } else { - w.Redraw(w.CommonSettings().Title, w.createOutput(), false) - } + w.RedrawFunc(w.createOutput) } func (widget *Widget) HelpText() string { return widget.KeyboardWidget.HelpText() } -func (w *Widget) createOutput() string { - output := utils.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width) - output += utils.CenterText(fmt.Sprintf("[green]Title:[white] %v\n", w.Info.Title), w.CommonSettings().Width) - output += utils.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artists), w.CommonSettings().Width) - output += utils.CenterText(fmt.Sprintf("[green]Album:[white] %v\n", w.Info.Album), w.CommonSettings().Width) - if w.playerState.ShuffleState { - output += utils.CenterText(fmt.Sprintf("[green]Shuffle:[white] on\n"), w.CommonSettings().Width) +func (w *Widget) createOutput() (string, string, bool) { + err := w.refreshSpotifyInfos() + var output string + if err != nil { + output = err.Error() } else { - output += utils.CenterText(fmt.Sprintf("[green]Shuffle:[white] off\n"), w.CommonSettings().Width) + output := utils.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width) + output += utils.CenterText(fmt.Sprintf("[green]Title:[white] %v\n", w.Info.Title), w.CommonSettings().Width) + output += utils.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artists), w.CommonSettings().Width) + output += utils.CenterText(fmt.Sprintf("[green]Album:[white] %v\n", w.Info.Album), w.CommonSettings().Width) + if w.playerState.ShuffleState { + output += utils.CenterText(fmt.Sprintf("[green]Shuffle:[white] on\n"), w.CommonSettings().Width) + } else { + output += utils.CenterText(fmt.Sprintf("[green]Shuffle:[white] off\n"), w.CommonSettings().Width) + } } - return output + return w.CommonSettings().Title, output, true } diff --git a/modules/status/widget.go b/modules/status/widget.go index 69981beb..f5cc1bc8 100644 --- a/modules/status/widget.go +++ b/modules/status/widget.go @@ -28,12 +28,12 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - widget.Redraw(widget.CommonSettings().Title, widget.animation(), false) + widget.RedrawFunc(widget.animation) } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) animation() string { +func (widget *Widget) animation() (string, string, bool) { icons := []string{"|", "/", "-", "\\", "|"} next := icons[widget.CurrentIcon] @@ -42,5 +42,5 @@ func (widget *Widget) animation() string { widget.CurrentIcon = 0 } - return next + return widget.CommonSettings().Title, next, false } diff --git a/modules/system/widget.go b/modules/system/widget.go index b6b4923a..8258e070 100644 --- a/modules/system/widget.go +++ b/modules/system/widget.go @@ -34,7 +34,7 @@ func NewWidget(app *tview.Application, date, version string, settings *Settings) return &widget } -func (widget *Widget) Refresh() { +func (widget *Widget) display() (string, string, bool) { content := fmt.Sprintf( "%8s: %s\n%8s: %s\n\n%8s: %s\n%8s: %s", "Built", @@ -46,7 +46,12 @@ func (widget *Widget) Refresh() { "Build", widget.systemInfo.BuildVersion, ) - widget.Redraw(widget.CommonSettings().Title, content, false) + + return widget.CommonSettings().Title, content, false +} + +func (widget *Widget) Refresh() { + widget.RedrawFunc(widget.display) } func (widget *Widget) prettyDate() string { diff --git a/modules/textfile/widget.go b/modules/textfile/widget.go index 9e68d96b..ebcfc62d 100644 --- a/modules/textfile/widget.go +++ b/modules/textfile/widget.go @@ -71,6 +71,10 @@ func (widget *Widget) HelpText() string { /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { title := fmt.Sprintf("[green]%s[white]", widget.CurrentSource()) _, _, width, _ := widget.View.GetRect() @@ -82,7 +86,7 @@ func (widget *Widget) display() { text += widget.plainText() } - widget.Redraw(title, text, widget.settings.wrapText) + return title, text, widget.settings.wrapText } func (widget *Widget) fileName() string { diff --git a/modules/todo/display.go b/modules/todo/display.go index cb4dc4b2..8cef871d 100644 --- a/modules/todo/display.go +++ b/modules/todo/display.go @@ -9,6 +9,10 @@ import ( ) func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { str := "" newList := checklist.NewChecklist( widget.settings.common.Sigils.Checkbox.Checked, @@ -31,7 +35,7 @@ func (widget *Widget) display() { newList.SetSelectedByItem(widget.list.SelectedItem()) widget.SetList(newList) - widget.Redraw(widget.CommonSettings().Title, str, false) + return widget.CommonSettings().Title, str, false } func (widget *Widget) formattedItemLine(idx int, item *checklist.ChecklistItem, selectedItem *checklist.ChecklistItem, maxLen int) string { diff --git a/modules/todoist/display.go b/modules/todoist/display.go index 81d707dd..e5ed4d7a 100644 --- a/modules/todoist/display.go +++ b/modules/todoist/display.go @@ -7,14 +7,15 @@ import ( "github.com/wtfutil/wtf/utils" ) -func (widget *Widget) display() { +func (widget *Widget) content() (string, string, bool) { proj := widget.CurrentProject() if proj == nil { - return + return widget.CommonSettings().Title, "", false } title := fmt.Sprintf("[green]%s[white]", proj.Project.Name) + str := "" for idx, item := range proj.tasks { @@ -27,6 +28,9 @@ func (widget *Widget) display() { str += utils.HighlightableHelper(widget.View, row, idx, len(item.Content)) } - - widget.ScrollableWidget.Redraw(title, str, false) + return title, str, false +} + +func (widget *Widget) display() { + widget.ScrollableWidget.RedrawFunc(widget.content) } diff --git a/modules/transmission/display.go b/modules/transmission/display.go index 5d0849bd..a8c0234b 100644 --- a/modules/transmission/display.go +++ b/modules/transmission/display.go @@ -9,7 +9,8 @@ import ( "github.com/wtfutil/wtf/utils" ) -func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string { +func (widget *Widget) content() (string, string, bool) { + data := widget.torrents str := "" for idx, torrent := range data { @@ -27,12 +28,11 @@ func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string { str += utils.HighlightableHelper(widget.View, row, idx, len(torrName)) } - return str + return widget.CommonSettings().Title, str, false } func (widget *Widget) display() { - content := widget.contentFrom(widget.torrents) - widget.ScrollableWidget.Redraw(widget.CommonSettings().Title, content, false) + widget.ScrollableWidget.RedrawFunc(widget.content) } func (widget *Widget) prettyTorrentName(name string) string { diff --git a/modules/travisci/widget.go b/modules/travisci/widget.go index b85592b9..a878dc9e 100644 --- a/modules/travisci/widget.go +++ b/modules/travisci/widget.go @@ -59,13 +59,13 @@ func (widget *Widget) Render() { return } - title := fmt.Sprintf("%s - Builds", widget.CommonSettings().Title) - widget.Redraw(title, widget.contentFrom(widget.builds), false) + widget.RedrawFunc(widget.content) } -func (widget *Widget) contentFrom(builds *Builds) string { +func (widget *Widget) content() (string, string, bool) { + title := fmt.Sprintf("%s - Builds", widget.CommonSettings().Title) var str string - for idx, build := range builds.Builds { + for idx, build := range widget.builds.Builds { row := fmt.Sprintf( "[%s] [%s] %s-%s (%s) [%s]%s - [blue]%s\n", @@ -81,7 +81,7 @@ func (widget *Widget) contentFrom(builds *Builds) string { str += utils.HighlightableHelper(widget.View, row, idx, len(build.Branch.Name)) } - return str + return title, str, false } func buildColor(build *Build) string { diff --git a/modules/trello/widget.go b/modules/trello/widget.go index 5dcb217c..3ee2e436 100644 --- a/modules/trello/widget.go +++ b/modules/trello/widget.go @@ -27,6 +27,13 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { + widget.RedrawFunc(widget.content) +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *Widget) content() (string, string, bool) { + client := trello.NewClient( widget.settings.apiKey, widget.settings.accessToken, @@ -41,7 +48,7 @@ func (widget *Widget) Refresh() { ) var title string - var content string + content := "" wrap := false if err != nil { @@ -49,31 +56,20 @@ func (widget *Widget) Refresh() { title = widget.CommonSettings().Title content = err.Error() } else { - widget.View.SetWrap(false) title = fmt.Sprintf( "[white]%s: [green]%s ", widget.CommonSettings().Title, widget.settings.board, ) - content = widget.contentFrom(searchResult) - } + for list, cardArray := range searchResult.TrelloCards { + content += fmt.Sprintf(" [red]%s[white]\n", list) - widget.Redraw(title, content, wrap) -} - -/* -------------------- Unexported Functions -------------------- */ - -func (widget *Widget) contentFrom(searchResult *SearchResult) string { - str := "" - - for list, cardArray := range searchResult.TrelloCards { - str += fmt.Sprintf(" [red]%s[white]\n", list) - - for _, card := range cardArray { - str += fmt.Sprintf(" %s[white]\n", card.Name) + for _, card := range cardArray { + content += fmt.Sprintf(" %s[white]\n", card.Name) + } + content = fmt.Sprintf("%s\n", content) } - str = fmt.Sprintf("%s\n", str) } - return str + return title, content, wrap } diff --git a/modules/twitter/widget.go b/modules/twitter/widget.go index 766f837c..69c3ca77 100644 --- a/modules/twitter/widget.go +++ b/modules/twitter/widget.go @@ -62,6 +62,10 @@ func (widget *Widget) HelpText() string { /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { widget.client.screenName = widget.CurrentSource() tweets := widget.client.Tweets() @@ -69,8 +73,7 @@ func (widget *Widget) display() { if len(tweets) == 0 { str := fmt.Sprintf("\n\n\n%s", utils.CenterText("[lightblue]No Tweets[white]", 50)) - widget.Redraw(title, str, true) - return + return title, str, true } _, _, width, _ := widget.View.GetRect() @@ -79,7 +82,7 @@ func (widget *Widget) display() { str += widget.format(tweet) } - widget.Redraw(title, str, true) + return title, str, true } // If the tweet's Username is the same as the account we're watching, no diff --git a/modules/unknown/widget.go b/modules/unknown/widget.go index 2e53475f..c09fd733 100644 --- a/modules/unknown/widget.go +++ b/modules/unknown/widget.go @@ -27,5 +27,5 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { func (widget *Widget) Refresh() { content := fmt.Sprintf("Widget %s and/or type %s does not exist", widget.Name(), widget.CommonSettings().Module.Type) - widget.Redraw(widget.CommonSettings().Title, content, true) + widget.RedrawFunc(func() (string, string, bool) { return widget.CommonSettings().Title, content, true }) } diff --git a/modules/weatherservices/prettyweather/widget.go b/modules/weatherservices/prettyweather/widget.go index f98a7791..5a8f639c 100644 --- a/modules/weatherservices/prettyweather/widget.go +++ b/modules/weatherservices/prettyweather/widget.go @@ -30,7 +30,7 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { func (widget *Widget) Refresh() { widget.prettyWeather() - widget.Redraw(widget.CommonSettings().Title, widget.result, false) + widget.RedrawFunc(func() (string, string, bool) { return widget.CommonSettings().Title, widget.result, false }) } //this method reads the config and calls wttr.in for pretty weather diff --git a/modules/weatherservices/weather/display.go b/modules/weatherservices/weather/display.go index 377a4b70..613fc609 100644 --- a/modules/weatherservices/weather/display.go +++ b/modules/weatherservices/weather/display.go @@ -9,6 +9,10 @@ import ( ) func (widget *Widget) display() { + widget.RedrawFunc(widget.content) +} + +func (widget *Widget) content() (string, string, bool) { var err string if widget.apiKeyValid() == false { @@ -40,7 +44,7 @@ func (widget *Widget) display() { content += widget.sunInfo(cityData) } - widget.Redraw(title, content, setWrap) + return title, content, setWrap } func (widget *Widget) description(cityData *owm.CurrentWeatherData) string { diff --git a/modules/zendesk/widget.go b/modules/zendesk/widget.go index 9c3ae53f..eb12a759 100644 --- a/modules/zendesk/widget.go +++ b/modules/zendesk/widget.go @@ -53,13 +53,14 @@ func (widget *Widget) Refresh() { /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) Render() { - title := fmt.Sprintf("%s (%d)", widget.CommonSettings().Title, widget.result.Count) - widget.Redraw(title, widget.textContent(widget.result.Tickets), false) + widget.RedrawFunc(widget.content) } -func (widget *Widget) textContent(items []Ticket) string { +func (widget *Widget) content() (string, string, bool) { + title := fmt.Sprintf("%s (%d)", widget.CommonSettings().Title, widget.result.Count) + items := widget.result.Tickets if len(items) == 0 { - return fmt.Sprintf("No unassigned tickets in queue - woop!!") + return title, "No unassigned tickets in queue - woop!!", false } str := "" @@ -67,7 +68,7 @@ func (widget *Widget) textContent(items []Ticket) string { str += widget.format(data, idx) } - return str + return title, str, false } func (widget *Widget) format(ticket Ticket, idx int) string { diff --git a/view/text_widget.go b/view/text_widget.go index 4f691f1c..9b32f500 100644 --- a/view/text_widget.go +++ b/view/text_widget.go @@ -38,6 +38,16 @@ func (widget *TextWidget) Redraw(title, text string, wrap bool) { }) } +func (widget *TextWidget) RedrawFunc(data func() (string, string, bool)) { + widget.app.QueueUpdateDraw(func() { + title, content, wrap := data() + widget.View.Clear() + widget.View.SetWrap(wrap) + widget.View.SetTitle(widget.ContextualTitle(title)) + widget.View.SetText(content) + }) +} + /* -------------------- Unexported Functions -------------------- */ func (widget *TextWidget) createView(bordered bool) *tview.TextView {