github.com/wtfutil/wtf@v0.43.0/modules/circleci/widget.go (about)

     1  package circleci
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rivo/tview"
     7  	"github.com/wtfutil/wtf/view"
     8  )
     9  
    10  type Widget struct {
    11  	view.TextWidget
    12  	*Client
    13  
    14  	settings *Settings
    15  }
    16  
    17  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
    18  	widget := Widget{
    19  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common),
    20  		Client:     NewClient(settings.apiKey),
    21  
    22  		settings: settings,
    23  	}
    24  
    25  	return &widget
    26  }
    27  
    28  /* -------------------- Exported Functions -------------------- */
    29  
    30  func (widget *Widget) Refresh() {
    31  	if widget.Disabled() {
    32  		return
    33  	}
    34  
    35  	widget.Redraw(widget.content)
    36  }
    37  
    38  /* -------------------- Unexported Functions -------------------- */
    39  
    40  func (widget *Widget) content() (string, string, bool) {
    41  	builds, err := widget.Client.BuildsFor()
    42  
    43  	title := fmt.Sprintf("%s - Builds", widget.CommonSettings().Title)
    44  	var str string
    45  	wrap := false
    46  	if err != nil {
    47  		wrap = true
    48  		str = err.Error()
    49  	} else {
    50  		for idx, build := range builds {
    51  			if idx > widget.settings.numberOfBuilds {
    52  				break
    53  			}
    54  
    55  			str += fmt.Sprintf(
    56  				"[%s] %s-%d (%s) [white]%s\n",
    57  				buildColor(build),
    58  				build.Reponame,
    59  				build.BuildNum,
    60  				build.Branch,
    61  				build.AuthorName,
    62  			)
    63  		}
    64  	}
    65  
    66  	return title, str, wrap
    67  }
    68  
    69  func buildColor(build *Build) string {
    70  	switch build.Status {
    71  	case "failed":
    72  		return "red"
    73  	case "running":
    74  		return "yellow"
    75  	case "success":
    76  		return "green"
    77  	case "fixed":
    78  		return "green"
    79  	default:
    80  		return "white"
    81  	}
    82  }