diff --git a/commands/output/output.go b/commands/output/output.go index 4613cd38..6e52e0ae 100644 --- a/commands/output/output.go +++ b/commands/output/output.go @@ -23,6 +23,7 @@ import ( "strings" "time" + "github.com/google/git-appraise/repository" "github.com/google/git-appraise/review" ) @@ -52,6 +53,15 @@ status: %s // Number of lines of context to print for inline comments contextLineCount = 5 ) +var defaultColors = map[string]string{ + "tbr": "red white bold blink", + "pending": "cyan", + "submitted": "yellow", + "accepted": "green", + "danger": "yellow red bold blink", + "abandon": "magenta", + "rejected": "yellow red bold strike", +} // getStatusString returns a human friendly string encapsulating both the review's // resolved status, and its submitted status. @@ -77,11 +87,44 @@ func getStatusString(r *review.Summary) string { return "rejected" } +func getColorStatusString(repo repository.Repo, statusString string) string { + useColor, err := repo.GetColorBool("color.appraise") + if err != nil || !useColor { + return statusString + } + + var colorOn string + var colorOff string + + defaultColor, _ := defaultColors[statusString] + colorOn, err = repo.GetColor( + fmt.Sprintf("color.appraise.%s", statusString), + defaultColor, + ) + if err != nil { + return statusString + } + + colorOff, err = repo.GetColor("", "reset") + + if err != nil { + return statusString + } + + return fmt.Sprintf("%s%s%s", colorOn, statusString, colorOff) +} + // PrintSummary prints a single-line summary of a review. func PrintSummary(r *review.Summary) { statusString := getStatusString(r) indentedDescription := strings.Replace(r.Request.Description, "\n", "\n ", -1) - fmt.Printf(reviewSummaryTemplate, statusString, r.Revision, indentedDescription) + + fmt.Printf( + reviewSummaryTemplate, + getColorStatusString(r.Repo, statusString), + r.Revision, + indentedDescription, + ) } // reformatTimestamp takes a timestamp string of the form "0123456789" and changes it diff --git a/repository/git.go b/repository/git.go index 31d27ea6..ba990763 100644 --- a/repository/git.go +++ b/repository/git.go @@ -985,3 +985,12 @@ func (repo *GitRepo) FetchAndReturnNewReviewHashes(remote, notesRefPattern, } return updatedReviews, nil } + +func (repo *GitRepo) GetColorBool(name string) (bool, error) { + err := repo.runGitCommandInline("config", "--get-colorbool", name) + return (err == nil), nil +} + +func (repo *GitRepo) GetColor(name, defaultValue string) (string, error) { + return repo.runGitCommand("config", "--get-color", name, defaultValue) +} diff --git a/repository/mock_repo.go b/repository/mock_repo.go index 2d8debe4..18b5f637 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -611,3 +611,11 @@ func (repo *mockRepoForTest) FetchAndReturnNewReviewHashes(remote, notesRefPatte archiveRefPattern string) ([]string, error) { return nil, nil } + +func (repo *mockRepoForTest) GetColorBool(name string) (bool, error) { + return false, nil +} + +func (repo *mockRepoForTest) GetColor(name, defaultValue string) (string, error) { + return "", nil +} diff --git a/repository/repo.go b/repository/repo.go index 91acd177..7836aba9 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -51,6 +51,12 @@ type Repo interface { // GetSubmitStrategy returns the way in which a review is submitted GetSubmitStrategy() (string, error) + // GetColorBool returns color setting for "name" (e.g. color.diff). + GetColorBool(name string) (bool, error) + + // GetColor returns color configured for "name" (e.g. color.diff.new). + GetColor(name, defaultValue string) (string, error) + // HasUncommittedChanges returns true if there are local, uncommitted changes. HasUncommittedChanges() (bool, error)