1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/circleci/widget.go
2018-06-10 03:54:58 +03:00

84 lines
1.4 KiB
Go

package circleci
import (
"fmt"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
var Config *config.Config
type Widget struct {
wtf.TextWidget
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" CircleCI ", "circleci", false),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
builds, err := BuildsFor()
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf("%s - Builds", widget.Name))
if err != nil {
widget.View.SetWrap(true)
fmt.Fprintf(widget.View, "%v", err)
} else {
widget.View.SetWrap(false)
widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(builds)))
}
}
/* -------------------- 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(b *Build) string {
var color string
switch b.Status {
case "failed":
color = "red"
case "running":
color = "yellow"
case "success":
color = "green"
default:
color = "white"
}
return color
}