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

     1  package github
     2  
     3  import (
     4  	"fmt"
     5  
     6  	ghb "github.com/google/go-github/v32/github"
     7  )
     8  
     9  func (widget *Widget) display() {
    10  	widget.TextWidget.Redraw(widget.content)
    11  }
    12  
    13  func (widget *Widget) content() (string, string, bool) {
    14  	repo := widget.currentGithubRepo()
    15  	username := widget.settings.username
    16  
    17  	// Choses the correct place to scroll to when changing sources
    18  	if len(widget.View.GetHighlights()) > 0 {
    19  		widget.View.ScrollToHighlight()
    20  	} else {
    21  		widget.View.ScrollToBeginning()
    22  	}
    23  
    24  	// initial maxItems count
    25  	widget.Items = make([]int, 0)
    26  	widget.SetItemCount(len(repo.myReviewRequests((username))))
    27  
    28  	title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.title(repo))
    29  	if repo == nil {
    30  		return title, " GitHub repo data is unavailable ", false
    31  	} else if repo.Err != nil {
    32  		return title, repo.Err.Error(), true
    33  	}
    34  
    35  	_, _, width, _ := widget.View.GetRect()
    36  	str := widget.settings.PaginationMarker(len(widget.GithubRepos), widget.Idx, width)
    37  	if widget.settings.showStats {
    38  		str += fmt.Sprintf("\n [%s]Stats[white]\n", widget.settings.Colors.Subheading)
    39  		str += widget.displayStats(repo)
    40  	}
    41  	if widget.settings.showOpenReviewRequests {
    42  		str += fmt.Sprintf("\n [%s]Open Review Requests[white]\n", widget.settings.Colors.Subheading)
    43  		str += widget.displayMyReviewRequests(repo, username)
    44  	}
    45  	if widget.settings.showMyPullRequests {
    46  		str += fmt.Sprintf("\n [%s]My Pull Requests[white]\n", widget.settings.Colors.Subheading)
    47  		str += widget.displayMyPullRequests(repo, username)
    48  	}
    49  	for _, customQuery := range widget.settings.customQueries {
    50  		str += fmt.Sprintf("\n [%s]%s[white]\n", widget.settings.Colors.Subheading, customQuery.title)
    51  		str += widget.displayCustomQuery(repo, customQuery.filter, customQuery.perPage)
    52  	}
    53  
    54  	return title, str, false
    55  }
    56  
    57  func (widget *Widget) displayMyPullRequests(repo *Repo, username string) string {
    58  	prs := repo.myPullRequests(username, widget.settings.enableStatus)
    59  
    60  	prLength := len(prs)
    61  
    62  	if prLength == 0 {
    63  		return " [grey]none[white]\n"
    64  	}
    65  
    66  	maxItems := widget.GetItemCount()
    67  
    68  	str := ""
    69  	for idx, pr := range prs {
    70  		str += fmt.Sprintf(` %s[green]["%d"]%4d[""][white] %s`, widget.mergeString(pr), maxItems+idx, *pr.Number, *pr.Title)
    71  		str += "\n"
    72  		widget.Items = append(widget.Items, *pr.Number)
    73  	}
    74  
    75  	widget.SetItemCount(maxItems + prLength)
    76  
    77  	return str
    78  }
    79  
    80  func (widget *Widget) displayCustomQuery(repo *Repo, filter string, perPage int) string {
    81  	res := repo.customIssueQuery(filter, perPage)
    82  
    83  	if res == nil {
    84  		return " [grey]Invalid Query[white]\n"
    85  	}
    86  
    87  	issuesLength := len(res.Issues)
    88  
    89  	if issuesLength == 0 {
    90  		return " [grey]none[white]\n"
    91  	}
    92  
    93  	maxItems := widget.GetItemCount()
    94  
    95  	str := ""
    96  	for idx, issue := range res.Issues {
    97  		str += fmt.Sprintf(` [green]["%d"]%4d[""][white] %s`, maxItems+idx, *issue.Number, *issue.Title)
    98  		str += "\n"
    99  		widget.Items = append(widget.Items, *issue.Number)
   100  	}
   101  
   102  	widget.SetItemCount(maxItems + issuesLength)
   103  
   104  	return str
   105  }
   106  
   107  func (widget *Widget) displayMyReviewRequests(repo *Repo, username string) string {
   108  	prs := repo.myReviewRequests(username)
   109  
   110  	if len(prs) == 0 {
   111  		return " [grey]none[white]\n"
   112  	}
   113  
   114  	str := ""
   115  	for idx, pr := range prs {
   116  		str += fmt.Sprintf(` [green]["%d"]%4d[""][white] %s`, idx, *pr.Number, *pr.Title)
   117  		str += "\n"
   118  		widget.Items = append(widget.Items, *pr.Number)
   119  	}
   120  
   121  	return str
   122  }
   123  
   124  func (widget *Widget) displayStats(repo *Repo) string {
   125  	locPrinter, err := widget.settings.LocalizedPrinter()
   126  	if err != nil {
   127  		return err.Error()
   128  	}
   129  
   130  	str := fmt.Sprintf(
   131  		" PRs: %s  Issues: %s  Stars: %s\n",
   132  		locPrinter.Sprintf("%d", repo.PullRequestCount()),
   133  		locPrinter.Sprintf("%d", repo.IssueCount()),
   134  		locPrinter.Sprintf("%d", repo.StarCount()),
   135  	)
   136  
   137  	return str
   138  }
   139  
   140  func (widget *Widget) title(repo *Repo) string {
   141  	return fmt.Sprintf(
   142  		"[%s]%s - %s[white]",
   143  		widget.settings.Colors.TextTheme.Title,
   144  		repo.Owner,
   145  		repo.Name,
   146  	)
   147  }
   148  
   149  var mergeIcons = map[string]string{
   150  	"dirty":    "[red]\u0021[white] ",
   151  	"clean":    "[green]\u2713[white] ",
   152  	"unstable": "[red]\u2717[white] ",
   153  	"blocked":  "[red]\u2717[white] ",
   154  }
   155  
   156  func (widget *Widget) mergeString(pr *ghb.PullRequest) string {
   157  	if !widget.settings.enableStatus {
   158  		return ""
   159  	}
   160  	if str, ok := mergeIcons[pr.GetMergeableState()]; ok {
   161  		return str
   162  	}
   163  	return "? "
   164  }