1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-17 01:12:27 +02:00

fix(color): moved global color calls to separate functions

There is an initialization order issue when forcing colorizing.

Every calls from the fatich/color package must be done by a function
not during global variable initialization.
This commit is contained in:
Tymoteusz Blazejczyk
2020-05-14 22:53:13 +02:00
committed by Minko Gechev
parent 3bba955563
commit 2e98c7c63a
3 changed files with 24 additions and 16 deletions

View File

@ -249,14 +249,13 @@ var help bool
var originalUsage = flag.Usage var originalUsage = flag.Usage
func init() { func init() {
// Force colorizing for no TTY environments. It works for formatters but // Force colorizing for no TTY environments
// not for -help, initialization order issue
if os.Getenv("REVIVE_FORCE_COLOR") == "1" { if os.Getenv("REVIVE_FORCE_COLOR") == "1" {
color.NoColor = false color.NoColor = false
} }
flag.Usage = func() { flag.Usage = func() {
fmt.Println(banner) fmt.Println(getBanner())
originalUsage() originalUsage()
} }
// command line help strings // command line help strings

View File

@ -10,11 +10,6 @@ import (
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
) )
var (
errorEmoji = color.RedString("✘")
warningEmoji = color.YellowString("⚠")
)
var newLines = map[rune]bool{ var newLines = map[rune]bool{
0x000A: true, 0x000A: true,
0x000B: true, 0x000B: true,
@ -25,6 +20,14 @@ var newLines = map[rune]bool{
0x2029: true, 0x2029: true,
} }
func getErrorEmoji() string {
return color.RedString("✘")
}
func getWarningEmoji() string {
return color.YellowString("⚠")
}
// Friendly is an implementation of the Formatter interface // Friendly is an implementation of the Formatter interface
// which formats the errors to JSON. // which formats the errors to JSON.
type Friendly struct { type Friendly struct {
@ -68,9 +71,9 @@ func (f *Friendly) printFriendlyFailure(failure lint.Failure, severity lint.Seve
} }
func (f *Friendly) printHeaderRow(failure lint.Failure, severity lint.Severity) { func (f *Friendly) printHeaderRow(failure lint.Failure, severity lint.Severity) {
emoji := warningEmoji emoji := getWarningEmoji()
if severity == lint.SeverityError { if severity == lint.SeverityError {
emoji = errorEmoji emoji = getErrorEmoji()
} }
fmt.Print(f.table([][]string{{emoji, "https://revive.run/r#" + failure.RuleName, color.GreenString(failure.Failure)}})) fmt.Print(f.table([][]string{{emoji, "https://revive.run/r#" + failure.RuleName, color.GreenString(failure.Failure)}}))
} }
@ -85,9 +88,9 @@ type statEntry struct {
} }
func (f *Friendly) printSummary(errors, warnings int) { func (f *Friendly) printSummary(errors, warnings int) {
emoji := warningEmoji emoji := getWarningEmoji()
if errors > 0 { if errors > 0 {
emoji = errorEmoji emoji = getErrorEmoji()
} }
problemsLabel := "problems" problemsLabel := "problems"
if errors+warnings == 1 { if errors+warnings == 1 {

14
main.go
View File

@ -9,19 +9,25 @@ import (
"github.com/mgechev/revive/lint" "github.com/mgechev/revive/lint"
) )
var logo = color.YellowString(` _ __ _____ _(_)__ _____ func getLogo() string {
return color.YellowString(` _ __ _____ _(_)__ _____
| '__/ _ \ \ / / \ \ / / _ \ | '__/ _ \ \ / / \ \ / / _ \
| | | __/\ V /| |\ V / __/ | | | __/\ V /| |\ V / __/
|_| \___| \_/ |_| \_/ \___|`) |_| \___| \_/ |_| \_/ \___|`)
}
var call = color.MagentaString("revive -config c.toml -formatter friendly -exclude a.go -exclude b.go ./...") func getCall() string {
return color.MagentaString("revive -config c.toml -formatter friendly -exclude a.go -exclude b.go ./...")
}
var banner = fmt.Sprintf(` func getBanner() string {
return fmt.Sprintf(`
%s %s
Example: Example:
%s %s
`, logo, call) `, getLogo(), getCall())
}
func main() { func main() {
config := getConfig() config := getConfig()