Repository: ogen-go/ogen
Pull request #604 (2022-10-06 09:45:40)

fix: do not colorize listing if `color` is disabled


Patch:
diff --git a/cmd/ogen/main.go b/cmd/ogen/main.go
index 2fd49404..f8bd0f13 100644
--- a/cmd/ogen/main.go
+++ b/cmd/ogen/main.go
@@ -92,7 +92,7 @@ func generate(data []byte, packageName, targetDir string, clean bool, opts gen.O
 	return nil
 }
 
-func handleGenerateError(w io.Writer, specPath string, data []byte, err error) (r bool) {
+func handleGenerateError(w io.Writer, color bool, specPath string, data []byte, err error) (r bool) {
 	defer func() {
 		// Add trailing newline to the error message if error is handled.
 		if r {
@@ -100,7 +100,7 @@ func handleGenerateError(w io.Writer, specPath string, data []byte, err error) (
 		}
 	}()
 
-	if location.PrintPrettyError(w, specPath, data, err) {
+	if location.PrintPrettyError(w, color, specPath, data, err) {
 		return true
 	}
 
@@ -287,7 +287,7 @@ func run() error {
 	}
 
 	if err := generate(data, *packageName, *targetDir, *clean, opts); err != nil {
-		if handleGenerateError(os.Stderr, fileName, data, err) {
+		if handleGenerateError(os.Stderr, logOptions.Color, fileName, data, err) {
 			return errors.New("generation failed")
 		}
 		return errors.Wrap(err, "generate")
diff --git a/internal/location/pretty_error.go b/internal/location/pretty_error.go
index 0f96106f..4c609fad 100644
--- a/internal/location/pretty_error.go
+++ b/internal/location/pretty_error.go
@@ -9,7 +9,7 @@ import (
 )
 
 // PrintPrettyError prints the error in a pretty way and returns true if it was printed successfully.
-func PrintPrettyError(w io.Writer, filename string, data []byte, err error) bool {
+func PrintPrettyError(w io.Writer, color bool, filename string, data []byte, err error) bool {
 	// TODO(tdakkota): make it configurable?
 	const (
 		printLimit   = 5
@@ -24,6 +24,9 @@ func PrintPrettyError(w io.Writer, filename string, data []byte, err error) bool
 			Filename: filename,
 			Context:  context,
 		}
+		if !color {
+			opts = opts.WithoutColor()
+		}
 		_ = lines.PrintListing(w, msg, loc, opts)
 	}
 
diff --git a/internal/location/print.go b/internal/location/print.go
index 5079ffd6..663d826d 100644
--- a/internal/location/print.go
+++ b/internal/location/print.go
@@ -47,6 +47,13 @@ type PrintListingOptions struct {
 	PlainColor *color.Color
 }
 
+// WithoutColor creates a copy of the options with disabled color.
+func (o PrintListingOptions) WithoutColor() PrintListingOptions {
+	o.ErrColor = color.New(color.Reset)
+	o.PlainColor = color.New(color.Reset)
+	return o
+}
+
 func (o PrintListingOptions) contextLines(errLine int) (padNum, top, bottom int) {
 	context := o.Context
 


Test patch:
diff --git a/gen_test.go b/gen_test.go
index 3eacfb1f..89029481 100644
--- a/gen_test.go
+++ b/gen_test.go
@@ -169,7 +169,7 @@ func TestNegative(t *testing.T) {
 		a.Error(err)
 
 		var buf strings.Builder
-		if location.PrintPrettyError(&buf, name, data, err) {
+		if location.PrintPrettyError(&buf, true, name, data, err) {
 			t.Log(buf.String())
 		} else {
 			t.Logf("%+v", err)
diff --git a/jsonschema/jsonschema_test.go b/jsonschema/jsonschema_test.go
index 5ac92c01..aa7f9597 100644
--- a/jsonschema/jsonschema_test.go
+++ b/jsonschema/jsonschema_test.go
@@ -38,7 +38,7 @@ func TestNegative(t *testing.T) {
 		a.Error(err)
 
 		var buf strings.Builder
-		ok := location.PrintPrettyError(&buf, name, data, err)
+		ok := location.PrintPrettyError(&buf, true, name, data, err)
 		// Ensure that the error message is pretty printed.
 		//
 		// There should be a good reason to remove this line.
diff --git a/openapi/parser/parser_test.go b/openapi/parser/parser_test.go
index 95512f0f..002f7bc5 100644
--- a/openapi/parser/parser_test.go
+++ b/openapi/parser/parser_test.go
@@ -36,7 +36,7 @@ func TestNegative(t *testing.T) {
 		a.Error(err)
 
 		var buf strings.Builder
-		ok := location.PrintPrettyError(&buf, name, data, err)
+		ok := location.PrintPrettyError(&buf, true, name, data, err)
 		// Ensure that the error message is pretty printed.
 		//
 		// There should be a good reason to remove this line.