github.com/wtfutil/wtf@v0.43.0/modules/travisci/widget.go (about) 1 package travisci 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/rivo/tview" 8 "github.com/wtfutil/wtf/utils" 9 "github.com/wtfutil/wtf/view" 10 ) 11 12 type Widget struct { 13 view.ScrollableWidget 14 15 builds *Builds 16 settings *Settings 17 err error 18 } 19 20 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 21 widget := Widget{ 22 ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common), 23 24 settings: settings, 25 } 26 27 widget.SetRenderFunction(widget.Render) 28 widget.initializeKeyboardControls() 29 30 return &widget 31 } 32 33 /* -------------------- Exported Functions -------------------- */ 34 35 func (widget *Widget) Refresh() { 36 if widget.Disabled() { 37 return 38 } 39 40 builds, err := BuildsFor(widget.settings) 41 42 if err != nil { 43 widget.err = err 44 widget.builds = nil 45 widget.SetItemCount(0) 46 } else { 47 widget.err = nil 48 widget.builds = builds 49 widget.SetItemCount(len(builds.Builds)) 50 } 51 widget.Render() 52 } 53 54 /* -------------------- Unexported Functions -------------------- */ 55 56 func (widget *Widget) Render() { 57 widget.Redraw(widget.content) 58 } 59 60 func (widget *Widget) content() (string, string, bool) { 61 title := fmt.Sprintf("%s - Builds", widget.CommonSettings().Title) 62 var str string 63 if widget.err != nil { 64 str = widget.err.Error() 65 } else { 66 var rowFormat = "[%s] [%s] %s-%s (%s) [%s]%s - [blue]%s" 67 if !widget.settings.compact { 68 rowFormat += "\n" 69 } 70 71 for idx, build := range widget.builds.Builds { 72 row := fmt.Sprintf( 73 rowFormat, 74 widget.RowColor(idx), 75 buildColor(build), 76 build.Repository.Name, 77 build.Number, 78 build.Branch.Name, 79 widget.RowColor(idx), 80 strings.Split(build.Commit.Message, "\n")[0], 81 build.CreatedBy.Login, 82 ) 83 str += utils.HighlightableHelper(widget.View, row, idx, len(build.Branch.Name)) 84 } 85 } 86 87 return title, str, false 88 } 89 90 func buildColor(build Build) string { 91 switch build.State { 92 case "broken": 93 return "red" 94 case "failed": 95 return "red" 96 case "failing": 97 return "red" 98 case "pending": 99 return "yellow" 100 case "started": 101 return "yellow" 102 case "fixed": 103 return "green" 104 case "passed": 105 return "green" 106 default: 107 return "white" 108 } 109 } 110 111 func (widget *Widget) openBuild() { 112 sel := widget.GetSelected() 113 if sel >= 0 && widget.builds != nil && sel < len(widget.builds.Builds) { 114 build := &widget.builds.Builds[sel] 115 travisHost := TRAVIS_HOSTS[widget.settings.pro] 116 utils.OpenFile(fmt.Sprintf("https://%s/%s/%s/%d", travisHost, build.Repository.Slug, "builds", build.ID)) 117 } 118 }