diff --git a/command/issue.go b/command/issue.go index 34648c29d..958bcb9bd 100644 --- a/command/issue.go +++ b/command/issue.go @@ -13,6 +13,7 @@ import ( "github.com/github/gh-cli/utils" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func init() { @@ -102,14 +103,22 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - out := cmd.OutOrStdout() - colorOut := colorableOut(cmd) - if len(issues) == 0 { - printMessage(colorOut, "There are no open issues") + colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open issues" message is acually an issue + msg := "There are no open issues" + + userSetFlags := false + cmd.Flags().VisitAll(func(f *pflag.Flag) { + userSetFlags = f.Changed || userSetFlags + }) + if userSetFlags { + msg = "No issues match your search" + } + printMessage(colorErr, msg) return nil } + out := cmd.OutOrStdout() table := utils.NewTablePrinter(out) for _, issue := range issues { issueNum := strconv.Itoa(issue.Number) diff --git a/command/pr.go b/command/pr.go index 1777e963c..e72b73024 100644 --- a/command/pr.go +++ b/command/pr.go @@ -14,6 +14,7 @@ import ( "github.com/github/gh-cli/git" "github.com/github/gh-cli/utils" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func init() { @@ -185,6 +186,21 @@ func prList(cmd *cobra.Command, args []string) error { return err } + if len(prs) == 0 { + colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open prs" message is acually a pr + msg := "There are no open pull requests" + + userSetFlags := false + cmd.Flags().VisitAll(func(f *pflag.Flag) { + userSetFlags = f.Changed || userSetFlags + }) + if userSetFlags { + msg = "No pull requests match your search" + } + printMessage(colorErr, msg) + return nil + } + table := utils.NewTablePrinter(cmd.OutOrStdout()) for _, pr := range prs { prNum := strconv.Itoa(pr.Number) diff --git a/command/root.go b/command/root.go index 33a8c4aa5..c925285ed 100644 --- a/command/root.go +++ b/command/root.go @@ -117,3 +117,11 @@ func colorableOut(cmd *cobra.Command) io.Writer { } return out } + +func colorableErr(cmd *cobra.Command) io.Writer { + err := cmd.ErrOrStderr() + if outFile, isFile := err.(*os.File); isFile { + return utils.NewColorable(outFile) + } + return err +}