Skip to content

Commit 8a597d4

Browse files
committed
updated readme examples
1 parent d625409 commit 8a597d4

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ printer := &pretty.Printer{
166166
},
167167
}, true
168168
}
169-
return nil, false
169+
return pretty.AsPrintable(v) // Use default
170170
},
171171
}
172172

@@ -188,8 +188,7 @@ printer := &pretty.Printer{
188188
},
189189
}, true
190190
}
191-
192-
return nil, false
191+
return pretty.AsPrintable(v) // Use default
193192
},
194193
}
195194

@@ -199,9 +198,9 @@ printer.Println("a sensitive string")
199198

200199
**Note:** If `Printer.AsPrintable` is not set, the package-level `AsPrintable` function is used, which checks if the value implements the `Printable` interface.
201200

202-
### Global Configuration with DefaultPrinter
201+
### Integration with go-errs
203202

204-
The `DefaultPrinter` variable is used by all package-level print functions (`pretty.Sprint`, `pretty.Println`, etc.) and can be configured globally. This is especially useful when you want to customize formatting for third-party packages that use go-pretty (like `go-errs` for error formatting) without implementing the `Printable` interface on your types.
203+
The `go-errs` package uses a configurable `Printer` variable (of type `*pretty.Printer`) for formatting function parameters in error call stacks. You can customize this printer to mask secrets, adapt types, or change formatting without implementing the `Printable` interface on your types.
205204

206205
**Use cases:**
207206
- Hide sensitive data (secrets, passwords, tokens) in error messages and stack traces
@@ -215,12 +214,22 @@ import (
215214
"io"
216215
"reflect"
217216
"strings"
217+
"github.com/domonda/go-errs"
218218
"github.com/domonda/go-pretty"
219219
)
220220

221+
// Wrapper that adapts any interface to pretty.Printable
222+
type printableAdapter struct {
223+
format func(io.Writer)
224+
}
225+
226+
func (p printableAdapter) PrettyPrint(w io.Writer) {
227+
p.format(w)
228+
}
229+
221230
func init() {
222-
// Configure the global DefaultPrinter used by go-errs and other packages
223-
pretty.DefaultPrinter.AsPrintable = func(v reflect.Value) (pretty.Printable, bool) {
231+
// Configure the Printer used by go-errs for error call stacks
232+
errs.Printer.AsPrintable = func(v reflect.Value) (pretty.Printable, bool) {
224233
// Mask sensitive strings
225234
if v.Kind() == reflect.String {
226235
str := v.String()
@@ -249,7 +258,7 @@ func init() {
249258
}
250259
}
251260

252-
return nil, false
261+
return pretty.AsPrintable(v) // Use default
253262
}
254263
}
255264

@@ -258,12 +267,12 @@ func init() {
258267
```
259268

260269
This approach allows you to:
261-
1. **Centrally control** how all values are formatted in your application
270+
1. **Centrally control** how all values are formatted in error call stacks
262271
2. **Protect sensitive data** in logs, error traces, and debug output
263-
3. **Customize third-party package behavior** without modifying their code
272+
3. **Customize error formatting** without modifying go-errs code
264273
4. **Apply formatting rules** to types you don't control
265274

266-
**Note:** If `Printer.AsPrintable` is not set, the package-level `AsPrintable` function is used, which checks if the value implements the `Printable` interface.
275+
**Note:** The `errs.Printer` variable is a `*pretty.Printer` that can be fully configured with custom settings like `MaxStringLength`, `MaxErrorLength`, `MaxSliceLength`, and `AsPrintable`.
267276

268277
## Output Examples
269278

0 commit comments

Comments
 (0)