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/gitter/widget.go b/modules/gitter/widget.go index db8c55e4..e1467841 100644 --- a/modules/gitter/widget.go +++ b/modules/gitter/widget.go @@ -76,14 +76,13 @@ func (widget *Widget) display() { 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) 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 +96,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..a07bf2eb 100644 --- a/modules/hackernews/widget.go +++ b/modules/hackernews/widget.go @@ -71,13 +71,14 @@ func (widget *Widget) Render() { 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 var str string for idx, story := range stories { @@ -94,7 +95,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/jenkins/widget.go b/modules/jenkins/widget.go index 74c39f88..20b21a56 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" @@ -64,14 +63,15 @@ func (widget *Widget) Render() { 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) + 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 +83,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/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/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/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/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 {