github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/shared/display.go (about)

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ungtb10d/cli/v2/api"
     7  	"github.com/ungtb10d/cli/v2/internal/text"
     8  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
     9  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    10  )
    11  
    12  func StateTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string {
    13  	prStateColorFunc := cs.ColorFromString(ColorForPRState(pr))
    14  	if pr.State == "OPEN" && pr.IsDraft {
    15  		return prStateColorFunc("Draft")
    16  	}
    17  	return prStateColorFunc(text.Title(pr.State))
    18  }
    19  
    20  func ColorForPRState(pr api.PullRequest) string {
    21  	switch pr.State {
    22  	case "OPEN":
    23  		if pr.IsDraft {
    24  			return "gray"
    25  		}
    26  		return "green"
    27  	case "CLOSED":
    28  		return "red"
    29  	case "MERGED":
    30  		return "magenta"
    31  	default:
    32  		return ""
    33  	}
    34  }
    35  
    36  func ColorForIssueState(issue api.Issue) string {
    37  	switch issue.State {
    38  	case "OPEN":
    39  		return "green"
    40  	case "CLOSED":
    41  		if issue.StateReason == "NOT_PLANNED" {
    42  			return "gray"
    43  		}
    44  		return "magenta"
    45  	default:
    46  		return ""
    47  	}
    48  }
    49  
    50  func PrintHeader(io *iostreams.IOStreams, s string) {
    51  	fmt.Fprintln(io.Out, io.ColorScheme().Bold(s))
    52  }
    53  
    54  func PrintMessage(io *iostreams.IOStreams, s string) {
    55  	fmt.Fprintln(io.Out, io.ColorScheme().Gray(s))
    56  }
    57  
    58  func ListNoResults(repoName string, itemName string, hasFilters bool) error {
    59  	if hasFilters {
    60  		return cmdutil.NewNoResultsError(fmt.Sprintf("no %ss match your search in %s", itemName, repoName))
    61  	}
    62  	return cmdutil.NewNoResultsError(fmt.Sprintf("no open %ss in %s", itemName, repoName))
    63  }
    64  
    65  func ListHeader(repoName string, itemName string, matchCount int, totalMatchCount int, hasFilters bool) string {
    66  	if hasFilters {
    67  		matchVerb := "match"
    68  		if totalMatchCount == 1 {
    69  			matchVerb = "matches"
    70  		}
    71  		return fmt.Sprintf("Showing %d of %s in %s that %s your search", matchCount, text.Pluralize(totalMatchCount, itemName), repoName, matchVerb)
    72  	}
    73  
    74  	return fmt.Sprintf("Showing %d of %s in %s", matchCount, text.Pluralize(totalMatchCount, fmt.Sprintf("open %s", itemName)), repoName)
    75  }
    76  
    77  func PrCheckStatusSummaryWithColor(cs *iostreams.ColorScheme, checks api.PullRequestChecksStatus) string {
    78  	var summary = cs.Gray("No checks")
    79  	if checks.Total > 0 {
    80  		if checks.Failing > 0 {
    81  			if checks.Failing == checks.Total {
    82  				summary = cs.Red("× All checks failing")
    83  			} else {
    84  				summary = cs.Redf("× %d/%d checks failing", checks.Failing, checks.Total)
    85  			}
    86  		} else if checks.Pending > 0 {
    87  			summary = cs.Yellow("- Checks pending")
    88  		} else if checks.Passing == checks.Total {
    89  			summary = cs.Green("✓ Checks passing")
    90  		}
    91  	}
    92  	return summary
    93  }