From 5140794b16b246d3177e2beb67859f1d1bcacc00 Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Tue, 26 Aug 2025 20:19:10 +0200 Subject: [PATCH] fix backtracking of line wrappers --- README.md | 9 +++------ example_test.go | 2 +- fprint.go | 1 + notation.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4846169..3fa8c6b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,5 @@ # Notation - print Go objects -[![Go Report Card](https://goreportcard.com/badge/github.com/aryszka/notation)](https://goreportcard.com/report/github.com/aryszka/notation) -[![codecov](https://codecov.io/gh/aryszka/notation/branch/master/graph/badge.svg?token=7M18MEAVQW)](https://codecov.io/gh/aryszka/notation) - This package can be used to print (or sprint) Go objects for debugging purposes, with optional wrapping (indentation) and optional type information. @@ -18,7 +15,7 @@ Notation differs from these primarily in the 'flavor' of printing and the packag ### Installation -`go get github.com/aryszka/notation` +`go get code.squareroundforest.org/arpio/notation` ### Usage @@ -44,8 +41,8 @@ Wrapping is **not eager**. It doesn't wrap a line when it can fit on 72 columns. to 112 columns, when the output is considered to be more readable that way. This means very simple Go objects are not wrapped even with the **`w`** variant of the functions. -For the available functions, see also the [godoc](https://godoc.org/github.com/aryszka/notation). -(Alternatively: [pkg.go.dev](https://pkg.go.dev/github.com/aryszka/notation).) +For the available functions, see also the [godoc](https://godoc.org/code.squareroundforest.org/arpio/notation). +(Alternatively: [pkg.go.dev](https://pkg.go.dev/squareroundforest.org/arpio/notation).) ### Example diff --git a/example_test.go b/example_test.go index 027c2f9..704ebea 100644 --- a/example_test.go +++ b/example_test.go @@ -3,7 +3,7 @@ package notation_test import ( "os" - "github.com/aryszka/notation" + "code.squareroundforest.org/arpio/notation" ) type bike struct { diff --git a/fprint.go b/fprint.go index ec75619..4e772f1 100644 --- a/fprint.go +++ b/fprint.go @@ -290,6 +290,7 @@ func wrapNode(t, cf0, c0, c1 int, n node) node { // cl := cf0 - t var w int + part.lineEnds = nil for j, nj := range part.items { if w > 0 && w+len(part.sep)+nj.len > cl { part.lineEnds = append(part.lineEnds, j) diff --git a/notation.go b/notation.go index cfef565..3173353 100644 --- a/notation.go +++ b/notation.go @@ -181,6 +181,16 @@ func fprintValues(w io.Writer, o opts, v []interface{}) (int, error) { return wr.n, wr.err } +func fprintlnValues(w io.Writer, o opts, v []interface{}) (int, error) { + n, err := fprintValues(w, o, v) + if err != nil { + return n, err + } + + nn, err := w.Write([]byte("\n")) + return n + nn, err +} + func printValues(o opts, v []interface{}) (int, error) { return fprintValues(stderr, o, v) } @@ -237,6 +247,42 @@ func Fprintwv(w io.Writer, v ...interface{}) (int, error) { return fprintValues(w, wrap|allTypes, v) } +// Fprintln prints the provided objects to the provided writer with a closing newline. When multiple objects are printed, they'll be +// separated by a space. +func Fprintln(w io.Writer, v ...interface{}) (int, error) { + return fprintlnValues(w, none, v) +} + +// Fprintlnw prints the provided objects to the provided writer with a closing newline, with wrapping (and indentation) where necessary. +// When multiple objects are printed, they'll be separated by a newline. +func Fprintlnw(w io.Writer, v ...interface{}) (int, error) { + return fprintlnValues(w, wrap, v) +} + +// Fprintlnt prints the provided objects to the provided writer with a closing newline, with moderate type information. When multiple +// objects are printed, they'll be separated by a space. +func Fprintlnt(w io.Writer, v ...interface{}) (int, error) { + return fprintlnValues(w, types, v) +} + +// Fprintlnwt prints the provided objects to the provided writer with a closing newline, with wrapping (and indentation) where necessary, +// and with moderate type information. When multiple objects are printed, they'll be separated by a newline. +func Fprintlnwt(w io.Writer, v ...interface{}) (int, error) { + return fprintlnValues(w, wrap|types, v) +} + +// Fprintv prints the provided objects to the provided writer with a closing newline, with verbose type information. When multiple +// objects are printed, they'll be separated by a space. +func Fprintlnv(w io.Writer, v ...interface{}) (int, error) { + return fprintlnValues(w, allTypes, v) +} + +// Fprintlnwv prints the provided objects to the provided writer with a closing newline, with wrapping (and indentation) where necessary, +// and with verbose type information. When multiple objects are printed, they'll be separated by a newline. +func Fprintlnwv(w io.Writer, v ...interface{}) (int, error) { + return fprintlnValues(w, wrap|allTypes, v) +} + // Print prints the provided objects to stderr. When multiple objects are printed, they'll be separated by a // space. func Print(v ...interface{}) (int, error) {