Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not remove initial new line #20

Open
RafalSkolasinski opened this issue Feb 21, 2023 · 11 comments
Open

does not remove initial new line #20

RafalSkolasinski opened this issue Feb 21, 2023 · 11 comments

Comments

@RafalSkolasinski
Copy link

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.
@lithammer
Copy link
Owner

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

@RafalSkolasinski
Copy link
Author

RafalSkolasinski commented Feb 21, 2023

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

@lithammer
Copy link
Owner

Yeah I guess there's a few options 🤔

@krader1961
Copy link

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
Copy link

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, "")
    }

@RafalSkolasinski
Copy link
Author

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

@bersace
Copy link

bersace commented Feb 22, 2024

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:]

@krader1961
Copy link

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.

@bersace
Copy link

bersace commented Feb 22, 2024

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.

@RafalSkolasinski
Copy link
Author

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?

@justinfx
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants