lithammer / dedent

Remove any common leading whitespace from multiline strings
https://pkg.go.dev/github.com/lithammer/dedent
MIT License
101 stars 9 forks source link

does not remove initial new line #20

Open RafalSkolasinski opened 1 year ago

RafalSkolasinski commented 1 year ago

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 commented 1 year ago

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

RafalSkolasinski commented 1 year ago

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

lithammer commented 1 year ago

Yeah I guess there's a few options 🤔

krader1961 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

bersace commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 7 months ago

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 commented 4 months ago

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