From d2443469605d57b6df54ce79e33d2ecc8ba9c84f Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Thu, 12 May 2022 15:52:21 +0200 Subject: [PATCH] Replace uses of strings.Title (#5623) --- go.mod | 2 +- internal/codespaces/states.go | 4 ++-- pkg/cmd/issue/view/view.go | 6 +++++- pkg/cmd/pr/shared/comments.go | 5 +++-- pkg/cmd/pr/shared/display.go | 7 +++---- pkg/cmd/pr/view/view.go | 26 ++++++++++++-------------- pkg/cmd/secret/shared/shared.go | 4 +++- pkg/text/convert.go | 12 +++++++++++- 8 files changed, 40 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 1965d05620b..70e6f039a92 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 + golang.org/x/text v0.3.7 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) @@ -66,7 +67,6 @@ require ( github.com/yuin/goldmark-emoji v1.0.1 // indirect golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect - golang.org/x/text v0.3.7 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/internal/codespaces/states.go b/internal/codespaces/states.go index 0d22f1ab6af..887735caf6d 100644 --- a/internal/codespaces/states.go +++ b/internal/codespaces/states.go @@ -8,18 +8,18 @@ import ( "io" "log" "net" - "strings" "time" "github.com/cli/cli/v2/internal/codespaces/api" "github.com/cli/cli/v2/pkg/liveshare" + "github.com/cli/cli/v2/pkg/text" ) // PostCreateStateStatus is a string value representing the different statuses a state can have. type PostCreateStateStatus string func (p PostCreateStateStatus) String() string { - return strings.Title(string(p)) + return text.Title(string(p)) } const ( diff --git a/pkg/cmd/issue/view/view.go b/pkg/cmd/issue/view/view.go index 14c5a9eb8f6..2b59832591e 100644 --- a/pkg/cmd/issue/view/view.go +++ b/pkg/cmd/issue/view/view.go @@ -256,7 +256,11 @@ func printHumanIssuePreview(opts *ViewOptions, issue *api.Issue) error { func issueStateTitleWithColor(cs *iostreams.ColorScheme, issue *api.Issue) string { colorFunc := cs.ColorFromString(prShared.ColorForIssueState(*issue)) - return colorFunc(strings.Title(strings.ToLower(issue.State))) + state := "Open" + if issue.State == "CLOSED" { + state = "Closed" + } + return colorFunc(state) } func issueAssigneeList(issue api.Issue) string { diff --git a/pkg/cmd/pr/shared/comments.go b/pkg/cmd/pr/shared/comments.go index 0d1d6c476d4..d8ff2091e66 100644 --- a/pkg/cmd/pr/shared/comments.go +++ b/pkg/cmd/pr/shared/comments.go @@ -9,6 +9,7 @@ import ( "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/markdown" + "github.com/cli/cli/v2/pkg/text" "github.com/cli/cli/v2/utils" ) @@ -99,7 +100,7 @@ func formatComment(io *iostreams.IOStreams, comment Comment, newest bool) (strin fmt.Fprint(&b, formatCommentStatus(cs, comment.Status())) } if comment.Association() != "NONE" { - fmt.Fprint(&b, cs.Boldf(" (%s)", strings.Title(strings.ToLower(comment.Association())))) + fmt.Fprint(&b, cs.Boldf(" (%s)", text.Title(comment.Association()))) } fmt.Fprint(&b, cs.Boldf(" • %s", utils.FuzzyAgoAbbr(time.Now(), comment.Created()))) if comment.IsEdited() { @@ -195,7 +196,7 @@ func formatHiddenComment(comment Comment) string { var b strings.Builder fmt.Fprint(&b, comment.AuthorLogin()) if comment.Association() != "NONE" { - fmt.Fprintf(&b, " (%s)", strings.Title(strings.ToLower(comment.Association()))) + fmt.Fprintf(&b, " (%s)", text.Title(comment.Association())) } fmt.Fprintf(&b, " • This comment has been marked as %s\n\n", comment.HiddenReason()) return b.String() diff --git a/pkg/cmd/pr/shared/display.go b/pkg/cmd/pr/shared/display.go index 739dea47ba2..f82d0943d42 100644 --- a/pkg/cmd/pr/shared/display.go +++ b/pkg/cmd/pr/shared/display.go @@ -2,21 +2,20 @@ package shared import ( "fmt" - "strings" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/text" "github.com/cli/cli/v2/utils" ) func StateTitleWithColor(cs *iostreams.ColorScheme, pr api.PullRequest) string { prStateColorFunc := cs.ColorFromString(ColorForPRState(pr)) - if pr.State == "OPEN" && pr.IsDraft { - return prStateColorFunc(strings.Title(strings.ToLower("Draft"))) + return prStateColorFunc("Draft") } - return prStateColorFunc(strings.Title(strings.ToLower(pr.State))) + return prStateColorFunc(text.Title(pr.State)) } func ColorForPRState(pr api.PullRequest) string { diff --git a/pkg/cmd/pr/view/view.go b/pkg/cmd/pr/view/view.go index 019ba53b5ae..c385e165451 100644 --- a/pkg/cmd/pr/view/view.go +++ b/pkg/cmd/pr/view/view.go @@ -12,6 +12,7 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/markdown" + "github.com/cli/cli/v2/pkg/text" "github.com/cli/cli/v2/utils" "github.com/spf13/cobra" ) @@ -253,26 +254,23 @@ type reviewerState struct { // formattedReviewerState formats a reviewerState with state color func formattedReviewerState(cs *iostreams.ColorScheme, reviewer *reviewerState) string { - state := reviewer.State - if state == dismissedReviewState { - // Show "DISMISSED" review as "COMMENTED", since "dismissed" only makes - // sense when displayed in an events timeline but not in the final tally. - state = commentedReviewState - } - - var colorFunc func(string) string - switch state { + var displayState string + switch reviewer.State { case requestedReviewState: - colorFunc = cs.Yellow + displayState = cs.Yellow("Requested") case approvedReviewState: - colorFunc = cs.Green + displayState = cs.Green("Approved") case changesRequestedReviewState: - colorFunc = cs.Red + displayState = cs.Red("Changes requested") + case commentedReviewState, dismissedReviewState: + // Show "DISMISSED" review as "COMMENTED", since "dismissed" only makes + // sense when displayed in an events timeline but not in the final tally. + displayState = "Commented" default: - colorFunc = func(str string) string { return str } // Do nothing + displayState = text.Title(reviewer.State) } - return fmt.Sprintf("%s (%s)", reviewer.Name, colorFunc(strings.ReplaceAll(strings.Title(strings.ToLower(state)), "_", " "))) + return fmt.Sprintf("%s (%s)", reviewer.Name, displayState) } // prReviewerList generates a reviewer list with their last state diff --git a/pkg/cmd/secret/shared/shared.go b/pkg/cmd/secret/shared/shared.go index 4e5e7061793..200c8b0fae4 100644 --- a/pkg/cmd/secret/shared/shared.go +++ b/pkg/cmd/secret/shared/shared.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" "strings" + + "github.com/cli/cli/v2/pkg/text" ) type Visibility string @@ -28,7 +30,7 @@ func (app App) String() string { } func (app App) Title() string { - return strings.Title(app.String()) + return text.Title(app.String()) } type SecretEntity string diff --git a/pkg/text/convert.go b/pkg/text/convert.go index c5d2f401d60..8ec3db0dd0a 100644 --- a/pkg/text/convert.go +++ b/pkg/text/convert.go @@ -1,6 +1,11 @@ package text -import "unicode" +import ( + "unicode" + + "golang.org/x/text/cases" + "golang.org/x/text/language" +) // Copied from: https://github.com/asaskevich/govalidator func CamelToKebab(str string) string { @@ -27,3 +32,8 @@ func addSegment(inrune, segment []rune) []rune { inrune = append(inrune, segment...) return inrune } + +func Title(str string) string { + c := cases.Title(language.English) + return c.String(str) +}