Skip to content

Commit

Permalink
Replace uses of strings.Title (#5623)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed May 12, 2022
1 parent 02357c0 commit d244346
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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
)

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

Expand Down
4 changes: 2 additions & 2 deletions internal/codespaces/states.go
Expand Up @@ -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 (
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/issue/view/view.go
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/pr/shared/comments.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions pkg/cmd/pr/shared/display.go
Expand Up @@ -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 {
Expand Down
26 changes: 12 additions & 14 deletions pkg/cmd/pr/view/view.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/secret/shared/shared.go
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"strings"

"github.com/cli/cli/v2/pkg/text"
)

type Visibility string
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion 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 {
Expand All @@ -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)
}

0 comments on commit d244346

Please sign in to comment.