timofurrer / colorful

Terminal string styling done right, in Python :snake: :tada:
MIT License
525 stars 23 forks source link

Styling non-strings #40

Open keturn opened 4 years ago

keturn commented 4 years ago

Here are a few test cases demonstrating failures when colorful makes ColorfulStrings out of non-string objects.

s.red(1) + s.red(2) case is particularly fun for generating quirky results that sneak by without raising exceptions.

keturn commented 4 years ago

Not sure how you'd like to handle this.

The thing that seems most obvious to me is for ColorfulStyle.evaluate to str() everything that comes in, so that ColorfulString.orig_string is always a string.

Strictly speaking, that won't be 100% backwards compatible, because you'll be executing the input's __str__ earlier than you would have, and things like ColorfulString.__format__ will no longer call the original object's __format__.

but since style_string freezes its input as a string at that time anyway, it's probably the only thing that makes sense, to do them both at the same time.

keturn commented 4 years ago

And while I'm looking at ColorfulString, I notice __len__ uses orig_string but __iter__ uses styled_string.

Is that intentional? Should __iter__ at least switch on colormode == NO_COLORS?