Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions command/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ var issueViewCmd = &cobra.Command{

func issueList(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
palette, err := utils.NewPalette(cmd)
if err != nil {
return err
}

apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -126,7 +131,7 @@ func issueList(cmd *cobra.Command, args []string) error {
if userSetFlags {
msg = "No issues match your search"
}
printMessage(colorErr, msg)
printMessage(colorErr, palette, msg)
return nil
}

Expand All @@ -141,9 +146,9 @@ func issueList(cmd *cobra.Command, args []string) error {
if labels != "" && table.IsTTY() {
labels = fmt.Sprintf("(%s)", labels)
}
table.AddField(issueNum, nil, colorFuncForState(issue.State))
table.AddField(issueNum, nil, colorFuncForState(issue.State, palette))
table.AddField(replaceExcessiveWhitespace(issue.Title), nil, nil)
table.AddField(labels, nil, utils.Gray)
table.AddField(labels, nil, palette.Gray)
table.EndRow()
}
table.Render()
Expand All @@ -153,6 +158,11 @@ func issueList(cmd *cobra.Command, args []string) error {

func issueStatus(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
palette, err := utils.NewPalette(cmd)
if err != nil {
return err
}

apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
Expand All @@ -179,28 +189,28 @@ func issueStatus(cmd *cobra.Command, args []string) error {
fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(*baseRepo))
fmt.Fprintln(out, "")

printHeader(out, "Issues assigned to you")
printHeader(out, palette, "Issues assigned to you")
if issuePayload.Assigned.TotalCount > 0 {
printIssues(out, " ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues)
printIssues(out, palette, " ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues)
} else {
message := fmt.Sprintf(" There are no issues assigned to you")
printMessage(out, message)
printMessage(out, palette, message)
}
fmt.Fprintln(out)

printHeader(out, "Issues mentioning you")
printHeader(out, palette, "Issues mentioning you")
if issuePayload.Mentioned.TotalCount > 0 {
printIssues(out, " ", issuePayload.Mentioned.TotalCount, issuePayload.Mentioned.Issues)
printIssues(out, palette, " ", issuePayload.Mentioned.TotalCount, issuePayload.Mentioned.Issues)
} else {
printMessage(out, " There are no issues mentioning you")
printMessage(out, palette, " There are no issues mentioning you")
}
fmt.Fprintln(out)

printHeader(out, "Issues opened by you")
printHeader(out, palette, "Issues opened by you")
if issuePayload.Authored.TotalCount > 0 {
printIssues(out, " ", issuePayload.Authored.TotalCount, issuePayload.Authored.Issues)
printIssues(out, palette, " ", issuePayload.Authored.TotalCount, issuePayload.Authored.Issues)
} else {
printMessage(out, " There are no issues opened by you")
printMessage(out, palette, " There are no issues opened by you")
}
fmt.Fprintln(out)

Expand All @@ -209,6 +219,10 @@ func issueStatus(cmd *cobra.Command, args []string) error {

func issueView(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
palette, err := utils.NewPalette(cmd)
if err != nil {
return err
}

apiClient, err := apiClientForContext(ctx)
if err != nil {
Expand All @@ -233,22 +247,22 @@ func issueView(cmd *cobra.Command, args []string) error {

if preview {
out := colorableOut(cmd)
return printIssuePreview(out, issue)
return printIssuePreview(out, palette, issue)
} else {
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
return utils.OpenInBrowser(openURL)
}

}

func printIssuePreview(out io.Writer, issue *api.Issue) error {
func printIssuePreview(out io.Writer, palette *utils.Palette, issue *api.Issue) error {
coloredLabels := labelList(*issue)
if coloredLabels != "" {
coloredLabels = utils.Gray(fmt.Sprintf("(%s)", coloredLabels))
coloredLabels = palette.Gray(fmt.Sprintf("(%s)", coloredLabels))
}

fmt.Fprintln(out, utils.Bold(issue.Title))
fmt.Fprintln(out, utils.Gray(fmt.Sprintf(
fmt.Fprintln(out, palette.Bold(issue.Title))
fmt.Fprintln(out, palette.Gray(fmt.Sprintf(
"opened by %s. %s. %s",
issue.Author.Login,
utils.Pluralize(issue.Comments.TotalCount, "comment"),
Expand All @@ -264,8 +278,8 @@ func printIssuePreview(out io.Writer, issue *api.Issue) error {
fmt.Fprintln(out, md)
fmt.Fprintln(out)
}
fmt.Fprintf(out, palette.Gray("View this issue on GitHub: %s\n"), issue.URL)

fmt.Fprintf(out, utils.Gray("View this issue on GitHub: %s\n"), issue.URL)
return nil
}

Expand Down Expand Up @@ -395,12 +409,12 @@ func issueCreate(cmd *cobra.Command, args []string) error {
return nil
}

func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue) {
func printIssues(w io.Writer, palette *utils.Palette, prefix string, totalCount int, issues []api.Issue) {
for _, issue := range issues {
number := utils.Green("#" + strconv.Itoa(issue.Number))
number := palette.Green("#" + strconv.Itoa(issue.Number))
coloredLabels := labelList(issue)
if coloredLabels != "" {
coloredLabels = utils.Gray(fmt.Sprintf(" (%s)", coloredLabels))
coloredLabels = palette.Gray(fmt.Sprintf(" (%s)", coloredLabels))
}

now := time.Now()
Expand All @@ -409,11 +423,11 @@ func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue)
fmt.Fprintf(w, "%s%s %s%s %s\n", prefix, number,
truncate(70, replaceExcessiveWhitespace(issue.Title)),
coloredLabels,
utils.Gray(utils.FuzzyAgo(ago)))
palette.Gray(utils.FuzzyAgo(ago)))
}
remaining := totalCount - len(issues)
if remaining > 0 {
fmt.Fprintf(w, utils.Gray("%sAnd %d more\n"), prefix, remaining)
fmt.Fprintf(w, palette.Gray("%sAnd %d more\n"), prefix, remaining)
}
}

Expand Down
91 changes: 54 additions & 37 deletions command/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ branch is opened.`,

func prStatus(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)

palette, err := utils.NewPalette(cmd)
if err != nil {
return err
}

apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -95,28 +101,28 @@ func prStatus(cmd *cobra.Command, args []string) error {
fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(*baseRepo))
fmt.Fprintln(out, "")

printHeader(out, "Current branch")
printHeader(out, palette, "Current branch")
if prPayload.CurrentPR != nil {
printPrs(out, 0, *prPayload.CurrentPR)
printPrs(out, palette, 0, *prPayload.CurrentPR)
} else {
message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]"))
printMessage(out, message)
message := fmt.Sprintf(" There is no pull request associated with %s", palette.Cyan("["+currentPRHeadRef+"]"))
printMessage(out, palette, message)
}
fmt.Fprintln(out)

printHeader(out, "Created by you")
printHeader(out, palette, "Created by you")
if prPayload.ViewerCreated.TotalCount > 0 {
printPrs(out, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...)
printPrs(out, palette, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...)
} else {
printMessage(out, " You have no open pull requests")
printMessage(out, palette, " You have no open pull requests")
}
fmt.Fprintln(out)

printHeader(out, "Requesting a code review from you")
printHeader(out, palette, "Requesting a code review from you")
if prPayload.ReviewRequested.TotalCount > 0 {
printPrs(out, prPayload.ReviewRequested.TotalCount, prPayload.ReviewRequested.PullRequests...)
printPrs(out, palette, prPayload.ReviewRequested.TotalCount, prPayload.ReviewRequested.PullRequests...)
} else {
printMessage(out, " You have no pull requests to review")
printMessage(out, palette, " You have no pull requests to review")
}
fmt.Fprintln(out)

Expand All @@ -125,6 +131,12 @@ func prStatus(cmd *cobra.Command, args []string) error {

func prList(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)

palette, err := utils.NewPalette(cmd)
if err != nil {
return err
}

apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -203,7 +215,7 @@ func prList(cmd *cobra.Command, args []string) error {
if userSetFlags {
msg = "No pull requests match your search"
}
printMessage(colorErr, msg)
printMessage(colorErr, palette, msg)
return nil
}

Expand All @@ -213,9 +225,9 @@ func prList(cmd *cobra.Command, args []string) error {
if table.IsTTY() {
prNum = "#" + prNum
}
table.AddField(prNum, nil, colorFuncForState(pr.State))
table.AddField(prNum, nil, colorFuncForState(pr.State, palette))
table.AddField(replaceExcessiveWhitespace(pr.Title), nil, nil)
table.AddField(pr.HeadLabel(), nil, utils.Cyan)
table.AddField(pr.HeadLabel(), nil, palette.Cyan)
table.EndRow()
}
err = table.Render()
Expand All @@ -226,14 +238,14 @@ func prList(cmd *cobra.Command, args []string) error {
return nil
}

func colorFuncForState(state string) func(string) string {
func colorFuncForState(state string, palette *utils.Palette) func(string) string {
switch state {
case "OPEN":
return utils.Green
return palette.Green
case "CLOSED":
return utils.Red
return palette.Red
case "MERGED":
return utils.Magenta
return palette.Magenta
default:
return nil
}
Expand All @@ -242,6 +254,11 @@ func colorFuncForState(state string) func(string) string {
func prView(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)

palette, err := utils.NewPalette(cmd)
if err != nil {
return err
}

apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -291,16 +308,16 @@ func prView(cmd *cobra.Command, args []string) error {

if preview {
out := colorableOut(cmd)
return printPrPreview(out, pr)
return printPrPreview(out, palette, pr)
} else {
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
return utils.OpenInBrowser(openURL)
}
}

func printPrPreview(out io.Writer, pr *api.PullRequest) error {
fmt.Fprintln(out, utils.Bold(pr.Title))
fmt.Fprintln(out, utils.Gray(fmt.Sprintf(
func printPrPreview(out io.Writer, palette *utils.Palette, pr *api.PullRequest) error {
fmt.Fprintln(out, palette.Bold(pr.Title))
fmt.Fprintln(out, palette.Gray(fmt.Sprintf(
"%s wants to merge %s into %s from %s",
pr.Author.Login,
utils.Pluralize(pr.Commits.TotalCount, "commit"),
Expand All @@ -316,8 +333,8 @@ func printPrPreview(out io.Writer, pr *api.PullRequest) error {
fmt.Fprintln(out, md)
fmt.Fprintln(out)
}

fmt.Fprintf(out, utils.Gray("View this pull request on GitHub: %s\n"), pr.URL)
fmt.Fprintf(out, palette.Gray("View this pull request on GitHub: %s\n"), pr.URL)
return nil
}

Expand Down Expand Up @@ -381,10 +398,10 @@ func prSelectorForCurrentBranch(ctx context.Context) (prNumber int, prHeadRef st
return
}

func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) {
func printPrs(w io.Writer, palette *utils.Palette, totalCount int, prs ...api.PullRequest) {
for _, pr := range prs {
prNumber := fmt.Sprintf("#%d", pr.Number)
fmt.Fprintf(w, " %s %s %s", utils.Green(prNumber), truncate(50, replaceExcessiveWhitespace(pr.Title)), utils.Cyan("["+pr.HeadLabel()+"]"))
fmt.Fprintf(w, " %s %s %s", palette.Green(prNumber), truncate(50, replaceExcessiveWhitespace(pr.Title)), palette.Cyan("["+pr.HeadLabel()+"]"))

checks := pr.ChecksStatus()
reviews := pr.ReviewStatus()
Expand All @@ -396,40 +413,40 @@ func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) {
var summary string
if checks.Failing > 0 {
if checks.Failing == checks.Total {
summary = utils.Red("All checks failing")
summary = palette.Red("All checks failing")
} else {
summary = utils.Red(fmt.Sprintf("%d/%d checks failing", checks.Failing, checks.Total))
summary = palette.Red(fmt.Sprintf("%d/%d checks failing", checks.Failing, checks.Total))
}
} else if checks.Pending > 0 {
summary = utils.Yellow("Checks pending")
summary = palette.Yellow("Checks pending")
} else if checks.Passing == checks.Total {
summary = utils.Green("Checks passing")
summary = palette.Green("Checks passing")
}
fmt.Fprintf(w, " - %s", summary)
}

if reviews.ChangesRequested {
fmt.Fprintf(w, " - %s", utils.Red("Changes requested"))
fmt.Fprintf(w, " - %s", palette.Red("Changes requested"))
} else if reviews.ReviewRequired {
fmt.Fprintf(w, " - %s", utils.Yellow("Review required"))
fmt.Fprintf(w, " - %s", palette.Yellow("Review required"))
} else if reviews.Approved {
fmt.Fprintf(w, " - %s", utils.Green("Approved"))
fmt.Fprintf(w, " - %s", palette.Green("Approved"))
}

fmt.Fprint(w, "\n")
}
remaining := totalCount - len(prs)
if remaining > 0 {
fmt.Fprintf(w, utils.Gray(" And %d more\n"), remaining)
fmt.Fprintf(w, palette.Gray(" And %d more\n"), remaining)
}
}

func printHeader(w io.Writer, s string) {
fmt.Fprintln(w, utils.Bold(s))
func printHeader(w io.Writer, palette *utils.Palette, s string) {
fmt.Fprintln(w, palette.Bold(s))
}

func printMessage(w io.Writer, s string) {
fmt.Fprintln(w, utils.Gray(s))
func printMessage(w io.Writer, palette *utils.Palette, s string) {
fmt.Fprintln(w, palette.Gray(s))
}

func truncate(maxLength int, title string) string {
Expand Down
Loading