Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<a href="https://goreportcard.com/report/github.com/goforj/godump"><img src="https://goreportcard.com/badge/github.com/goforj/godump" alt="Go Report Card"></a>
<a href="https://codecov.io/gh/goforj/godump" ><img src="https://codecov.io/gh/goforj/godump/graph/badge.svg?token=ULUTXL03XC"/></a>
<!-- test-count:embed:start -->
<img src="https://img.shields.io/badge/tests-127-brightgreen" alt="Tests">
<img src="https://img.shields.io/badge/tests-130-brightgreen" alt="Tests">
<!-- test-count:embed:end -->
<a href="https://github.com/avelino/awesome-go?tab=readme-ov-file#parsersencodersdecoders"><img src="https://awesome.re/mentioned-badge-flat.svg" alt="Mentioned in Awesome Go"></a>
</p>
Expand Down Expand Up @@ -236,7 +236,7 @@ If a pointer has already been printed:
| **Dump** | [Dd](#dd) [Dump](#dump) [DumpStr](#dumpstr) [Fdump](#fdump) |
| **HTML** | [DumpHTML](#dumphtml) |
| **JSON** | [DumpJSON](#dumpjson) [DumpJSONStr](#dumpjsonstr) |
| **Options** | [WithDisableStringer](#withdisablestringer) [WithMaxDepth](#withmaxdepth) [WithMaxItems](#withmaxitems) [WithMaxStringLen](#withmaxstringlen) [WithSkipStackFrames](#withskipstackframes) [WithWriter](#withwriter) [WithoutColor](#withoutcolor) |
| **Options** | [WithDisableStringer](#withdisablestringer) [WithMaxDepth](#withmaxdepth) [WithMaxItems](#withmaxitems) [WithMaxStringLen](#withmaxstringlen) [WithoutHeader](#withoutheader) [WithSkipStackFrames](#withskipstackframes) [WithWriter](#withwriter) [WithoutColor](#withoutcolor) |


## Builder
Expand Down Expand Up @@ -581,6 +581,17 @@ d.Dump(v)
// "hello…" #string
```

### <a id="withoutheader"></a>WithoutHeader

WithoutHeader disables printing the source location header.

```go
// Default: false
d := godump.NewDumper(godump.WithoutHeader())
d.Dump("hello")
// "hello" #string
```

### <a id="withskipstackframes"></a>WithSkipStackFrames

WithSkipStackFrames skips additional stack frames for header reporting.
Expand Down
3 changes: 3 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ func (d *Dumper) dumpStrNoHeader(vs ...any) string {

// printDiffHeader writes the diff header line when a caller frame is available.
func (d *Dumper) printDiffHeader(out io.Writer) {
if d.disableHeader {
return
}
file, line := d.findFirstNonInternalFrame(d.skippedStackFrames)
if file == "" {
return
Expand Down
10 changes: 10 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func TestDiffStrNoColor(t *testing.T) {
assert.Contains(t, out, `+ "b" #string`)
}

func TestDiffStrNoHeader(t *testing.T) {
d := NewDumper(WithoutHeader())
d.colorizer = colorizeUnstyled
out := d.DiffStr(1, 2)
out = stripANSI(out)
assert.NotContains(t, out, "<#diff")
assert.Contains(t, out, "- 1")
assert.Contains(t, out, "+ 2")
}

func TestDiffHTMLNoColor(t *testing.T) {
out := NewDumper(WithoutColor()).DiffHTML("a", "b")
assert.NotContains(t, out, `<span style="color:`)
Expand Down
16 changes: 16 additions & 0 deletions examples/withoutheader/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build ignore
// +build ignore

package main

import "github.com/goforj/godump"

func main() {
// WithoutHeader disables printing the source location header.

// Example: disable header
// Default: false
d := godump.NewDumper(godump.WithoutHeader())
d.Dump("hello")
// "hello" #string
}
20 changes: 20 additions & 0 deletions godump.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type Dumper struct {
skippedStackFrames int
disableStringer bool
disableColor bool
disableHeader bool

// callerFn is used to get the caller information.
// It defaults to [runtime.Caller], it is here to be overridden for testing purposes.
Expand Down Expand Up @@ -270,6 +271,22 @@ func WithoutColor() Option {
}
}

// WithoutHeader disables printing the source location header.
// @group Options
//
// Example: disable header
//
// // Default: false
// d := godump.NewDumper(godump.WithoutHeader())
// d.Dump("hello")
// // "hello" #string
func WithoutHeader() Option {
return func(d *Dumper) *Dumper {
d.disableHeader = true
return d
}
}

// NewDumper creates a new Dumper with the given options applied.
// Defaults are used for any setting not overridden.
// @group Builder
Expand Down Expand Up @@ -553,6 +570,9 @@ func (d *Dumper) ensureColorizer() {

// printDumpHeader prints the header for the dump output, including the file and line number.
func (d *Dumper) printDumpHeader(out io.Writer) {
if d.disableHeader {
return
}
file, line := d.findFirstNonInternalFrame(d.skippedStackFrames)
if file == "" {
return
Expand Down
6 changes: 6 additions & 0 deletions godump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ func TestDumpStrNoColor(t *testing.T) {
assert.Contains(t, out, `"x"`)
}

func TestDumpStrNoHeader(t *testing.T) {
out := newDumperT(t, WithoutHeader()).DumpStr("x")
assert.NotContains(t, out, "<#dump")
assert.Contains(t, out, `"x"`)
}

func TestDiffStr(t *testing.T) {
type User struct {
Name string
Expand Down