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

     1  package cdsfavorites
     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  	workflows []sdk.Workflow
    20  
    21  	client cdsclient.Interface
    22  
    23  	settings *Settings
    24  	Selected int
    25  	maxItems int
    26  	Items    []int64
    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  
    44  	widget.client = cdsclient.New(cdsclient.Config{
    45  		Host:                              settings.apiURL,
    46  		BuitinConsumerAuthenticationToken: settings.token,
    47  	})
    48  
    49  	config, _ := widget.client.ConfigUser()
    50  
    51  	if config.URLUI != "" {
    52  		widget.settings.uiURL = config.URLUI
    53  	}
    54  
    55  	widget.workflows = widget.buildWorkflowsCollection()
    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) buildWorkflowsCollection() []sdk.Workflow {
   114  	workflows := []sdk.Workflow{}
   115  	data, _ := widget.client.Navbar()
   116  	for _, v := range data {
   117  		if v.Favorite && v.WorkflowName != "" {
   118  			workflows = append(workflows, sdk.Workflow{ProjectKey: v.Key, Name: v.WorkflowName})
   119  		}
   120  	}
   121  	return workflows
   122  }
   123  
   124  func (widget *Widget) currentCDSWorkflow() *sdk.Workflow {
   125  	if len(widget.workflows) == 0 {
   126  		return nil
   127  	}
   128  
   129  	if widget.Idx < 0 || widget.Idx >= len(widget.workflows) {
   130  		widget.Idx = 0
   131  	}
   132  
   133  	p := widget.workflows[widget.Idx]
   134  	return &p
   135  }
   136  
   137  func (widget *Widget) openWorkflow() {
   138  	currentSelection := widget.View.GetHighlights()
   139  	if widget.Selected >= 0 && currentSelection[0] != "" {
   140  		wf := widget.currentCDSWorkflow()
   141  		url := fmt.Sprintf("%s/project/%s/workflow/%s/run/%d",
   142  			widget.settings.uiURL, wf.ProjectKey, wf.Name, widget.Items[widget.Selected])
   143  		utils.OpenFile(url)
   144  	}
   145  }