got icon indicating copy to clipboard operation
got copied to clipboard

How can i remove end of line characters at the end

Open ymolists opened this issue 11 months ago • 7 comments

Hi.

Thank you so much for providing this tool.

I am trying to trim the end of lines \n that is added at the end of each line. I did not find a way to do that from the documentation. In go template you can say {{- xyz -}} which means trim white space from left and right of the template.

is there anything similar in got

Regards

ymolists avatar Jan 24 '25 12:01 ymolists

That can be a pain.

First, are you trying to trim from the ends of the template, or just the end of a line?

Usually what I do is open a tag before the end of the line, like so:

{{ Here is a line {{= outString }} {{= nextString}} }}

Its ugly, but it gets the job done.

If you have a particular example and some suggestions of how it might work, I can noodle on it. It is a problem that comes up from time to time. Used to have a {{- n}} operator that would backup n characters, but that became difficult to implement at the time. We could look at something like that again, but as you said, perhaps some kind of join operator that simply would make sure the next newline was eliminated. Open for suggestions.

spekary avatar Jan 24 '25 16:01 spekary

Hey there. Thank you first for the quick answer.

At the bare minimum i want to remove the additional \n that is always added at the end of the line. End of the template is sugar on the cake. Is it too complicated to add {{- = it.name -}} without going for n characters. this is how go templates work already so people could be already familiar to the syntax. i think -n is asking too much.

Tbf i think the default should be to not add any new lines but that would break existing code :-( an alternative would be to come up with a way to say trim from right/left of the string. as in {{- = it.name -}} would that make sense ?

Regards

ymolists avatar Jan 24 '25 20:01 ymolists

More thoughts

Another option would be to introduce a new trimmed construct as in {{== it.operationId}} this tells the template that i have a string on the right and it should not be appended with a new line in the resulting template. could be any characters but i thought of == because it means be strict

ymolists avatar Jan 24 '25 21:01 ymolists

Can you put together an example of what kind of template you are having trouble with?

For example, in this template:

{{ Here {{= is }} An example }}

Since outside the {{= }} there is a newline, the template language just uses the newline. Are you saying this should be interpreted as:

{{ Here {{= is }} An example }}

  1. Why not just write it that way
  2. Should the newline after {{= ... }} be changed to a a space? When would you do that and when would you not?
  3. What about the newline in front of {{= }}.
  4. What about the newline after "example"?
  5. What about the newline in front of "Here"

The rules need to be well thought out and intuitive. And again, you now can do something like:

{{ Here {{= is }} An example }}

This becomes a bigger issue around {{for}} and {{if}} constructs.

spekary avatar Jan 24 '25 22:01 spekary

Here is my template iam trying to generate an open api spec in yaml.

func (it genOapiEndp) genOapiEndp(
  _w io.Writer,
) (err error) {
{{

  {{= it.path}}:
    {{= it.method}}:
      operationId: {{= it.operationId}}
      summary: {{= it.summary}}
      description: {{= it.description}}
{{if it.tags != "" }}
      tags:
{{= it.tags}}{{if}}
}}

  return // make sure the error gets returned
}

here is the generated code

Image

the problem is as you mentioned when inside an if block you cant control the end of line being inserted. so line 180 should not be inserted.

here is the generated code. i am trying to avoid that gap in between the blocks

  /cli/wflows/stages:
    post:
      operationId: /cli/wflows/stages/createTopology
      summary: Compile commands passed from the command line
      description: Compile commands passed from the command line
      tags:
        - cli

      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/cliStageSession'

ymolists avatar Jan 25 '25 01:01 ymolists

I don't think its the if, but rather I think the newline between the closing {{if}} and the closing }}. Some possible workarounds:

  • Make sure it.tags does not contain a newline at the end inside itself
  • Put the }} closing tag at the end of the line before with a space in between. That will end it with a space rather than a newline, which I assume is acceptable?

spekary avatar Jan 25 '25 16:01 spekary

I was able to narrow it down to this :

{{if !it.istype }}
{{= it.ind2}}  type: {{= it.typename}}{{else}}{{= it.ind2}}  $ref: '#/components/schemas/{{= it.typename}}' {{if}} }}

ideally this is what i wanted so i can see clearly which block will be added. it adds a new line any way else i tried

{{if !it.istype }}
{{= it.ind2}}  type: {{= it.typename}}
{{else}}
{{= it.ind2}}  $ref: '#/components/schemas/{{= it.typename}}'
{{if}} }}

I am lucky above as i can put everything in one line.

ymolists avatar Jan 25 '25 21:01 ymolists