icecream icon indicating copy to clipboard operation
icecream copied to clipboard

Handling of \n

Open ICreedenI opened this issue 3 years ago • 2 comments

When a string contains '\n' icecream interprets it as a new line, even if the string actually contains '\\n' or equivalent. I would expect the same behavior print shows.

Expected behavior:

ic(r'C:\Users\new test')

ic| r'C:\Users\new test': 'C:\Users\new test'

ic('C:\\Users\\new test')

ic| 'C:\Users\new test': 'C:\Users\new test'

Actual behavior:

ic(r'C:\Users\new test')

ic| r'C:\Users\new test': 'C:\Users
                                      ew test'

ic('C:\\Users\\new test')

ic| 'C:\Users\new test': 'C:\Users
                                     ew test'

Possible Quick Fix (creates new problem)

Changing

@singledispatch
def argumentToString(obj):
    s = DEFAULT_ARG_TO_STRING_FUNCTION(obj)
    s = s.replace('\\n', '\n')  # Preserve string newlines in output.
    return s

to

@singledispatch
def argumentToString(obj):
    if type(obj) == str:
        return obj
    else:
        s = DEFAULT_ARG_TO_STRING_FUNCTION(obj)
        s = s.replace('\\n', '\n')  # Preserve string newlines in output.
        return s

in icecream.py line 181 onwards will get rid of this speciific problem, but there will be no coloring.

The chosen example of a path shows the problem of this behavior. Especially when paths are entered, the usability suffers. A possible solution would be the introduction of a new parameter that allows the choice of behavior. Of course, this would only circumvent the problem.

ICreedenI avatar Sep 07 '22 03:09 ICreedenI

The issue has the same roots as the attached one.

Jakeroid avatar May 15 '25 05:05 Jakeroid

There are some updates due this issue: https://github.com/gruns/icecream/releases/tag/untagged-19e3d02255ee5494672f

@ICreedenI

Jakeroid avatar May 31 '25 16:05 Jakeroid

The update solves that: https://github.com/gruns/icecream/releases/tag/v2.1.5

Jakeroid avatar Jun 26 '25 09:06 Jakeroid