Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support not squeezing whitespace #28

Closed
mgeisler opened this issue Jan 29, 2017 · 2 comments
Closed

Support not squeezing whitespace #28

mgeisler opened this issue Jan 29, 2017 · 2 comments

Comments

@mgeisler
Copy link
Owner

We currently split the input string by whitespace and process the words one by one. This implies that we squeeze runs of whitespace to a single space.

We could instead have a mode where whitespace is significant and kept in the output. This would make it possible to wrap text like this line by line:

foobar:    Have lots of foos and bars in the output.
baz:       Show a single bar in the output, but only if it is a full moon.

Lines that wrap would still line up somewhat:

foobar:    Have lots of foos and bars in
the output.
baz:       Show a single bar in the output,
but only if it is a full moon.

In Python's textwrap this option is called replace_whitespace, but I think I'll call it squeeze_whitespace instead. The option should come in handy to support the output wanted in clap-rs/clap#617.

@najamelan
Copy link

Hi,
I needed to wrap text in rust and thus tried this crate. I quickly ran into limitations, like squeezing the whitespace, breaking on non-break spaces, before punctuation etc... so I set off trying to remedy that, which was a bit more hassle than I anticipated, but currently I have something that does the job and that has quite some margin for improvement, extension, so I thought it might interest you. The code is at: https://github.com/najamelan/unicode-wrap and it is very much draft. I'm a total beginner to rust, and not a unicode expert either.

I kind of stole your example and benchmarks. I hope that's ok, if not let me know. I can also add attribution if you would like. Haven't done that yet. Having the same benchmarks allows comparing performance and thus, unicode-wrap is 10 times slower than textwrap on my system.

ps: currently there is no documentation...

greets,
Naja

@mgeisler
Copy link
Owner Author

mgeisler commented Apr 7, 2017

Hey @najamelan -- feel free to use the code if you can, just make sure to keep the license and the copyright notices intact. For example, you should probably not put it under public domain since it is MIT licensed right now.

I took a brief look at your crate and it seems you have built an algorithm that is much more fancy than what I did :-) So I can imagine it will be slower, but with the filters you can get overall better results.

The points about respecting non-breaking spaces is great -- I should double check how they're handled by textwrap and fix any problems.

mgeisler added a commit that referenced this issue May 18, 2017
With this option, whitespace between words can be significant. That
is, whitespace added for alignment purposes can be kept in the output.
This means that wrapping strings like

  -v:        be very verbose and output lots of messages
  --foo:     inject extra foos in the output
  --foobar:  enable the foobar optimization (if possible)

can end up looking like

  -v:        be very verbose and
  output lots of messages
  --foo:     inject extra foos
  in the output
  --foobar:  enable the foobar
  optimization (if possible)

Fixes #28. Related to clap-rs/clap#617.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants