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

wrapWords seems to ignore linebreaks when wrapping, leaving breaks in the wrong place #14579

Closed
maxgrenderjones opened this issue Jun 6, 2020 · 2 comments

Comments

@maxgrenderjones
Copy link
Contributor

maxgrenderjones commented Jun 6, 2020

I want to use wrapwords to ensure that some text is not more than x chars wide. I would expect that either:

  1. Existing line breaks are elided to make sure that the lines are as long as they can be without being wider than x
  2. Existing line breaks are honored, so any lines shorter than x are not changed

I get a third behaviour - lines are broken every x characters regardless of whether a newline has been seen in the meantime.

Example

import std/wordwrap

import unittest

const input = """
This is a long string. It is manually wrapped to 60
characters. I would not expect it to be changed by
wordwrap if wordwrap is set to wrap at 80 characters
"""

suite "Test Wrap":
    test "Rewrap":
        check(wrapWords(input)==input)

Current Output

Check failed: wrapWords(input) == input
    wrapWords(input) was This is a long string. It is manually wrapped to 60
characters. That means it
should not be changed by
wordwrap if wordwrap is set to wrap at 80 characters
    input was This is a long string. It is manually wrapped to 60
characters. That means it should not be changed by
wordwrap if wordwrap is set to wrap at 80 characters

  [FAILED] Rewrap

Expected Output

Test passes

Is this a bug or a feature? If a feature, it might help to document it. If a bug, what is the desired/expected behaviour?

> nim -v
Nim Compiler Version 1.3.5 [MacOSX: amd64]
Compiled at 2020-05-28
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release
@maxgrenderjones
Copy link
Contributor Author

Fwiw, python's answer is option 1

#!/usr/bin/python3
import textwrap

input = """
This is a long string. It is manually wrapped to 60
characters. I would not expect it to be changed by
wordwrap if wordwrap is set to wrap at 80 characters
""".strip()

rewrapped = """
This is a long string. It is manually wrapped to 60 characters. I
would not expect it to be changed by wordwrap if wordwrap is set to
wrap at 80 characters
""".strip()

def test_wrap():
    assert textwrap.fill(input) == rewrapped

@Araq
Copy link
Member

Araq commented Jun 8, 2020

We'll follow Python here too.

Araq added a commit that referenced this issue Jun 8, 2020
@Araq Araq closed this as completed in c308c2e Jun 8, 2020
narimiran pushed a commit that referenced this issue Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants