diff --git a/.gitignore b/.gitignore index b384e3d5c..31c27ed97 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ .vscode # macOS -.DS_Store \ No newline at end of file +.DS_Store + +vendor/ diff --git a/.goreleaser.yml b/.goreleaser.yml index 619180def..8fca69fb3 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -20,7 +20,7 @@ builds: - <<: *build_defaults id: linux goos: [linux] - goarch: [386, amd64] + goarch: [386, amd64, arm64] - <<: *build_defaults id: windows goos: [windows] diff --git a/api/client.go b/api/client.go index c056680c9..a0ed6a859 100644 --- a/api/client.go +++ b/api/client.go @@ -9,6 +9,8 @@ import ( "net/http" "regexp" "strings" + + "github.com/henvic/httpretty" ) // ClientOption represents an argument to NewClient @@ -36,30 +38,22 @@ func AddHeader(name, value string) ClientOption { } // VerboseLog enables request/response logging within a RoundTripper -func VerboseLog(out io.Writer, logBodies bool) ClientOption { - return func(tr http.RoundTripper) http.RoundTripper { - return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { - fmt.Fprintf(out, "> %s %s\n", req.Method, req.URL.RequestURI()) - if logBodies && req.Body != nil && inspectableMIMEType(req.Header.Get("Content-type")) { - newBody := &bytes.Buffer{} - io.Copy(out, io.TeeReader(req.Body, newBody)) - fmt.Fprintln(out) - req.Body = ioutil.NopCloser(newBody) - } - res, err := tr.RoundTrip(req) - if err == nil { - fmt.Fprintf(out, "< HTTP %s\n", res.Status) - if logBodies && res.Body != nil && inspectableMIMEType(res.Header.Get("Content-type")) { - newBody := &bytes.Buffer{} - // TODO: pretty-print response JSON - io.Copy(out, io.TeeReader(res.Body, newBody)) - fmt.Fprintln(out) - res.Body = ioutil.NopCloser(newBody) - } - } - return res, err - }} +func VerboseLog(out io.Writer, logTraffic bool, colorize bool) ClientOption { + logger := &httpretty.Logger{ + Time: true, + TLS: false, + Colors: colorize, + RequestHeader: logTraffic, + RequestBody: logTraffic, + ResponseHeader: logTraffic, + ResponseBody: logTraffic, + Formatters: []httpretty.Formatter{&httpretty.JSONFormatter{}}, } + logger.SetOutput(out) + logger.SetBodyFilter(func(h http.Header) (skip bool, err error) { + return !inspectableMIMEType(h.Get("Content-Type")), nil + }) + return logger.RoundTripper } // ReplaceTripper substitutes the underlying RoundTripper with a custom one diff --git a/api/queries_issue.go b/api/queries_issue.go index ec125129a..ae56bfab2 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -185,21 +185,24 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str } query := fragments + ` - query($owner: String!, $repo: String!, $limit: Int, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String) { - repository(owner: $owner, name: $repo) { - hasIssuesEnabled - issues(first: $limit, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) { - totalCount - nodes { - ...issue - } - } - } - } - ` + query($owner: String!, $repo: String!, $limit: Int, $endCursor: String, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String) { + repository(owner: $owner, name: $repo) { + hasIssuesEnabled + issues(first: $limit, after: $endCursor, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) { + totalCount + nodes { + ...issue + } + pageInfo { + hasNextPage + endCursor + } + } + } + } + ` variables := map[string]interface{}{ - "limit": limit, "owner": repo.RepoOwner(), "repo": repo.RepoName(), "states": states, @@ -211,26 +214,50 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str variables["assignee"] = assigneeString } - var resp struct { + var response struct { Repository struct { Issues struct { - Nodes []Issue TotalCount int + Nodes []Issue + PageInfo struct { + HasNextPage bool + EndCursor string + } } HasIssuesEnabled bool } } - err := client.GraphQL(query, variables, &resp) - if err != nil { - return nil, err + var issues []Issue + pageLimit := min(limit, 100) + +loop: + for { + variables["limit"] = pageLimit + err := client.GraphQL(query, variables, &response) + if err != nil { + return nil, err + } + if !response.Repository.HasIssuesEnabled { + return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo)) + } + + for _, issue := range response.Repository.Issues.Nodes { + issues = append(issues, issue) + if len(issues) == limit { + break loop + } + } + + if response.Repository.Issues.PageInfo.HasNextPage { + variables["endCursor"] = response.Repository.Issues.PageInfo.EndCursor + pageLimit = min(pageLimit, limit-len(issues)) + } else { + break + } } - if !resp.Repository.HasIssuesEnabled { - return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo)) - } - - res := IssuesAndTotalCount{Issues: resp.Repository.Issues.Nodes, TotalCount: resp.Repository.Issues.TotalCount} + res := IssuesAndTotalCount{Issues: issues, TotalCount: response.Repository.Issues.TotalCount} return &res, nil } diff --git a/api/queries_issue_test.go b/api/queries_issue_test.go new file mode 100644 index 000000000..bde6b8696 --- /dev/null +++ b/api/queries_issue_test.go @@ -0,0 +1,68 @@ +package api + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "testing" + + "github.com/cli/cli/internal/ghrepo" +) + +func TestIssueList(t *testing.T) { + http := &FakeHTTP{} + client := NewClient(ReplaceTripper(http)) + + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "hasIssuesEnabled": true, + "issues": { + "nodes": [], + "pageInfo": { + "hasNextPage": true, + "endCursor": "ENDCURSOR" + } + } + } } } + `)) + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { + "hasIssuesEnabled": true, + "issues": { + "nodes": [], + "pageInfo": { + "hasNextPage": false, + "endCursor": "ENDCURSOR" + } + } + } } } + `)) + + _, err := IssueList(client, ghrepo.FromFullName("OWNER/REPO"), "open", []string{}, "", 251) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(http.Requests) != 2 { + t.Fatalf("expected 2 HTTP requests, seen %d", len(http.Requests)) + } + var reqBody struct { + Query string + Variables map[string]interface{} + } + + bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body) + json.Unmarshal(bodyBytes, &reqBody) + if reqLimit := reqBody.Variables["limit"].(float64); reqLimit != 100 { + t.Errorf("expected 100, got %v", reqLimit) + } + if _, cursorPresent := reqBody.Variables["endCursor"]; cursorPresent { + t.Error("did not expect first request to pass 'endCursor'") + } + + bodyBytes, _ = ioutil.ReadAll(http.Requests[1].Body) + json.Unmarshal(bodyBytes, &reqBody) + if endCursor := reqBody.Variables["endCursor"].(string); endCursor != "ENDCURSOR" { + t.Errorf("expected %q, got %q", "ENDCURSOR", endCursor) + } +} diff --git a/api/queries_pr.go b/api/queries_pr.go index 3b057d5e7..c235f67d4 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -40,6 +40,7 @@ type PullRequest struct { } } IsCrossRepository bool + IsDraft bool MaintainerCanModify bool ReviewDecision string @@ -156,6 +157,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu login } isCrossRepository + isDraft commits(last: 1) { nodes { commit { @@ -366,6 +368,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string) login } isCrossRepository + isDraft } } } @@ -462,6 +465,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P login } isCrossRepository + isDraft } ` @@ -500,7 +504,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P } }` - prs := []PullRequest{} + var prs []PullRequest pageLimit := min(limit, 100) variables := map[string]interface{}{} res := PullRequestAndTotalCount{} @@ -560,7 +564,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P variables[name] = val } } - +loop: for { variables["limit"] = pageLimit var data response @@ -578,17 +582,16 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*P for _, edge := range prData.Edges { prs = append(prs, edge.Node) if len(prs) == limit { - goto done + break loop } } if prData.PageInfo.HasNextPage { variables["endCursor"] = prData.PageInfo.EndCursor pageLimit = min(pageLimit, limit-len(prs)) - continue + } else { + break } - done: - break } res.PullRequests = prs return &res, nil diff --git a/auth/oauth.go b/auth/oauth.go index 6e85575bf..74c7ab6b6 100644 --- a/auth/oauth.go +++ b/auth/oauth.go @@ -56,7 +56,13 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) { oa.logf("open %s\n", startURL) if err := openInBrowser(startURL); err != nil { fmt.Fprintf(os.Stderr, "error opening web browser: %s\n", err) + fmt.Fprintf(os.Stderr, "") fmt.Fprintf(os.Stderr, "Please open the following URL manually:\n%s\n", startURL) + fmt.Fprintf(os.Stderr, "") + // TODO: Temporary workaround for https://github.com/cli/cli/issues/297 + fmt.Fprintf(os.Stderr, "If you are on a server or other headless system, use this workaround instead:") + fmt.Fprintf(os.Stderr, " 1. Complete authentication on a GUI system") + fmt.Fprintf(os.Stderr, " 2. Copy the contents of ~/.config/gh/config.yml to this system") } http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 00ee1e00c..a9e0837f3 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -13,7 +13,6 @@ import ( "github.com/cli/cli/context" "github.com/cli/cli/update" "github.com/cli/cli/utils" - "github.com/mattn/go-isatty" "github.com/mgutz/ansi" "github.com/spf13/cobra" ) @@ -71,8 +70,7 @@ func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) { } func shouldCheckForUpdate() bool { - errFd := os.Stderr.Fd() - return updaterEnabled != "" && (isatty.IsTerminal(errFd) || isatty.IsCygwinTerminal(errFd)) + return updaterEnabled != "" && utils.IsTerminal(os.Stderr) } func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) { diff --git a/command/completion.go b/command/completion.go index ee0e8978a..d225082ef 100644 --- a/command/completion.go +++ b/command/completion.go @@ -33,14 +33,13 @@ When installing with Homebrew, see https://docs.brew.sh/Shell-Completion switch shellType { case "bash": - RootCmd.GenBashCompletion(cmd.OutOrStdout()) + return RootCmd.GenBashCompletion(cmd.OutOrStdout()) case "zsh": - RootCmd.GenZshCompletion(cmd.OutOrStdout()) + return RootCmd.GenZshCompletion(cmd.OutOrStdout()) case "fish": - cobrafish.GenCompletion(RootCmd, cmd.OutOrStdout()) + return cobrafish.GenCompletion(RootCmd, cmd.OutOrStdout()) default: return fmt.Errorf("unsupported shell type %q", shellType) } - return nil }, } diff --git a/command/issue.go b/command/issue.go index db18cf040..6fc07e0fd 100644 --- a/command/issue.go +++ b/command/issue.go @@ -14,6 +14,7 @@ import ( "github.com/cli/cli/git" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/githubtemplate" + "github.com/cli/cli/pkg/text" "github.com/cli/cli/utils" "github.com/spf13/cobra" ) @@ -107,7 +108,7 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - issuesData, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit) + issuesData, err := api.IssueList(apiClient, baseRepo, state, labels, assignee, limit) if err != nil { return err } @@ -155,7 +156,7 @@ func issueStatus(cmd *cobra.Command, args []string) error { return err } - issuePayload, err := api.IssueStatus(apiClient, *baseRepo, currentUser) + issuePayload, err := api.IssueStatus(apiClient, baseRepo, currentUser) if err != nil { return err } @@ -163,7 +164,7 @@ func issueStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) fmt.Fprintln(out, "") - fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(*baseRepo)) + fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(baseRepo)) fmt.Fprintln(out, "") printHeader(out, "Issues assigned to you") @@ -207,7 +208,7 @@ func issueView(cmd *cobra.Command, args []string) error { return err } - issue, err := issueFromArg(apiClient, *baseRepo, args[0]) + issue, err := issueFromArg(apiClient, baseRepo, args[0]) if err != nil { return err } @@ -243,13 +244,13 @@ func printIssuePreview(out io.Writer, issue *api.Issue) error { ))) if issue.Body != "" { - fmt.Fprintln(out) - md, err := utils.RenderMarkdown(issue.Body) - if err != nil { - return err - } - fmt.Fprintln(out, md) - fmt.Fprintln(out) + fmt.Fprintln(out) + md, err := utils.RenderMarkdown(issue.Body) + if err != nil { + return err + } + fmt.Fprintln(out, md) + fmt.Fprintln(out) } fmt.Fprintf(out, utils.Gray("View this issue on GitHub: %s\n"), issue.URL) @@ -280,8 +281,6 @@ func issueCreate(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(*baseRepo)) - baseOverride, err := cmd.Flags().GetString("repo") if err != nil { return err @@ -295,31 +294,6 @@ func issueCreate(cmd *cobra.Command, args []string) error { } } - if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb { - // TODO: move URL generation into GitHubRepository - openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(*baseRepo)) - if len(templateFiles) > 1 { - openURL += "/choose" - } - cmd.Printf("Opening %s in your browser.\n", openURL) - return utils.OpenInBrowser(openURL) - } - - apiClient, err := apiClientForContext(ctx) - if err != nil { - return err - } - - repo, err := api.GitHubRepo(apiClient, *baseRepo) - if err != nil { - return err - } - if !repo.HasIssuesEnabled { - return fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(*baseRepo)) - } - - action := SubmitAction - title, err := cmd.Flags().GetString("title") if err != nil { return fmt.Errorf("could not parse title: %w", err) @@ -329,6 +303,39 @@ func issueCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not parse body: %w", err) } + if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb { + // TODO: move URL generation into GitHubRepository + openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(baseRepo)) + if title != "" || body != "" { + openURL += fmt.Sprintf( + "?title=%s&body=%s", + url.QueryEscape(title), + url.QueryEscape(body), + ) + } else if len(templateFiles) > 1 { + openURL += "/choose" + } + cmd.Printf("Opening %s in your browser.\n", displayURL(openURL)) + return utils.OpenInBrowser(openURL) + } + + fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(baseRepo)) + + apiClient, err := apiClientForContext(ctx) + if err != nil { + return err + } + + repo, err := api.GitHubRepo(apiClient, baseRepo) + if err != nil { + return err + } + if !repo.HasIssuesEnabled { + return fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(baseRepo)) + } + + action := SubmitAction + interactive := title == "" || body == "" if interactive { @@ -356,7 +363,7 @@ func issueCreate(cmd *cobra.Command, args []string) error { if action == PreviewAction { openURL := fmt.Sprintf( "https://github.com/%s/issues/new/?title=%s&body=%s", - ghrepo.FullName(*baseRepo), + ghrepo.FullName(baseRepo), url.QueryEscape(title), url.QueryEscape(body), ) @@ -394,7 +401,7 @@ func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue) ago := now.Sub(issue.UpdatedAt) fmt.Fprintf(w, "%s%s %s%s %s\n", prefix, number, - truncate(70, replaceExcessiveWhitespace(issue.Title)), + text.Truncate(70, replaceExcessiveWhitespace(issue.Title)), coloredLabels, utils.Gray(utils.FuzzyAgo(ago))) } diff --git a/command/issue_test.go b/command/issue_test.go index e67e62fb2..71641221e 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "regexp" + "strings" "testing" "github.com/cli/cli/utils" @@ -496,5 +497,31 @@ func TestIssueCreate_web(t *testing.T) { } url := seenCmd.Args[len(seenCmd.Args)-1] eq(t, url, "https://github.com/OWNER/REPO/issues/new") - eq(t, output.String(), "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n") + eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n") + eq(t, output.Stderr(), "") +} + +func TestIssueCreate_webTitleBody(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(issueCreateCmd, `issue create -w -t mytitle -b mybody`) + if err != nil { + t.Errorf("error running command `issue create`: %v", err) + } + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := strings.ReplaceAll(seenCmd.Args[len(seenCmd.Args)-1], "^", "") + eq(t, url, "https://github.com/OWNER/REPO/issues/new?title=mytitle&body=mybody") + eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n") } diff --git a/command/pr.go b/command/pr.go index 8bd4ca802..4747b2038 100644 --- a/command/pr.go +++ b/command/pr.go @@ -11,6 +11,7 @@ import ( "github.com/cli/cli/context" "github.com/cli/cli/git" "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/pkg/text" "github.com/cli/cli/utils" "github.com/spf13/cobra" ) @@ -83,7 +84,7 @@ func prStatus(cmd *cobra.Command, args []string) error { return err } - prPayload, err := api.PullRequests(apiClient, *baseRepo, currentPRNumber, currentPRHeadRef, currentUser) + prPayload, err := api.PullRequests(apiClient, baseRepo, currentPRNumber, currentPRHeadRef, currentUser) if err != nil { return err } @@ -91,7 +92,7 @@ func prStatus(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) fmt.Fprintln(out, "") - fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(*baseRepo)) + fmt.Fprintf(out, "Relevant pull requests in %s\n", ghrepo.FullName(baseRepo)) fmt.Fprintln(out, "") printHeader(out, "Current branch") @@ -160,7 +161,7 @@ func prList(cmd *cobra.Command, args []string) error { case "open": graphqlState = []string{"OPEN"} case "closed": - graphqlState = []string{"CLOSED"} + graphqlState = []string{"CLOSED", "MERGED"} case "merged": graphqlState = []string{"MERGED"} case "all": @@ -170,8 +171,8 @@ func prList(cmd *cobra.Command, args []string) error { } params := map[string]interface{}{ - "owner": (*baseRepo).RepoOwner(), - "repo": (*baseRepo).RepoName(), + "owner": baseRepo.RepoOwner(), + "repo": baseRepo.RepoName(), "state": graphqlState, } if len(labels) > 0 { @@ -201,7 +202,7 @@ func prList(cmd *cobra.Command, args []string) error { if table.IsTTY() { prNum = "#" + prNum } - table.AddField(prNum, nil, colorFuncForState(pr.State)) + table.AddField(prNum, nil, colorFuncForPR(pr)) table.AddField(replaceExcessiveWhitespace(pr.Title), nil, nil) table.AddField(pr.HeadLabel(), nil, utils.Cyan) table.EndRow() @@ -214,6 +215,14 @@ func prList(cmd *cobra.Command, args []string) error { return nil } +func colorFuncForPR(pr api.PullRequest) func(string) string { + if pr.State == "OPEN" && pr.IsDraft { + return utils.Gray + } else { + return colorFuncForState(pr.State) + } +} + func colorFuncForState(state string) func(string) string { switch state { case "OPEN": @@ -248,7 +257,7 @@ func prView(cmd *cobra.Command, args []string) error { var openURL string var pr *api.PullRequest if len(args) > 0 { - pr, err = prFromArg(apiClient, *baseRepo, args[0]) + pr, err = prFromArg(apiClient, baseRepo, args[0]) if err != nil { return err } @@ -260,15 +269,15 @@ func prView(cmd *cobra.Command, args []string) error { } if prNumber > 0 { - openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(*baseRepo), prNumber) + openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(baseRepo), prNumber) if preview { - pr, err = api.PullRequestByNumber(apiClient, *baseRepo, prNumber) + pr, err = api.PullRequestByNumber(apiClient, baseRepo, prNumber) if err != nil { return err } } } else { - pr, err = api.PullRequestForBranch(apiClient, *baseRepo, branchWithOwner) + pr, err = api.PullRequestForBranch(apiClient, baseRepo, branchWithOwner) if err != nil { return err } @@ -372,7 +381,13 @@ func prSelectorForCurrentBranch(ctx context.Context) (prNumber int, prHeadRef st func printPrs(w io.Writer, 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()+"]")) + + prNumberColorFunc := utils.Green + if pr.IsDraft { + prNumberColorFunc = utils.Gray + } + + fmt.Fprintf(w, " %s %s %s", prNumberColorFunc(prNumber), text.Truncate(50, replaceExcessiveWhitespace(pr.Title)), utils.Cyan("["+pr.HeadLabel()+"]")) checks := pr.ChecksStatus() reviews := pr.ReviewStatus() @@ -420,13 +435,6 @@ func printMessage(w io.Writer, s string) { fmt.Fprintln(w, utils.Gray(s)) } -func truncate(maxLength int, title string) string { - if len(title) > maxLength { - return title[0:maxLength-3] + "..." - } - return title -} - func replaceExcessiveWhitespace(s string) string { s = strings.TrimSpace(s) s = regexp.MustCompile(`\r?\n`).ReplaceAllString(s, " ") diff --git a/command/pr_create.go b/command/pr_create.go index 7c1109b09..c40f5d8c9 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -99,7 +99,9 @@ func prCreate(cmd *cobra.Command, _ []string) error { if didForkRepo && pushTries < maxPushTries { pushTries++ // first wait 2 seconds after forking, then 4s, then 6s - time.Sleep(time.Duration(2*pushTries) * time.Second) + waitSeconds := 2 * pushTries + fmt.Fprintf(cmd.ErrOrStderr(), "waiting %s before retrying...\n", utils.Pluralize(waitSeconds, "second")) + time.Sleep(time.Duration(waitSeconds) * time.Second) continue } return err @@ -107,24 +109,10 @@ func prCreate(cmd *cobra.Command, _ []string) error { break } - isWeb, err := cmd.Flags().GetBool("web") - if err != nil { - return fmt.Errorf("could not parse web: %q", err) - } - if isWeb { - openURL := fmt.Sprintf(`https://github.com/%s/pull/%s`, ghrepo.FullName(headRepo), headBranch) - fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL) - return utils.OpenInBrowser(openURL) - } - headBranchLabel := headBranch if !ghrepo.IsSame(baseRepo, headRepo) { headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) } - fmt.Fprintf(colorableErr(cmd), "\nCreating pull request for %s into %s in %s\n\n", - utils.Cyan(headBranchLabel), - utils.Cyan(baseBranch), - ghrepo.FullName(baseRepo)) title, err := cmd.Flags().GetString("title") if err != nil { @@ -135,6 +123,21 @@ func prCreate(cmd *cobra.Command, _ []string) error { return fmt.Errorf("could not parse body: %w", err) } + isWeb, err := cmd.Flags().GetBool("web") + if err != nil { + return fmt.Errorf("could not parse web: %q", err) + } + if isWeb { + compareURL := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body) + fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(compareURL)) + return utils.OpenInBrowser(compareURL) + } + + fmt.Fprintf(colorableErr(cmd), "\nCreating pull request for %s into %s in %s\n\n", + utils.Cyan(headBranchLabel), + utils.Cyan(baseBranch), + ghrepo.FullName(baseRepo)) + action := SubmitAction interactive := title == "" || body == "" @@ -196,14 +199,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { fmt.Fprintln(cmd.OutOrStdout(), pr.URL) } else if action == PreviewAction { - openURL := fmt.Sprintf( - "https://github.com/%s/compare/%s...%s?expand=1&title=%s&body=%s", - ghrepo.FullName(baseRepo), - baseBranch, - headBranchLabel, - url.QueryEscape(title), - url.QueryEscape(body), - ) + openURL := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body) // TODO could exceed max url length for explorer fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL)) return utils.OpenInBrowser(openURL) @@ -215,6 +211,22 @@ func prCreate(cmd *cobra.Command, _ []string) error { } +func generateCompareURL(r ghrepo.Interface, base, head, title, body string) string { + u := fmt.Sprintf( + "https://github.com/%s/compare/%s...%s?expand=1", + ghrepo.FullName(r), + base, + head, + ) + if title != "" { + u += "&title=" + url.QueryEscape(title) + } + if body != "" { + u += "&body=" + url.QueryEscape(body) + } + return u +} + var prCreateCmd = &cobra.Command{ Use: "create", Short: "Create a pull request", diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 72cbdce94..b0813d30b 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -98,11 +98,11 @@ func TestPRCreate_web(t *testing.T) { eq(t, err, nil) eq(t, output.String(), "") - eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/pull/feature in your browser.\n") + eq(t, output.Stderr(), "Opening github.com/OWNER/REPO/compare/master...feature in your browser.\n") eq(t, len(ranCommands), 3) eq(t, strings.Join(ranCommands[1], " "), "git push --set-upstream origin HEAD:feature") - eq(t, ranCommands[2][len(ranCommands[2])-1], "https://github.com/OWNER/REPO/pull/feature") + eq(t, ranCommands[2][len(ranCommands[2])-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1") } func TestPRCreate_ReportsUncommittedChanges(t *testing.T) { diff --git a/command/pr_test.go b/command/pr_test.go index 7f3555733..26691e577 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -213,6 +213,30 @@ No pull requests match your search in OWNER/REPO eq(t, reqBody.Variables.Labels, []string{"one", "two", "three"}) } +func TestPRList_filteringClosed(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + respBody := bytes.NewBufferString(`{ "data": {} }`) + http.StubResponse(200, respBody) + + _, err := RunCommand(prListCmd, `pr list -s closed`) + if err != nil { + t.Fatal(err) + } + + bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body) + reqBody := struct { + Variables struct { + State []string + } + }{} + json.Unmarshal(bodyBytes, &reqBody) + + eq(t, reqBody.Variables.State, []string{"CLOSED", "MERGED"}) +} + func TestPRList_filteringAssignee(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() diff --git a/command/repo.go b/command/repo.go new file mode 100644 index 000000000..484ef609c --- /dev/null +++ b/command/repo.go @@ -0,0 +1,87 @@ +package command + +import ( + "fmt" + "os" + "strings" + + "github.com/cli/cli/git" + "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/utils" + "github.com/spf13/cobra" +) + +func init() { + RootCmd.AddCommand(repoCmd) + repoCmd.AddCommand(repoCloneCmd) + repoCmd.AddCommand(repoViewCmd) +} + +var repoCmd = &cobra.Command{ + Use: "repo", + Short: "View repositories", + Long: `Work with GitHub repositories. + +A repository can be supplied as an argument in any of the following formats: +- "OWNER/REPO" +- by URL, e.g. "https://github.com/OWNER/REPO"`, +} + +var repoCloneCmd = &cobra.Command{ + Use: "clone ", + Args: cobra.MinimumNArgs(1), + Short: "Clone a repository locally", + Long: `Clone a GitHub repository locally. + +To pass 'git clone' options, separate them with '--'.`, + RunE: repoClone, +} + +var repoViewCmd = &cobra.Command{ + Use: "view []", + Short: "View a repository in the browser", + Long: `View a GitHub repository in the browser. + +With no argument, the repository for the current directory is opened.`, + RunE: repoView, +} + +func repoClone(cmd *cobra.Command, args []string) error { + cloneURL := args[0] + if !strings.Contains(cloneURL, ":") { + cloneURL = fmt.Sprintf("https://github.com/%s.git", cloneURL) + } + + cloneArgs := []string{"clone"} + cloneArgs = append(cloneArgs, args[1:]...) + cloneArgs = append(cloneArgs, cloneURL) + + cloneCmd := git.GitCommand(cloneArgs...) + cloneCmd.Stdin = os.Stdin + cloneCmd.Stdout = os.Stdout + cloneCmd.Stderr = os.Stderr + return utils.PrepareCmd(cloneCmd).Run() +} + +func repoView(cmd *cobra.Command, args []string) error { + ctx := contextForCommand(cmd) + + var openURL string + if len(args) == 0 { + baseRepo, err := determineBaseRepo(cmd, ctx) + if err != nil { + return err + } + openURL = fmt.Sprintf("https://github.com/%s", ghrepo.FullName(baseRepo)) + } else { + repoArg := args[0] + if strings.HasPrefix(repoArg, "http:/") || strings.HasPrefix(repoArg, "https:/") { + openURL = repoArg + } else { + openURL = fmt.Sprintf("https://github.com/%s", repoArg) + } + } + + fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL)) + return utils.OpenInBrowser(openURL) +} diff --git a/command/repo_test.go b/command/repo_test.go new file mode 100644 index 000000000..588ae3520 --- /dev/null +++ b/command/repo_test.go @@ -0,0 +1,149 @@ +package command + +import ( + "os/exec" + "strings" + "testing" + + "github.com/cli/cli/context" + "github.com/cli/cli/utils" +) + +func TestRepoClone(t *testing.T) { + tests := []struct { + name string + args string + want string + }{ + { + name: "shorthand", + args: "repo clone OWNER/REPO", + want: "git clone https://github.com/OWNER/REPO.git", + }, + { + name: "clone arguments", + args: "repo clone OWNER/REPO -- -o upstream --depth 1", + want: "git clone -o upstream --depth 1 https://github.com/OWNER/REPO.git", + }, + { + name: "HTTPS URL", + args: "repo clone https://github.com/OWNER/REPO", + want: "git clone https://github.com/OWNER/REPO", + }, + { + name: "SSH URL", + args: "repo clone git@github.com:OWNER/REPO.git", + want: "git clone git@github.com:OWNER/REPO.git", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(repoViewCmd, tt.args) + if err != nil { + t.Fatalf("error running command `repo clone`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + eq(t, strings.Join(seenCmd.Args, " "), tt.want) + }) + } +} + +func TestRepoView(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(repoViewCmd, "repo view") + if err != nil { + t.Errorf("error running command `repo view`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "Opening github.com/OWNER/REPO in your browser.\n") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/OWNER/REPO") +} + +func TestRepoView_ownerRepo(t *testing.T) { + ctx := context.NewBlank() + ctx.SetBranch("master") + initContext = func() context.Context { + return ctx + } + initFakeHTTP() + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(repoViewCmd, "repo view cli/cli") + if err != nil { + t.Errorf("error running command `repo view`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "Opening github.com/cli/cli in your browser.\n") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/cli/cli") +} + +func TestRepoView_fullURL(t *testing.T) { + ctx := context.NewBlank() + ctx.SetBranch("master") + initContext = func() context.Context { + return ctx + } + initFakeHTTP() + + var seenCmd *exec.Cmd + restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable { + seenCmd = cmd + return &outputStub{} + }) + defer restoreCmd() + + output, err := RunCommand(repoViewCmd, "repo view https://github.com/cli/cli") + if err != nil { + t.Errorf("error running command `repo view`: %v", err) + } + + eq(t, output.String(), "") + eq(t, output.Stderr(), "Opening github.com/cli/cli in your browser.\n") + + if seenCmd == nil { + t.Fatal("expected a command to run") + } + url := seenCmd.Args[len(seenCmd.Args)-1] + eq(t, url, "https://github.com/cli/cli") +} diff --git a/command/root.go b/command/root.go index 4f9d5ba09..5130f74c9 100644 --- a/command/root.go +++ b/command/root.go @@ -97,15 +97,14 @@ var initContext = func() context.Context { // BasicClient returns an API client that borrows from but does not depend on // user configuration func BasicClient() (*api.Client, error) { - opts := []api.ClientOption{ - api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)), + opts := []api.ClientOption{} + if verbose := os.Getenv("DEBUG"); verbose != "" { + opts = append(opts, apiVerboseLog()) } + opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version))) if c, err := context.ParseDefaultConfig(); err == nil { opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", c.Token))) } - if verbose := os.Getenv("DEBUG"); verbose != "" { - opts = append(opts, api.VerboseLog(os.Stderr, false)) - } return api.NewClient(opts...), nil } @@ -123,20 +122,28 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) { if err != nil { return nil, err } - opts := []api.ClientOption{ + opts := []api.ClientOption{} + if verbose := os.Getenv("DEBUG"); verbose != "" { + opts = append(opts, apiVerboseLog()) + } + opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", token)), api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)), // antiope-preview: Checks // shadow-cat-preview: Draft pull requests api.AddHeader("Accept", "application/vnd.github.antiope-preview+json, application/vnd.github.shadow-cat-preview"), api.AddHeader("GraphQL-Features", "pe_mobile"), - } - if verbose := os.Getenv("DEBUG"); verbose != "" { - opts = append(opts, api.VerboseLog(os.Stderr, strings.Contains(verbose, "api"))) - } + ) + return api.NewClient(opts...), nil } +func apiVerboseLog() api.ClientOption { + logTraffic := strings.Contains(os.Getenv("DEBUG"), "api") + colorize := utils.IsTerminal(os.Stderr) + return api.VerboseLog(utils.NewColorable(os.Stderr), logTraffic, colorize) +} + func colorableOut(cmd *cobra.Command) io.Writer { out := cmd.OutOrStdout() if outFile, isFile := out.(*os.File); isFile { @@ -164,7 +171,7 @@ func changelogURL(version string) string { return url } -func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (*ghrepo.Interface, error) { +func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (ghrepo.Interface, error) { apiClient, err := apiClientForContext(ctx) if err != nil { return nil, err @@ -185,11 +192,10 @@ func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (*ghrepo.Interfa return nil, err } - var baseRepo ghrepo.Interface - baseRepo, err = repoContext.BaseRepo() + baseRepo, err := repoContext.BaseRepo() if err != nil { return nil, err } - return &baseRepo, nil + return baseRepo, nil } diff --git a/command/title_body_survey.go b/command/title_body_survey.go index fba5b8282..fa34384af 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -79,9 +79,11 @@ func selectTemplate(templatePaths []string) (string, error) { func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody string, templatePaths []string) (*titleBody, error) { inProgress := titleBody{} + templateContents := "" if providedBody == "" && len(templatePaths) > 0 { - templateContents, err := selectTemplate(templatePaths) + var err error + templateContents, err = selectTemplate(templatePaths) if err != nil { return nil, err } @@ -121,6 +123,10 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody stri return nil, fmt.Errorf("could not prompt: %w", err) } + if inProgress.Body == "" { + inProgress.Body = templateContents + } + confirmA, err := confirm() if err != nil { return nil, fmt.Errorf("unable to confirm: %w", err) diff --git a/context/context.go b/context/context.go index fed589a8d..385425958 100644 --- a/context/context.go +++ b/context/context.go @@ -201,9 +201,17 @@ func (c *fsContext) Remotes() (Remotes, error) { if err != nil { return nil, err } + if len(gitRemotes) == 0 { + return nil, errors.New("no git remotes found") + } + sshTranslate := git.ParseSSHConfig().Translator() c.remotes = translateRemotes(gitRemotes, sshTranslate) } + + if len(c.remotes) == 0 { + return nil, errors.New("no git remote found for a github.com repository") + } return c.remotes, nil } diff --git a/git/git.go b/git/git.go index 92d2dfd0f..dfd45bade 100644 --- a/git/git.go +++ b/git/git.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/url" + "os" "os/exec" "regexp" "strings" @@ -70,8 +71,11 @@ func UncommittedChangeCount() (int, error) { return count, nil } +// Push publishes a git ref to a remote and sets up upstream configuration func Push(remote string, ref string) error { pushCmd := GitCommand("push", "--set-upstream", remote, ref) + pushCmd.Stdout = os.Stdout + pushCmd.Stderr = os.Stderr return utils.PrepareCmd(pushCmd).Run() } diff --git a/go.mod b/go.mod index 8b3711e37..458a7268e 100644 --- a/go.mod +++ b/go.mod @@ -3,18 +3,28 @@ module github.com/cli/cli go 1.13 require ( - github.com/AlecAivazis/survey/v2 v2.0.4 - github.com/charmbracelet/glamour v0.1.0 + github.com/AlecAivazis/survey/v2 v2.0.5 + github.com/alecthomas/chroma v0.7.1 // indirect + github.com/charmbracelet/glamour v0.1.1-0.20200210132808-8d4e3f0692f8 + github.com/dlclark/regexp2 v1.2.0 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/hashicorp/go-version v1.2.0 + github.com/henvic/httpretty v0.0.3 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 - github.com/mattn/go-colorable v0.1.2 - github.com/mattn/go-isatty v0.0.9 + github.com/mattn/go-colorable v0.1.4 + github.com/mattn/go-isatty v0.0.12 + github.com/mattn/go-runewidth v0.0.8 // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b github.com/mitchellh/go-homedir v1.1.0 - github.com/spf13/cobra v0.0.5 + github.com/muesli/reflow v0.0.0-20200210202703-cf7e7eac5cb4 // indirect + github.com/spf13/cobra v0.0.6 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.4.0 // indirect - golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 - gopkg.in/yaml.v3 v3.0.0-20191010095647-fc94e3f71652 + github.com/yuin/goldmark v1.1.23 // indirect + golang.org/x/crypto v0.0.0-20200219234226-1ad67e1f0ef4 + golang.org/x/net v0.0.0-20200219183655-46282727080f // indirect + golang.org/x/sys v0.0.0-20200219091948-cb0a6d8edb6c // indirect + golang.org/x/text v0.3.0 + gopkg.in/yaml.v2 v2.2.8 // indirect + gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 ) diff --git a/go.sum b/go.sum index 79cc2305e..518f745aa 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,20 @@ -github.com/AlecAivazis/survey/v2 v2.0.4 h1:qzXnJSzXEvmUllWqMBWpZndvT2YfoAUzAMvZUax3L2M= -github.com/AlecAivazis/survey/v2 v2.0.4/go.mod h1:WYBhg6f0y/fNYUuesWQc0PKbJcEliGcYHB9sNT3Bg74= +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/AlecAivazis/survey/v2 v2.0.5 h1:xpZp+Q55wi5C7Iaze+40onHnEkex1jSc34CltJjOoPM= +github.com/AlecAivazis/survey/v2 v2.0.5/go.mod h1:WYBhg6f0y/fNYUuesWQc0PKbJcEliGcYHB9sNT3Bg74= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0= github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0= github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw= github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U= github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= github.com/alecthomas/chroma v0.7.0 h1:z+0HgTUmkpRDRz0SRSdMaqOLfJV4F+N1FPDZUZIDUzw= github.com/alecthomas/chroma v0.7.0/go.mod h1:1U/PfCsTALWWYHDnsIQkxEBM0+6LLe0v8+RSVMOwxeY= +github.com/alecthomas/chroma v0.7.1 h1:G1i02OhUbRi2nJxcNkwJaY/J1gHXj9tt72qN6ZouLFQ= +github.com/alecthomas/chroma v0.7.1/go.mod h1:gHw09mkX1Qp80JlYbmN9L3+4R5o6DJJ3GRShh+AICNc= github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721 h1:JHZL0hZKJ1VENNfmXvHbgYlbUOvpzYzvy2aZU5gXVeo= github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= github.com/alecthomas/kong v0.1.17-0.20190424132513-439c674f7ae0/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI= @@ -18,55 +22,103 @@ github.com/alecthomas/kong v0.2.1-0.20190708041108-0548c6b1afae/go.mod h1:+inYUS github.com/alecthomas/kong-hcl v0.1.8-0.20190615233001-b21fea9723c8/go.mod h1:MRgZdU3vrFd05IQ89AxUZ0aYdF39BYoNFa324SodPCA= github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 h1:p9Sln00KOTlrYkxI1zYWl1QLnEqAqEARBEYa8FQnQcY= github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/charmbracelet/glamour v0.1.0 h1:BHCtc+YJjoBjNUnFKBtXyyM4Bp9u7L2kf49qV+/AGYw= -github.com/charmbracelet/glamour v0.1.0/go.mod h1:Z1C2JkVGBom/RYfoKcPBZ81lHMR3xp3W6OCLNWWEIMc= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/charmbracelet/glamour v0.1.1-0.20200210132808-8d4e3f0692f8 h1:udjWAc2bbIP0LyzEMt7BWxk6O6lIoONWoonN3C9bMtA= +github.com/charmbracelet/glamour v0.1.1-0.20200210132808-8d4e3f0692f8/go.mod h1:UsY1vFbaLp/j/SYgLfPH0n2I0jngBL+q6+mCAsESih4= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= -github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk= -github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E= github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dlclark/regexp2 v1.1.6 h1:CqB4MjHw0MFCDj+PHHjiESmHX+N7t0tJzKvC6M97BRg= github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= +github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4= +github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/gorilla/csrf v1.6.0/go.mod h1:7tSf8kmjNYr7IWDCYhd3U8Ck34iQ/Yw5CJu7bAkHEGI= github.com/gorilla/handlers v1.4.1/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/henvic/httpretty v0.0.3 h1:oHTreVv2lcdRYUNm4h3cgbrGN0dTieO9H8UnxEZNlvw= +github.com/henvic/httpretty v0.0.3/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo= github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ= github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ= github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23 h1:Wp7NjqGKGN9te9N/rvXYRhlVcrulGdxnz8zadXWs7fc= -github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= -github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0= +github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s= @@ -74,9 +126,14 @@ github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/le github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/muesli/reflow v0.0.0-20191216070243-e5efeac4e302 h1:jOh3Kh03uOFkRPV3PI4Am5tqACv2aELgbPgr7YgNX00= -github.com/muesli/reflow v0.0.0-20191216070243-e5efeac4e302/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I= +github.com/muesli/reflow v0.0.0-20200210123334-eb23c6404749/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I= +github.com/muesli/reflow v0.0.0-20200210202703-cf7e7eac5cb4 h1:IQAzML2A961stVPyK3En5VajxyiujmTEUkJUXsXHY44= +github.com/muesli/reflow v0.0.0-20200210202703-cf7e7eac5cb4/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I= +github.com/muesli/termenv v0.4.0 h1:8Bz8WDF/6OoL7QENtESkAl3TlMyQq6+c3bjtJO3y9V8= +github.com/muesli/termenv v0.4.0/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8= github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= @@ -85,53 +142,111 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= -github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= -github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/cobra v0.0.6 h1:breEStsVwemnKh2/s6gMvSdMEkwW0sK8vGStnlVBMCs= +github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= +github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -github.com/yuin/goldmark v1.1.19 h1:0s2/60x0XsFCXHeFut+F3azDVAAyIMyUfJRbRexiTYs= -github.com/yuin/goldmark v1.1.19/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0= -golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +github.com/yuin/goldmark v1.1.22/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.1.23 h1:eTodJ8hwEUvwXhb9qxQNuL/q1d+xMQClrXR4mdvV7gs= +github.com/yuin/goldmark v1.1.23/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200219234226-1ad67e1f0ef4 h1:4icQlpeqbz3WxfgP6Eq3szTj95KTrlH/CwzBzoxuFd0= +golang.org/x/crypto v0.0.0-20200219234226-1ad67e1f0ef4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20200219183655-46282727080f h1:dB42wwhNuwPvh8f+5zZWNcU+F2Xs/B9wXXwvUCOH7r8= +golang.org/x/net v0.0.0-20200219183655-46282727080f/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200219091948-cb0a6d8edb6c h1:jceGD5YNJGgGMkJz79agzOln1K9TaZUjv5ird16qniQ= +golang.org/x/sys v0.0.0-20200219091948-cb0a6d8edb6c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20191010095647-fc94e3f71652 h1:VKvJ/mQ4BgCjZUDggYFxTe0qv9jPMHsZPD4Xt91Y5H4= -gopkg.in/yaml.v3 v3.0.0-20191010095647-fc94e3f71652/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71 h1:Xe2gvTZUJpsvOWUnvmL/tmhVBZUmHSvLbMjRj6NUUKo= +gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/pkg/text/truncate.go b/pkg/text/truncate.go new file mode 100644 index 000000000..7ad10489f --- /dev/null +++ b/pkg/text/truncate.go @@ -0,0 +1,63 @@ +package text + +import ( + "golang.org/x/text/width" +) + +// DisplayWidth calculates what the rendered width of a string may be +func DisplayWidth(s string) int { + w := 0 + for _, r := range s { + w += runeDisplayWidth(r) + } + return w +} + +const ( + ellipsisWidth = 3 + minWidthForEllipsis = 5 +) + +// Truncate shortens a string to fit the maximum display width +func Truncate(max int, s string) string { + w := DisplayWidth(s) + if w <= max { + return s + } + + useEllipsis := false + if max >= minWidthForEllipsis { + useEllipsis = true + max -= 3 + } + + cw := 0 + ri := 0 + for _, r := range s { + rw := runeDisplayWidth(r) + if cw+rw > max { + break + } + cw += rw + ri++ + } + + res := string([]rune(s)[:ri]) + if useEllipsis { + res += "..." + } + if cw < max { + // compensate if truncating a wide character left an odd space + res += " " + } + return res +} + +func runeDisplayWidth(r rune) int { + switch width.LookupRune(r).Kind() { + case width.EastAsianWide, width.EastAsianAmbiguous, width.EastAsianFullwidth: + return 2 + default: + return 1 + } +} diff --git a/pkg/text/truncate_test.go b/pkg/text/truncate_test.go new file mode 100644 index 000000000..dbc0ec060 --- /dev/null +++ b/pkg/text/truncate_test.go @@ -0,0 +1,71 @@ +package text + +import "testing" + +func TestTruncate(t *testing.T) { + type args struct { + max int + s string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "Japanese", + args: args{ + max: 11, + s: "テストテストテストテスト", + }, + want: "テストテ...", + }, + { + name: "Japanese filled", + args: args{ + max: 11, + s: "aテストテストテストテスト", + }, + want: "aテスト... ", + }, + { + name: "Chinese", + args: args{ + max: 11, + s: "幫新舉報違章工廠新增編號", + }, + want: "幫新舉報...", + }, + { + name: "Chinese filled", + args: args{ + max: 11, + s: "a幫新舉報違章工廠新增編號", + }, + want: "a幫新舉... ", + }, + { + name: "Korean", + args: args{ + max: 11, + s: "프로젝트 내의", + }, + want: "프로젝트...", + }, + { + name: "Korean filled", + args: args{ + max: 11, + s: "a프로젝트 내의", + }, + want: "a프로젝... ", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Truncate(tt.args.max, tt.args.s); got != tt.want { + t.Errorf("Truncate() = %q, want %q", got, tt.want) + } + }) + } +} diff --git a/utils/color.go b/utils/color.go index ed22bc3e9..0a002588f 100644 --- a/utils/color.go +++ b/utils/color.go @@ -14,13 +14,17 @@ var checkedTerminal = false func isStdoutTerminal() bool { if !checkedTerminal { - fd := os.Stdout.Fd() - _isStdoutTerminal = isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd) + _isStdoutTerminal = IsTerminal(os.Stdout) checkedTerminal = true } return _isStdoutTerminal } +// IsTerminal reports whether the file descriptor is connected to a terminal +func IsTerminal(f *os.File) bool { + return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd()) +} + // NewColorable returns an output stream that handles ANSI color sequences on Windows func NewColorable(f *os.File) io.Writer { return colorable.NewColorable(f) diff --git a/utils/table_printer.go b/utils/table_printer.go index e1bf9bc36..b6f651196 100644 --- a/utils/table_printer.go +++ b/utils/table_printer.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "github.com/cli/cli/pkg/text" "github.com/mattn/go-isatty" "golang.org/x/crypto/ssh/terminal" ) @@ -21,6 +22,7 @@ type TablePrinter interface { func NewTablePrinter(w io.Writer) TablePrinter { if outFile, isFile := w.(*os.File); isFile { + // TODO: use utils.IsTerminal() isCygwin := isatty.IsCygwinTerminal(outFile.Fd()) if isatty.IsTerminal(outFile.Fd()) || isCygwin { ttyWidth := 80 @@ -62,16 +64,16 @@ func (t ttyTablePrinter) IsTTY() bool { return true } -func (t *ttyTablePrinter) AddField(text string, truncateFunc func(int, string) string, colorFunc func(string) string) { +func (t *ttyTablePrinter) AddField(s string, truncateFunc func(int, string) string, colorFunc func(string) string) { if truncateFunc == nil { - truncateFunc = truncate + truncateFunc = text.Truncate } if t.rows == nil { t.rows = [][]tableField{[]tableField{}} } rowI := len(t.rows) - 1 field := tableField{ - Text: text, + Text: s, TruncateFunc: truncateFunc, ColorFunc: colorFunc, } @@ -92,7 +94,7 @@ func (t *ttyTablePrinter) Render() error { // measure maximum content width per column for _, row := range t.rows { for col, field := range row { - textLen := len(field.Text) + textLen := text.DisplayWidth(field.Text) if textLen > colWidths[col] { colWidths[col] = textLen } @@ -128,7 +130,9 @@ func (t *ttyTablePrinter) Render() error { truncVal := field.TruncateFunc(colWidths[col], field.Text) if col < numCols-1 { // pad value with spaces on the right - truncVal = fmt.Sprintf("%-*s", colWidths[col], truncVal) + if padWidth := colWidths[col] - text.DisplayWidth(field.Text); padWidth > 0 { + truncVal += strings.Repeat(" ", padWidth) + } } if field.ColorFunc != nil { truncVal = field.ColorFunc(truncVal) @@ -173,13 +177,3 @@ func (t *tsvTablePrinter) EndRow() { func (t *tsvTablePrinter) Render() error { return nil } - -func truncate(maxLength int, title string) string { - if len(title) > maxLength { - if maxLength > 3 { - return title[0:maxLength-3] + "..." - } - return title[0:maxLength] - } - return title -} diff --git a/utils/utils.go b/utils/utils.go index db95ab3d0..1d23d7d89 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -63,7 +63,7 @@ func FuzzyAgo(ago time.Duration) string { return fmtDuration(int(ago.Hours()/24/365), "year") } -func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo *ghrepo.Interface) string { +func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo ghrepo.Interface) string { userSetFlagCounter := 0 limitSet := false @@ -81,14 +81,14 @@ func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, bas if userSetFlagCounter > 0 { msg = fmt.Sprintf("No %ss match your search", cmdType) } - return fmt.Sprintf(title, msg, ghrepo.FullName(*baseRepo)) + return fmt.Sprintf(title, msg, ghrepo.FullName(baseRepo)) } if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) { title = "\n%s match your search in %s\n\n" } - out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(*baseRepo)) + out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(baseRepo)) if limit < matchCount { out = out + fmt.Sprintln(Gray(fmt.Sprintf("Showing %d/%d results\n", limit, matchCount)))