Skip to content

Commit ebb7f39

Browse files
committed
Merge remote-tracking branch 'origin/master' into ansi-colorfunc
2 parents 24b04b5 + 64dcdd6 commit ebb7f39

File tree

5 files changed

+81
-56
lines changed

5 files changed

+81
-56
lines changed

command/issue.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"strconv"
78
"strings"
@@ -21,17 +22,17 @@ func init() {
2122
"Supply a title. Will prompt for one otherwise.")
2223
issueCreateCmd.Flags().StringP("body", "b", "",
2324
"Supply a body. Will prompt for one otherwise.")
24-
issueCreateCmd.Flags().BoolP("web", "w", false, "open the web browser to create an issue")
25+
issueCreateCmd.Flags().BoolP("web", "w", false, "Open the web browser to create an issue")
2526

2627
issueListCmd := &cobra.Command{
2728
Use: "list",
2829
Short: "List and filter issues in this repository",
2930
RunE: issueList,
3031
}
31-
issueListCmd.Flags().StringP("assignee", "a", "", "filter by assignee")
32-
issueListCmd.Flags().StringSliceP("label", "l", nil, "filter by label")
33-
issueListCmd.Flags().StringP("state", "s", "", "filter by state (open|closed|all)")
34-
issueListCmd.Flags().IntP("limit", "L", 30, "maximum number of issues to fetch")
32+
issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee")
33+
issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label")
34+
issueListCmd.Flags().StringP("state", "s", "", "Filter by state (open|closed|all)")
35+
issueListCmd.Flags().IntP("limit", "L", 30, "Maximum number of issues to fetch")
3536
issueCmd.AddCommand((issueListCmd))
3637
}
3738

@@ -94,12 +95,15 @@ func issueList(cmd *cobra.Command, args []string) error {
9495
return err
9596
}
9697

98+
out := cmd.OutOrStdout()
99+
colorOut := colorableOut(cmd)
100+
97101
if len(issues) == 0 {
98-
printMessage("There are no open issues")
102+
printMessage(colorOut, "There are no open issues")
99103
return nil
100104
}
101105

102-
table := utils.NewTablePrinter(cmd.OutOrStdout())
106+
table := utils.NewTablePrinter(out)
103107
for _, issue := range issues {
104108
issueNum := strconv.Itoa(issue.Number)
105109
if table.IsTTY() {
@@ -141,30 +145,32 @@ func issueStatus(cmd *cobra.Command, args []string) error {
141145
return err
142146
}
143147

144-
printHeader("Issues assigned to you")
148+
out := colorableOut(cmd)
149+
150+
printHeader(out, "Issues assigned to you")
145151
if issuePayload.Assigned != nil {
146-
printIssues(" ", issuePayload.Assigned...)
152+
printIssues(out, " ", issuePayload.Assigned...)
147153
} else {
148154
message := fmt.Sprintf(" There are no issues assgined to you")
149-
printMessage(message)
155+
printMessage(out, message)
150156
}
151-
fmt.Println()
157+
fmt.Fprintln(out)
152158

153-
printHeader("Issues mentioning you")
159+
printHeader(out, "Issues mentioning you")
154160
if len(issuePayload.Mentioned) > 0 {
155-
printIssues(" ", issuePayload.Mentioned...)
161+
printIssues(out, " ", issuePayload.Mentioned...)
156162
} else {
157-
printMessage(" There are no issues mentioning you")
163+
printMessage(out, " There are no issues mentioning you")
158164
}
159-
fmt.Println()
165+
fmt.Fprintln(out)
160166

161-
printHeader("Issues opened by you")
167+
printHeader(out, "Issues opened by you")
162168
if len(issuePayload.Authored) > 0 {
163-
printIssues(" ", issuePayload.Authored...)
169+
printIssues(out, " ", issuePayload.Authored...)
164170
} else {
165-
printMessage(" There are no issues opened by you")
171+
printMessage(out, " There are no issues opened by you")
166172
}
167-
fmt.Println()
173+
fmt.Fprintln(out)
168174

169175
return nil
170176
}
@@ -255,14 +261,14 @@ func issueCreate(cmd *cobra.Command, args []string) error {
255261
return nil
256262
}
257263

258-
func printIssues(prefix string, issues ...api.Issue) {
264+
func printIssues(w io.Writer, prefix string, issues ...api.Issue) {
259265
for _, issue := range issues {
260266
number := utils.Green("#" + strconv.Itoa(issue.Number))
261267
coloredLabels := labelList(issue)
262268
if coloredLabels != "" {
263269
coloredLabels = utils.Gray(fmt.Sprintf(" (%s)", coloredLabels))
264270
}
265-
fmt.Printf("%s%s %s%s\n", prefix, number, truncate(70, issue.Title), coloredLabels)
271+
fmt.Fprintf(w, "%s%s %s%s\n", prefix, number, truncate(70, issue.Title), coloredLabels)
266272
}
267273
}
268274

command/pr.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"os/exec"
78
"strconv"
@@ -20,10 +21,10 @@ func init() {
2021
prCmd.AddCommand(prStatusCmd)
2122
prCmd.AddCommand(prViewCmd)
2223

23-
prListCmd.Flags().IntP("limit", "L", 30, "maximum number of items to fetch")
24-
prListCmd.Flags().StringP("state", "s", "open", "filter by state")
25-
prListCmd.Flags().StringP("base", "b", "", "filter by base branch")
26-
prListCmd.Flags().StringArrayP("label", "l", nil, "filter by label")
24+
prListCmd.Flags().IntP("limit", "L", 30, "Maximum number of items to fetch")
25+
prListCmd.Flags().StringP("state", "s", "open", "Filter by state")
26+
prListCmd.Flags().StringP("base", "B", "", "Filter by base branch")
27+
prListCmd.Flags().StringArrayP("label", "l", nil, "Filter by label")
2728
}
2829

2930
var prCmd = &cobra.Command{
@@ -78,30 +79,32 @@ func prStatus(cmd *cobra.Command, args []string) error {
7879
return err
7980
}
8081

81-
printHeader("Current branch")
82+
out := colorableOut(cmd)
83+
84+
printHeader(out, "Current branch")
8285
if prPayload.CurrentPR != nil {
83-
printPrs(*prPayload.CurrentPR)
86+
printPrs(out, *prPayload.CurrentPR)
8487
} else {
8588
message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentBranch+"]"))
86-
printMessage(message)
89+
printMessage(out, message)
8790
}
88-
fmt.Println()
91+
fmt.Fprintln(out)
8992

90-
printHeader("Created by you")
93+
printHeader(out, "Created by you")
9194
if len(prPayload.ViewerCreated) > 0 {
92-
printPrs(prPayload.ViewerCreated...)
95+
printPrs(out, prPayload.ViewerCreated...)
9396
} else {
94-
printMessage(" You have no open pull requests")
97+
printMessage(out, " You have no open pull requests")
9598
}
96-
fmt.Println()
99+
fmt.Fprintln(out)
97100

98-
printHeader("Requesting a code review from you")
101+
printHeader(out, "Requesting a code review from you")
99102
if len(prPayload.ReviewRequested) > 0 {
100-
printPrs(prPayload.ReviewRequested...)
103+
printPrs(out, prPayload.ReviewRequested...)
101104
} else {
102-
printMessage(" You have no pull requests to review")
105+
printMessage(out, " You have no pull requests to review")
103106
}
104-
fmt.Println()
107+
fmt.Fprintln(out)
105108

106109
return nil
107110
}
@@ -330,15 +333,15 @@ func prCheckout(cmd *cobra.Command, args []string) error {
330333
return nil
331334
}
332335

