Skip to content

Commit

Permalink
fix(color): moved global color calls to separate functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tymonx authored and mgechev committed May 14, 2020
1 parent 3bba955 commit 2e98c7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
5 changes: 2 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions formatter/friendly.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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)}}))
}
Expand All @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2e98c7c

Please sign in to comment.