dedent icon indicating copy to clipboard operation
dedent copied to clipboard

does not remove initial new line

Open RafalSkolasinski opened this issue 2 years ago • 11 comments

running code from readme does not seem to remove the initial empty line

$ cat dedent.go                                                     
//go:build exclude

package main

import (
	"fmt"

	"github.com/lithammer/dedent"
)

func main() {
	s := `
		Lorem ipsum dolor sit amet,
		consectetur adipiscing elit.
		Curabitur justo tellus, facilisis nec efficitur dictum,
		fermentum vitae ligula. Sed eu convallis sapien.`
	fmt.Println(dedent.Dedent(s))
	fmt.Println("-------------")
	fmt.Println(s)
}

$go run dedent.go                                                                 

Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Curabitur justo tellus, facilisis nec efficitur dictum,
fermentum vitae ligula. Sed eu convallis sapien.
-------------

		Lorem ipsum dolor sit amet,
		consectetur adipiscing elit.
		Curabitur justo tellus, facilisis nec efficitur dictum,
		fermentum vitae ligula. Sed eu convallis sapien.

RafalSkolasinski avatar Feb 21 '23 19:02 RafalSkolasinski

Yeah related to #7, #14 and #17. I just haven't decided on the matter since it's technically a breaking change.

lithammer avatar Feb 21 '23 22:02 lithammer

Could be handled via optional argument? Or maybe another function dedent.DedentN (or sth with a better ring to it)

RafalSkolasinski avatar Feb 21 '23 23:02 RafalSkolasinski

Yeah I guess there's a few options 🤔

lithammer avatar Feb 28 '23 07:02 lithammer

FWIW, I found this package when I needed a way to write multiline literal strings with leading tabs for unit tests in a readable manner. That an initial newline isn't removed makes this implementation unpleasant to use since I have to write something like this:

        Prints(dedent.Dedent(`        [
             &abc=  y
             &def=  z
             &xyz=  x
            ]
        `)),

Rather than this (the only difference being the placement of the opening bracket:

        Prints(dedent.Dedent(`
            [
             &abc=  y
             &def=  z
             &xyz=  x
            ]
        `)),

krader1961 avatar Apr 25 '23 01:04 krader1961

For the record, the solution I settled on is the obvious one. At the top of the Dedent function replace

    text = whitespaceOnly.ReplaceAllString(text, "")

with

    if text[0] == '\n' {
        text = whitespaceOnly.ReplaceAllString(text[1:], "")
    } else {
        text = whitespaceOnly.ReplaceAllString(text, "")
    }

krader1961 avatar May 05 '23 01:05 krader1961

Thanks @krader1961 - this seems to work perfectly. I applied that in my fork to keep handy around until fix lands in main repo.

RafalSkolasinski avatar May 06 '23 23:05 RafalSkolasinski

I just append [1:] after Dedent() if I don't want the first newline. Like beginning string with \ in Python herestring.

query := dedent.Dedent(`
    SELECT *
    FROM t1, t2
    WHERE t1.col > 2000;
`)[1:]

bersace avatar Feb 22 '24 08:02 bersace

I just append [1:] after Dedent() if I don't want the first newline.

@bersace Yes, that works but is less efficient and requires the user to explicitly remove the newline prefix. Which, in the context of this package, shouldn't be necessary.

krader1961 avatar Feb 22 '24 08:02 krader1961

I just append [1:] after Dedent() if I don't want the first newline.

@bersace Yes, that works but is less efficient and requires the user to explicitly remove the newline prefix. Which, in the context of this package, shouldn't be necessary.

Yes, this is a workaround. But I prefer to handle it when producing a string with Dedent rather than presuming any string starting with a \n is dedented. The latter is an inversion of control. It depends on your use case.

bersace avatar Feb 22 '24 08:02 bersace

I have the feeling that adding a new function that skips initial new line would be a good compromise preserving behaviour / explicitness to folks that prefer it and nice short-cut for these who want to skip new line.

@lithammer I am happy to put in PR with the change if you think this is a right approach?

RafalSkolasinski avatar Apr 04 '24 11:04 RafalSkolasinski

Hit this as well. I ended up wrapping it as strings.Trim(dedent.Dedent(s)))

justinfx avatar Jun 18 '24 21:06 justinfx