diff --git a/README.md b/README.md index 570a9d9..d51fcbe 100644 --- a/README.md +++ b/README.md @@ -147,10 +147,13 @@ func main() { // Custom Dumper with all options set explicitly d := godump.NewDumper( - godump.WithMaxDepth(15), // default: 15 - godump.WithMaxItems(100), // default: 100 - godump.WithMaxStringLen(100000), // default: 100000 - godump.WithWriter(os.Stdout), // default: os.Stdout + godump.WithMaxDepth(15), // default: 15 + godump.WithMaxItems(100), // default: 100 + godump.WithMaxStringLen(100000), // default: 100000 + godump.WithWriter(os.Stdout), // default: os.Stdout + godump.WithSkipStackFrames(0), // default: 0 + godump.WithDisableStringer(false), // default: false + godump.WithHeader(true), // default: true ) // Use the custom dumper diff --git a/godump.go b/godump.go index 68564b1..dd30a32 100644 --- a/godump.go +++ b/godump.go @@ -34,6 +34,7 @@ const ( defaultMaxItems = 100 defaultMaxStringLen = 100000 defaultMaxStackDepth = 10 + defaultShowHeader = true initialCallerSkip = 2 ) @@ -92,6 +93,7 @@ type Dumper struct { writer io.Writer skippedStackFrames int disableStringer bool + showHeader bool // callerFn is used to get the caller information. // It defaults to [runtime.Caller], it is here to be overridden for testing purposes. @@ -167,6 +169,14 @@ func WithDisableStringer(b bool) Option { } } +// WithHeader configures whether to print the header with the call location. +func WithHeader(show bool) Option { + return func(d *Dumper) *Dumper { + d.showHeader = show + return d + } +} + // NewDumper creates a new Dumper with the given options applied. // Defaults are used for any setting not overridden. func NewDumper(opts ...Option) *Dumper { @@ -175,6 +185,7 @@ func NewDumper(opts ...Option) *Dumper { maxItems: defaultMaxItems, maxStringLen: defaultMaxStringLen, disableStringer: defaultDisableStringer, + showHeader: defaultShowHeader, writer: os.Stdout, colorizer: nil, // ensure no detection is made if we don't need it callerFn: runtime.Caller, @@ -208,7 +219,9 @@ func DumpStr(vs ...any) string { // DumpStr returns a string representation of the values with colorized output. func (d *Dumper) DumpStr(vs ...any) string { var sb strings.Builder - d.printDumpHeader(&sb) + if d.showHeader { + d.printDumpHeader(&sb) + } tw := tabwriter.NewWriter(&sb, 0, 0, 1, ' ', 0) d.writeDump(tw, vs...) tw.Flush() diff --git a/godump_test.go b/godump_test.go index 1f9c448..694a99e 100644 --- a/godump_test.go +++ b/godump_test.go @@ -1059,3 +1059,12 @@ func TestDisableStringer(t *testing.T) { v = d.DumpStr(data) assert.Contains(t, v, `-secret => 👻 hidden stringer`) } + +func TestWithHeader(t *testing.T) { + out := dumpStrT(t, "x") + assert.Contains(t, out, "<#dump //", "expected header to be present") + + d := newDumperT(t, WithHeader(false)) + out = d.DumpStr("x") + assert.NotContains(t, out, "<#dump //", "expected header to be disabled") +}