github.com/wtfutil/wtf@v0.43.0/modules/github/widget.go (about) 1 package github 2 3 import ( 4 "strconv" 5 "strings" 6 7 "github.com/rivo/tview" 8 "github.com/wtfutil/wtf/utils" 9 "github.com/wtfutil/wtf/view" 10 ) 11 12 // Widget define wtf widget to register widget later 13 type Widget struct { 14 view.MultiSourceWidget 15 view.TextWidget 16 17 GithubRepos []*Repo 18 19 settings *Settings 20 Selected int 21 maxItems int 22 Items []int 23 } 24 25 // NewWidget creates a new instance of the widget 26 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 27 widget := Widget{ 28 MultiSourceWidget: view.NewMultiSourceWidget(settings.Common, "repository", "repositories"), 29 TextWidget: view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common), 30 31 settings: settings, 32 } 33 34 widget.GithubRepos = widget.buildRepoCollection(widget.settings.repositories) 35 36 widget.initializeKeyboardControls() 37 38 widget.View.SetRegions(true) 39 widget.SetDisplayFunction(widget.display) 40 41 widget.Unselect() 42 43 widget.Sources = widget.settings.repositories 44 45 return &widget 46 } 47 48 /* -------------------- Exported Functions -------------------- */ 49 50 // SetItemCount sets the amount of PRs RRs and other PRs throughout the widgets display creation 51 func (widget *Widget) SetItemCount(items int) { 52 widget.maxItems = items 53 } 54 55 // GetItemCount returns the amount of PRs RRs and other PRs calculated so far as an int 56 func (widget *Widget) GetItemCount() int { 57 return widget.maxItems 58 } 59 60 // GetSelected returns the index of the currently highlighted item as an int 61 func (widget *Widget) GetSelected() int { 62 if widget.Selected < 0 { 63 return 0 64 } 65 return widget.Selected 66 } 67 68 // Next cycles the currently highlighted text down 69 func (widget *Widget) Next() { 70 widget.Selected++ 71 if widget.Selected >= widget.maxItems { 72 widget.Selected = 0 73 } 74 widget.View.Highlight(strconv.Itoa(widget.Selected)) 75 widget.View.ScrollToHighlight() 76 } 77 78 // Prev cycles the currently highlighted text up 79 func (widget *Widget) Prev() { 80 widget.Selected-- 81 if widget.Selected < 0 { 82 widget.Selected = widget.maxItems - 1 83 } 84 widget.View.Highlight(strconv.Itoa(widget.Selected)) 85 widget.View.ScrollToHighlight() 86 } 87 88 // Unselect stops highlighting the text and jumps the scroll position to the top 89 func (widget *Widget) Unselect() { 90 widget.Selected = -1 91 widget.View.Highlight() 92 widget.View.ScrollToBeginning() 93 } 94 95 // Refresh reloads the github data via the Github API and reruns the display 96 func (widget *Widget) Refresh() { 97 for _, repo := range widget.GithubRepos { 98 repo.Refresh() 99 } 100 101 widget.display() 102 } 103 104 /* -------------------- Unexported Functions -------------------- */ 105 106 func (widget *Widget) buildRepoCollection(repoData []string) []*Repo { 107 githubRepos := []*Repo{} 108 109 for _, repo := range repoData { 110 split := strings.Split(repo, "/") 111 owner, name := split[0], split[1] 112 repo := NewGithubRepo( 113 name, 114 owner, 115 widget.settings.apiKey, 116 widget.settings.baseURL, 117 widget.settings.uploadURL, 118 ) 119 120 githubRepos = append(githubRepos, repo) 121 } 122 123 return githubRepos 124 } 125 126 func (widget *Widget) currentGithubRepo() *Repo { 127 if len(widget.GithubRepos) == 0 { 128 return nil 129 } 130 131 if widget.Idx < 0 || widget.Idx >= len(widget.GithubRepos) { 132 return nil 133 } 134 135 return widget.GithubRepos[widget.Idx] 136 } 137 138 func (widget *Widget) openPr() { 139 currentSelection := widget.View.GetHighlights() 140 if widget.Selected >= 0 && len(widget.Items) > 0 && currentSelection[0] != "" { 141 url := (*widget.currentGithubRepo().RemoteRepo.HTMLURL + "/pull/" + strconv.Itoa(widget.Items[widget.Selected])) 142 utils.OpenFile(url) 143 } 144 } 145 146 func (widget *Widget) openRepo() { 147 repo := widget.currentGithubRepo() 148 149 if repo != nil { 150 repo.Open() 151 } 152 } 153 154 func (widget *Widget) openPulls() { 155 repo := widget.currentGithubRepo() 156 157 if repo != nil { 158 repo.OpenPulls() 159 } 160 } 161 162 func (widget *Widget) openIssues() { 163 repo := widget.currentGithubRepo() 164 165 if repo != nil { 166 repo.OpenIssues() 167 } 168 }