Replace uses of strings.Title (#5623)

This commit is contained in:
Sam Coe 2022-05-12 15:52:21 +02:00 committed by GitHub
parent 02357c0063
commit d244346960
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 27 deletions

2
go.mod
View file

@ -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
)

View file

@ -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 (

View file

@ -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 {

View file

@ -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()

View file

@ -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 {

View file

@ -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 {
var displayState string
switch reviewer.State {
case requestedReviewState:
displayState = cs.Yellow("Requested")
case approvedReviewState:
displayState = cs.Green("Approved")
case changesRequestedReviewState:
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.
state = commentedReviewState
}
var colorFunc func(string) string
switch state {
case requestedReviewState:
colorFunc = cs.Yellow
case approvedReviewState:
colorFunc = cs.Green
case changesRequestedReviewState:
colorFunc = cs.Red
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

View file

@ -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

View file

@ -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)
}