github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/shared/display.go (about)

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/cli/cli/api"
     8  	"github.com/cli/cli/pkg/iostreams"
     9  	"github.com/cli/cli/utils"
    10  )
    11  
    12  func StateTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string {
    13  	prStateColorFunc := cs.ColorFromString(ColorForPR(pr))
    14  
    15  	if pr.State == "OPEN" && pr.IsDraft {
    16  		return prStateColorFunc(strings.Title(strings.ToLower("Draft")))
    17  	}
    18  	return prStateColorFunc(strings.Title(strings.ToLower(pr.State)))
    19  }
    20  
    21  func ColorForPR(pr api.PullRequest) string {
    22  	if pr.State == "OPEN" && pr.IsDraft {
    23  		return "gray"
    24  	}
    25  	return ColorForState(pr.State)
    26  }
    27  
    28  // ColorForState returns a color constant for a PR/Issue state
    29  func ColorForState(state string) string {
    30  	switch state {
    31  	case "OPEN":
    32  		return "green"
    33  	case "CLOSED":
    34  		return "red"
    35  	case "MERGED":
    36  		return "magenta"
    37  	default:
    38  		return ""
    39  	}
    40  }
    41  
    42  func PrintHeader(io *iostreams.IOStreams, s string) {
    43  	fmt.Fprintln(io.Out, io.ColorScheme().Bold(s))
    44  }
    45  
    46  func PrintMessage(io *iostreams.IOStreams, s string) {
    47  	fmt.Fprintln(io.Out, io.ColorScheme().Gray(s))
    48  }
    49  
    50  func ListHeader(repoName string, itemName string, matchCount int, totalMatchCount int, hasFilters bool) string {
    51  	if totalMatchCount == 0 {
    52  		if hasFilters {
    53  			return fmt.Sprintf("No %ss match your search in %s", itemName, repoName)
    54  		}
    55  		return fmt.Sprintf("There are no open %ss in %s", itemName, repoName)
    56  	}
    57  
    58  	if hasFilters {
    59  		matchVerb := "match"
    60  		if totalMatchCount == 1 {
    61  			matchVerb = "matches"
    62  		}
    63  		return fmt.Sprintf("Showing %d of %s in %s that %s your search", matchCount, utils.Pluralize(totalMatchCount, itemName), repoName, matchVerb)
    64  	}
    65  
    66  	return fmt.Sprintf("Showing %d of %s in %s", matchCount, utils.Pluralize(totalMatchCount, fmt.Sprintf("open %s", itemName)), repoName)
    67  }