333-
func printPrs(prs ...api.PullRequest) {
336+
func printPrs(w io.Writer, prs ...api.PullRequest) {
334337
for _, pr := range prs {
335338
prNumber := fmt.Sprintf("#%d", pr.Number)
336-
fmt.Printf(" %s %s %s", utils.Yellow(prNumber), truncate(50, pr.Title), utils.Cyan("["+pr.HeadLabel()+"]"))
339+
fmt.Fprintf(w, " %s %s %s", utils.Yellow(prNumber), truncate(50, pr.Title), utils.Cyan("["+pr.HeadLabel()+"]"))
337340

338341
checks := pr.ChecksStatus()
339342
reviews := pr.ReviewStatus()
340343
if checks.Total > 0 || reviews.ChangesRequested || reviews.Approved {
341-
fmt.Printf("\n ")
344+
fmt.Fprintf(w, "\n ")
342345
}
343346

344347
if checks.Total > 0 {
@@ -354,27 +357,27 @@ func printPrs(prs ...api.PullRequest) {
354357
} else if checks.Passing == checks.Total {
355358
summary = utils.Green("Checks passing")
356359
}
357-
fmt.Printf(" - %s", summary)
360+
fmt.Fprintf(w, " - %s", summary)
358361
}
359362

360363
if reviews.ChangesRequested {
361-
fmt.Printf(" - %s", utils.Red("changes requested"))
364+
fmt.Fprintf(w, " - %s", utils.Red("changes requested"))
362365
} else if reviews.ReviewRequired {
363-
fmt.Printf(" - %s", utils.Yellow("review required"))
366+
fmt.Fprintf(w, " - %s", utils.Yellow("review required"))
364367
} else if reviews.Approved {
365-
fmt.Printf(" - %s", utils.Green("approved"))
368+
fmt.Fprintf(w, " - %s", utils.Green("approved"))
366369
}
367370

368-
fmt.Printf("\n")
371+
fmt.Fprint(w, "\n")
369372
}
370373
}
371374

372-
func printHeader(s string) {
373-
fmt.Println(utils.Bold(s))
375+
func printHeader(w io.Writer, s string) {
376+
fmt.Fprintln(w, utils.Bold(s))
374377
}
375378

376-
func printMessage(s string) {
377-
fmt.Println(utils.Gray(s))
379+
func printMessage(w io.Writer, s string) {
380+
fmt.Fprintln(w, utils.Gray(s))
378381
}
379382

380383
func truncate(maxLength int, title string) string {

command/root.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package command
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"strings"
78

89
"github.com/github/gh-cli/api"
910
"github.com/github/gh-cli/context"
11+
"github.com/github/gh-cli/utils"
1012

1113
"github.com/spf13/cobra"
1214
)
@@ -19,10 +21,11 @@ var BuildDate = "YYYY-MM-DD"
1921

2022
func init() {
2123
RootCmd.Version = fmt.Sprintf("%s (%s)", strings.TrimPrefix(Version, "v"), BuildDate)
22-
RootCmd.AddCommand(versionCmd)
24+
RootCmd.AddCommand(versionCmd)
2325

24-
RootCmd.PersistentFlags().StringP("repo", "R", "", "current GitHub repository")
25-
RootCmd.PersistentFlags().StringP("current-branch", "B", "", "current git branch")
26+
RootCmd.PersistentFlags().StringP("repo", "R", "", "Current GitHub repository")
27+
RootCmd.PersistentFlags().Bool("help", false, "Show help for command")
28+
RootCmd.Flags().Bool("version", false, "Print gh version")
2629
// TODO:
2730
// RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output")
2831

@@ -67,9 +70,7 @@ func contextForCommand(cmd *cobra.Command) context.Context {
6770
ctx := initContext()
6871
if repo, err := cmd.Flags().GetString("repo"); err == nil && repo != "" {
6972
ctx.SetBaseRepo(repo)
70-
}
71-
if branch, err := cmd.Flags().GetString("current-branch"); err == nil && branch != "" {
72-
ctx.SetBranch(branch)
73+
ctx.SetBranch("master")
7374
}
7475
return ctx
7576
}
@@ -93,3 +94,11 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
9394
}
9495
return api.NewClient(opts...), nil
9596
}
97+
98+
func colorableOut(cmd *cobra.Command) io.Writer {
99+
out := cmd.OutOrStdout()
100+
if outFile, isFile := out.(*os.File); isFile {
101+
return utils.NewColorable(outFile)
102+
}
103+
return out
104+
}

utils/color.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package utils
22

33
import (
4+
"io"
45
"os"
56

7+
"github.com/mattn/go-colorable"
68
"github.com/mattn/go-isatty"
79
"github.com/mgutz/ansi"
810
)
911

12+
// NewColorable returns an output stream that handles ANSI color sequences on Windows
13+
func NewColorable(f *os.File) io.Writer {
14+
return colorable.NewColorable(f)
15+
}
16+
1017
func makeColorFunc(color string) func(string) string {
1118
cf := ansi.ColorFunc(color)
1219
return func(arg string) string {

utils/table_printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewTablePrinter(w io.Writer) TablePrinter {
3636
}
3737
}
3838
return &ttyTablePrinter{
39-
out: w,
39+
out: NewColorable(outFile),
4040
maxWidth: ttyWidth,
4141
}
4242
}

0 commit comments

Comments
 (0)