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

New std-rfc command str dedent (a.k.a. unindent) to remove common indentation from a multiline string #894

Merged
merged 5 commits into from
Jul 16, 2024

Conversation

NotTheDr01ds
Copy link
Contributor

@NotTheDr01ds NotTheDr01ds commented Jul 16, 2024

Finally got around to polishing up and contributing the concept from Nushell #11477.

This is based on a (very slightly) modified version of the Swift approach suggested by @jameschensmith:

> use std-rfc str
> let s = "   
    Totally unindented
           
      Indented by two spaces
        Indented by four spaces
      Intended by two spaces

    Unindented

    "
> $s | str dedent
Totally unindented

  Indented by two spaces
    Indented by four spaces
  Intended by two spaces

Unindented

Specifically:

  • Requires a multi-line string where the first and last lines are blank (empty or contains only whitespace)
  • Will error if the first and/or last line are not blank
  • The amount of indentation to remove is based solely on the number of spaces on the final line of the string
  • Tabs are not considered for indentation calculations
  • The first and last line of the original string are not returned as part of the dedented string. Those lines are for "control"/formatting purposes only.
  • If you want the string to end in a linebreak, simply add an extra blank line before the last "control" line (as in the example above).
  • Unlike the Swift format, whitespace is allowed on the first line of the string. It may not be visible to the user in their IDE, and there's just no reason to create an error in this case since the line is intended to be stripped anyway.
  • Only indentation whitespace is removed. Any other whitespace is left untouched, including that on otherwise empty lines.

A good (I hope) set of examples for these rules can be found in the test cases.

@fdncred
Copy link
Collaborator

fdncred commented Jul 16, 2024

Interesting! Thanks.

@fdncred fdncred merged commit ca309b9 into nushell:main Jul 16, 2024
1 check passed
@NotTheDr01ds NotTheDr01ds deleted the str_dedent branch July 16, 2024 18:03
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

Successfully merging this pull request may close these issues.

2 participants