scales icon indicating copy to clipboard operation
scales copied to clipboard

Split long strings without spaces

Open MLopez-Ibanez opened this issue 2 years ago • 1 comments

Currently, label_wrap() only splits a string if there are spaces, however, there is no way (as far as I can tell) to force wrapping even if there is no space to ensure we do not overflow the specified width.

Example:

label_wrap(10)("Incomprehensibility")

should produce "Incomprehe\nnsibility".

MLopez-Ibanez avatar Jun 06 '22 19:06 MLopez-Ibanez

Other interesting variants could be:

label_wrap(10, camel_case=TRUE)("defaultOptionA")

which splits (if needed) at "(?<=[[:lower:]])(?=[[:upper:]])" thus, "default\nOptionA".

and specify allowed split characters:

label_wrap(10, split="[[:space:]_]")("default_option_a")

will produce "default\noption_a".

Note that all the above can work together in a well-defined way:

label_wrap(10, force=TRUE, split="[[:space:]_]", camel_case=TRUE)("defaultOptionA_allowed_wheneverpossible")

would produce "default\nOptionA\nallowed\nwheneverpo\nssible"

MLopez-Ibanez avatar Jun 06 '22 19:06 MLopez-Ibanez

This is outside the scope of scales as word wrapping is it's own horrible beast safe for very simple cases such as what scales provide. ggtext/gridtext offers a much richer word-wrapping featureset

thomasp85 avatar Nov 01 '23 12:11 thomasp85