1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +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
func init() {
// Force colorizing for no TTY environments. It works for formatters but
// not for -help, initialization order issue
// Force colorizing for no TTY environments
if os.Getenv("REVIVE_FORCE_COLOR") == "1" {
color.NoColor = false
}
flag.Usage = func() {
fmt.Println(banner)
fmt.Println(getBanner())
originalUsage()
}
// command line help strings

View File

@ -10,11 +10,6 @@ import (
"github.com/olekukonko/tablewriter"
)
var (
errorEmoji = color.RedString("✘")
warningEmoji = color.YellowString("⚠")
)
var newLines = map[rune]bool{
0x000A: true,
0x000B: true,
@ -25,6 +20,14 @@ var newLines = map[rune]bool{
0x2029: true,
}
func getErrorEmoji() string {
return color.RedString("✘")
}
func getWarningEmoji() string {
return color.YellowString("⚠")
}
// Friendly is an implementation of the Formatter interface
// which formats the errors to JSON.
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) {
emoji := warningEmoji
emoji := getWarningEmoji()
if severity == lint.SeverityError {
emoji = errorEmoji
emoji = getErrorEmoji()
}
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) {
emoji := warningEmoji
emoji := getWarningEmoji()
if errors > 0 {
emoji = errorEmoji
emoji = getErrorEmoji()
}
problemsLabel := "problems"
if errors+warnings == 1 {

14
main.go
View File

@ -9,19 +9,25 @@ import (
"github.com/mgechev/revive/lint"
)
var logo = color.YellowString(` _ __ _____ _(_)__ _____
func getLogo() string {
return color.YellowString(` _ __ _____ _(_)__ _____
| '__/ _ \ \ / / \ \ / / _ \
| | | __/\ 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
Example:
%s
`, logo, call)
`, getLogo(), getCall())
}
func main() {
config := getConfig()