icecream icon indicating copy to clipboard operation
icecream copied to clipboard

Icecream separates Strings at whitespaces & '\\n'

Open JosuaCarl opened this issue 11 months ago • 7 comments

When I use

from icecream import ic
ic('A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file')

resulting in:

ic| 'A\veryvery\long\path\to\no\even\longer\Bioblank _01_Heritisfinallythe file.file': ('A\veryvery\long\path\to
o\even\longer\HelloWorld ' '_01_Heritisfinallythe file.file')

Two things happen:

  1. The escape before \n i.e \\n is ignored and a new line created
  2. The string is cut into little bits for representation

I tried to figure out where within my code the string does get turned into a tuple, but it turns out it does not, it's just displayed wrong. I know ('' '') is another way to write a sting, but I found it very confusing as it resembled a tuple closely.

JosuaCarl avatar Jan 28 '25 11:01 JosuaCarl

If you change quotes to double ones, then everything works as expected?

Jakeroid avatar May 02 '25 18:05 Jakeroid

No, I don't know why I mentioned this. It was months ago. But here is the output with double quotes:

ic("A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file")

ic| ('A\veryvery\long\path\to
o\even\longer\HelloWorld ' '_01_Heritisfinallythe file.file')

So, no changes there.

Bonus: Using raw Strings has the same display.

ic(r"A\veryvery\long\path\to\no\even\longer\HelloWorld _01_Heritisfinallythe file.file")

ic| ('A\veryvery\long\path\to
o\even\longer\HelloWorld ' '_01_Heritisfinallythe file.file')

JosuaCarl avatar May 05 '25 07:05 JosuaCarl

@JosuaCarl As far as I can tell, it looks like new lines on \n are a bug, but as for others, we have a line wrap width constant, and it's impossible to configure it right now.

I'm working on improvement for this.

Jakeroid avatar May 15 '25 05:05 Jakeroid

@JosuaCarl Workaround for you could be the following: ic.configureOutput(argToStringFunction=repr).

For the code like this:

from icecream import ic

ic.configureOutput(argToStringFunction=repr)


test1 = "A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file"
test2 = r"A\veryvery\long\path\to\no\even\longer\HelloWorld _01_Heritisfinallythe file.file"

# I have added backslach for each invalid sequence here
test3 = "A\veryvery\\long\\path\to\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file"

ic(test1)
ic(test2)
ic(test3)

It give output like this:

ic| test1: 'A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file'
ic| test2: 'A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file'
ic| test3: 'A\x0beryvery\\long\\path\to\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file'

I'm considering to make this behavior by default, coz it looks strange if you have a line with "\n" but as output you get actually new line in the place.

Jakeroid avatar May 15 '25 05:05 Jakeroid

I don't know whether it makes sense to always have the raw representation as an output. Especially when checking whether the strings are correct, this could be unhelpful behavior. Could the line not just be separated at the current line-width like this:

import os

str = "Some arbitrary string with almost arbitrary ~ \\n symbols" *10
width = os.get_terminal_size().columns

start = 0
for end in range(0, len(str), width):
    print(str[start:end] + "<---- line break")
    start = end

print(str[start:])

Some arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost <---- line break arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitra<---- line break ry string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbols

JosuaCarl avatar May 15 '25 07:05 JosuaCarl

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

@JosuaCarl take a look

Jakeroid avatar May 31 '25 16:05 Jakeroid

The link is unfortunately a 404.

JosuaCarl avatar Jun 02 '25 07:06 JosuaCarl

@JosuaCarl Finally we have find time with @gruns to do this: https://github.com/gruns/icecream/releases/tag/v2.1.5

Jakeroid avatar Jun 25 '25 18:06 Jakeroid

Thanks a bunch :) Great work 👍

JosuaCarl avatar Jun 26 '25 08:06 JosuaCarl