github.com/wtfutil/wtf@v0.43.0/modules/cds/queue/widget.go (about) 1 package cdsqueue 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/ovh/cds/sdk" 8 "github.com/ovh/cds/sdk/cdsclient" 9 "github.com/rivo/tview" 10 "github.com/wtfutil/wtf/utils" 11 "github.com/wtfutil/wtf/view" 12 ) 13 14 // Widget define wtf widget to register widget later 15 type Widget struct { 16 view.MultiSourceWidget 17 view.TextWidget 18 19 filters []string 20 21 client cdsclient.Interface 22 23 settings *Settings 24 Selected int 25 maxItems int 26 Items []sdk.WorkflowNodeJobRun 27 } 28 29 // NewWidget creates a new instance of the widget 30 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 31 widget := Widget{ 32 MultiSourceWidget: view.NewMultiSourceWidget(settings.Common, "workflow", "workflows"), 33 TextWidget: view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common), 34 35 settings: settings, 36 } 37 38 widget.initializeKeyboardControls() 39 widget.View.SetRegions(true) 40 widget.SetDisplayFunction(widget.display) 41 42 widget.Unselect() 43 widget.filters = []string{sdk.StatusWaiting, sdk.StatusBuilding} 44 45 widget.client = cdsclient.New(cdsclient.Config{ 46 Host: settings.apiURL, 47 BuitinConsumerAuthenticationToken: settings.token, 48 }) 49 50 config, _ := widget.client.ConfigUser() 51 52 if config.URLUI != "" { 53 widget.settings.uiURL = config.URLUI 54 } 55 56 return &widget 57 } 58 59 /* -------------------- Exported Functions -------------------- */ 60 61 // SetItemCount sets the amount of workflows throughout the widgets display creation 62 func (widget *Widget) SetItemCount(items int) { 63 widget.maxItems = items 64 } 65 66 // GetItemCount returns the amount of workflows calculated so far as an int 67 func (widget *Widget) GetItemCount() int { 68 return widget.maxItems 69 } 70 71 // GetSelected returns the index of the currently highlighted item as an int 72 func (widget *Widget) GetSelected() int { 73 if widget.Selected < 0 { 74 return 0 75 } 76 return widget.Selected 77 } 78 79 // Next cycles the currently highlighted text down 80 func (widget *Widget) Next() { 81 widget.Selected++ 82 if widget.Selected >= widget.maxItems { 83 widget.Selected = 0 84 } 85 widget.View.Highlight(strconv.Itoa(widget.Selected)) 86 widget.View.ScrollToHighlight() 87 } 88 89 // Prev cycles the currently highlighted text up 90 func (widget *Widget) Prev() { 91 widget.Selected-- 92 if widget.Selected < 0 { 93 widget.Selected = widget.maxItems - 1 94 } 95 widget.View.Highlight(strconv.Itoa(widget.Selected)) 96 widget.View.ScrollToHighlight() 97 } 98 99 // Unselect stops highlighting the text and jumps the scroll position to the top 100 func (widget *Widget) Unselect() { 101 widget.Selected = -1 102 widget.View.Highlight() 103 widget.View.ScrollToBeginning() 104 } 105 106 // Refresh reloads the data 107 func (widget *Widget) Refresh() { 108 widget.display() 109 } 110 111 /* -------------------- Unexported Functions -------------------- */ 112 113 func (widget *Widget) currentFilter() string { 114 if len(widget.filters) == 0 { 115 return sdk.StatusWaiting 116 } 117 118 if widget.Idx < 0 || widget.Idx >= len(widget.filters) { 119 widget.Idx = 0 120 return sdk.StatusWaiting 121 } 122 123 return widget.filters[widget.Idx] 124 } 125 126 func (widget *Widget) openWorkflow() { 127 currentSelection := widget.View.GetHighlights() 128 if widget.Selected >= 0 && currentSelection[0] != "" { 129 job := widget.Items[widget.Selected] 130 prj := getVarsInPbj("cds.project", job.Parameters) 131 workflow := getVarsInPbj("cds.workflow", job.Parameters) 132 runNumber := getVarsInPbj("cds.run.number", job.Parameters) 133 url := fmt.Sprintf("%s/project/%s/workflow/%s/run/%s", widget.settings.uiURL, prj, workflow, runNumber) 134 utils.OpenFile(url) 135 } 136 }