Open c3potheds opened 7 months ago
Hi @c3potheds! Thanks for writing, I very much agree with you that this is confusing and could be made better...
In short, the option you're looking for is WordSeparator::AsciiSpace
. See this playground:
assert_eq!(
textwrap::fill(
"I wonder what's for dinner http://example.com/long/url/that/should/not/be/split",
textwrap::Options::new(60)
.break_words(false)
.word_separator(textwrap::WordSeparator::AsciiSpace)
),
"I wonder what's for dinner\nhttp://example.com/long/url/that/should/not/be/split"
);
The problem is that the default UnicodeBreakProperties
variant is too smart in this case: it sees /
as a valid place to break a line. This is great when dealing with text like "ä½ å„½" or "šš", but in this case it's not useful.
A first step to improve this could be to update the documentation to call this out. A second step could perhaps be to detect suppress this break point with a new option ā I'm not 100% sure what to do here.
While #384 is marked as resolved, that issue explicitly mentioned not splitting URLs as a motivation for the request and that use case is still not satisfied.
In this example, even with a
break_words(false)
specified into theOptions
, the URL is still split across lines. If printed to a terminal, the newline character interrupts any system URL recognition and makes it impossible to shift-click to open the link.This test fails with this output:
I would like a way to force textwrap to not insert the newline character in a URL even if it would wrap to the next line. In addition, if the URL could fit fully on the next line, it should not be split up:
It's not clear if this is because
/
is interpreted as a character that is allowed to be split.The
NoHyphenation
WordSplitter
option doesn't change anything:I tried a couple of tricks with
WordSplitter
andWordSeparator
but so far to no avail.