colored-rs / colored

(Rust) Coloring terminal so simple you already know how to do it !
Mozilla Public License 2.0
1.74k stars 84 forks source link

Implement `Add` on `ColoredString` #140

Open hwittenborn opened 1 year ago

hwittenborn commented 1 year ago

It would be nice to be able to add ColoredString instances together, so that something like "blue".blue() + "red".red() could be done. It would also be nice to add normal strings as well (i.e. "blue".blue() + "|" + "red".red()).

I'm not sure how this would work with #139 when mixing different formatter types though.

hwittenborn commented 1 year ago

This will be added after #139 gets implemented, and the plan is to only allow it to function when T: impl Display. This is due to some limitations in Rust's trait system which wouldn't allow for multiple impl definitions (Rust wouldn't know what to call for Add if T implemented both Display and Debug).

To support multiple colored strings while still being able to get their sources, a Vec of some sort would have to be kept internally to keep track of all the strings, and it looks like the heapless crate would provide for that need just fine for no_std environments.

ToBinio commented 1 year ago

would this help with #115 ?

I am currently running into a similar issue to #115 and I believe this could be fixed by handling the whole text as a ColoredString

btw. thanks @hwittenborn for bringing this project back to life ❤️

ClementNerma commented 11 months ago

I personally think this would cause more problems. There's a good reason String doesn't implement Add: aside from ergonomics, it hides an allocation.