Skip to content

Commit fe8da21

Browse files
committed
fix: do not colorize listing if color is disabled
1 parent 9736eb3 commit fe8da21

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

cmd/ogen/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ func generate(data []byte, packageName, targetDir string, clean bool, opts gen.O
9292
return nil
9393
}
9494

95-
func handleGenerateError(w io.Writer, specPath string, data []byte, err error) (r bool) {
95+
func handleGenerateError(w io.Writer, color bool, specPath string, data []byte, err error) (r bool) {
9696
defer func() {
9797
// Add trailing newline to the error message if error is handled.
9898
if r {
9999
_, _ = fmt.Fprintln(w)
100100
}
101101
}()
102102

103-
if location.PrintPrettyError(w, specPath, data, err) {
103+
if location.PrintPrettyError(w, color, specPath, data, err) {
104104
return true
105105
}
106106

@@ -287,7 +287,7 @@ func run() error {
287287
}
288288

289289
if err := generate(data, *packageName, *targetDir, *clean, opts); err != nil {
290-
if handleGenerateError(os.Stderr, fileName, data, err) {
290+
if handleGenerateError(os.Stderr, logOptions.Color, fileName, data, err) {
291291
return errors.New("generation failed")
292292
}
293293
return errors.Wrap(err, "generate")

gen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func TestNegative(t *testing.T) {
169169
a.Error(err)
170170

171171
var buf strings.Builder
172-
if location.PrintPrettyError(&buf, name, data, err) {
172+
if location.PrintPrettyError(&buf, true, name, data, err) {
173173
t.Log(buf.String())
174174
} else {
175175
t.Logf("%+v", err)

internal/location/pretty_error.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// PrintPrettyError prints the error in a pretty way and returns true if it was printed successfully.
12-
func PrintPrettyError(w io.Writer, filename string, data []byte, err error) bool {
12+
func PrintPrettyError(w io.Writer, color bool, filename string, data []byte, err error) bool {
1313
// TODO(tdakkota): make it configurable?
1414
const (
1515
printLimit = 5
@@ -24,6 +24,9 @@ func PrintPrettyError(w io.Writer, filename string, data []byte, err error) bool
2424
Filename: filename,
2525
Context: context,
2626
}
27+
if !color {
28+
opts = opts.WithoutColor()
29+
}
2730
_ = lines.PrintListing(w, msg, loc, opts)
2831
}
2932

internal/location/print.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ type PrintListingOptions struct {
4747
PlainColor *color.Color
4848
}
4949

50+
// WithoutColor creates a copy of the options with disabled color.
51+
func (o PrintListingOptions) WithoutColor() PrintListingOptions {
52+
o.ErrColor = color.New(color.Reset)
53+
o.PlainColor = color.New(color.Reset)
54+
return o
55+
}
56+
5057
func (o PrintListingOptions) contextLines(errLine int) (padNum, top, bottom int) {
5158
context := o.Context
5259

jsonschema/jsonschema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestNegative(t *testing.T) {
3838
a.Error(err)
3939

4040
var buf strings.Builder
41-
ok := location.PrintPrettyError(&buf, name, data, err)
41+
ok := location.PrintPrettyError(&buf, true, name, data, err)
4242
// Ensure that the error message is pretty printed.
4343
//
4444
// There should be a good reason to remove this line.

openapi/parser/parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestNegative(t *testing.T) {
3636
a.Error(err)
3737

3838
var buf strings.Builder
39-
ok := location.PrintPrettyError(&buf, name, data, err)
39+
ok := location.PrintPrettyError(&buf, true, name, data, err)
4040
// Ensure that the error message is pretty printed.
4141
//
4242
// There should be a good reason to remove this line.

0 commit comments

Comments
 (0)