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

Remove trailing empty newline from rendered multi-line strings #1018

Closed
tgdfool2 opened this issue Sep 27, 2022 · 3 comments
Closed

Remove trailing empty newline from rendered multi-line strings #1018

tgdfool2 opened this issue Sep 27, 2022 · 3 comments

Comments

@tgdfool2
Copy link

Hi All,

Is there an easy way to remove the trailing empty newline that is added in multi-line strings?

Given the following code:

local script = |||
  case "$ver" in
    %(current)s)
      echo "Same"
      exit 0
      ;;
    %(upgrade)s
    *)
      echo "Wrong"
      exit 2
      ;;
  esac
|||;

script % {
  current: '10',
  target: '20',
  upgrade: (if $.current != '20' then
  |||
    %(target)s)
        echo "Different"
        exit 1
        ;;
  ||| % { current: $.current, target: $.target } else ''),
}

It produces the following output:

case "$ver" in
  10)
    echo "Same"
    exit 0
    ;;
  20)
    echo "Different"
    exit 1
    ;;

  *)
    echo "Wrong"
    exit 2
    ;;
esac

I would like to get rid of the empty line that was added between the 20 and * cases. Is there an easy way to do this?

Thanks in advance for your help!

@sparkprime
Copy link
Contributor

sparkprime commented Sep 27, 2022

The best way right now is probably to write a function that strips the trailing newline and call that around the |||.

Related FRs:

#289
#915

@tgdfool2
Copy link
Author

Thanks for your answer and your hint :-). This one did the trick:

  upgrade: (if $.current != '2.0' then
    std.rstripChars(|||
      %(target)s)
          echo "Different"
          exit 1
          ;;
    ||| % { current: $.current, target: $.target }, '\n') else ''),

@vergenzt
Copy link

FYI for those following I implemented a "chomped multiline string" syntax in a PR at #1175. Would love some feedback on it and/or to get it merged! 🙂

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

4 participants