1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/circleci/widget.go
Sean Smith 17d53ca39a Update title handling so that config based title can be used
There is already default handling of title. Remove instances where people are unknowingly overriding it
For instances where we want to set special things, make sure to use CommonSettings.Title, so people can still override
2019-04-30 07:13:52 -07:00

90 lines
1.5 KiB
Go

package circleci
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
*Client
app *tview.Application
settings *Settings
}
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, settings.common, false),
Client: NewClient(settings.apiKey),
app: app,
settings: settings,
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
builds, err := widget.Client.BuildsFor()
var content string
if err != nil {
widget.View.SetWrap(true)
content = err.Error()
} else {
widget.View.SetWrap(false)
content = widget.contentFrom(builds)
}
widget.app.QueueUpdateDraw(func() {
widget.View.SetTitle(fmt.Sprintf("%s - Builds", widget.CommonSettings.Title))
widget.View.SetText(content)
})
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(builds []*Build) string {
var str string
for idx, build := range builds {
if idx > 10 {
return str
}
str = str + fmt.Sprintf(
"[%s] %s-%d (%s) [white]%s\n",
buildColor(build),
build.Reponame,
build.BuildNum,
build.Branch,
build.AuthorName,
)
}
return str
}
func buildColor(build *Build) string {
switch build.Status {
case "failed":
return "red"
case "running":
return "yellow"
case "success":
return "green"
case "fixed":
return "green"
default:
return "white"
}
}