3 [![](https://img.shields.io/crates/v/textwrap.svg)][crates-io]
4 [![](https://docs.rs/textwrap/badge.svg)][api-docs]
5 [![](https://travis-ci.org/mgeisler/textwrap.svg?branch=master)][travis-ci]
6 [![](https://ci.appveyor.com/api/projects/status/yo6iak55nraupjw3/branch/master?svg=true)][appveyor]
8 Textwrap is a small Rust crate for word wrapping text. You can use it
9 to format strings for display in commandline applications. The crate
10 name and interface is inspired by
11 the [Python textwrap module][py-textwrap].
15 Add this to your `Cargo.toml`:
21 and this to your crate root:
23 extern crate textwrap;
26 If you would like to have automatic hyphenation, specify the
30 textwrap = { version = "0.9", features = ["hyphenation"] }
33 To conveniently wrap text at the current terminal width, enable the
38 textwrap = { version = "0.9", features = ["term_size"] }
43 **[API documentation][api-docs]**
47 Word wrapping single strings is easy using the `fill` function:
49 extern crate textwrap;
53 let text = "textwrap: a small library for wrapping text.";
54 println!("{}", fill(text, 18));
64 With the `hyphenation` feature, you can get automatic hyphenation
65 for [about 70 languages][patterns]. Your program must load and
66 configure the hyphenation patterns to use:
68 extern crate hyphenation;
69 extern crate textwrap;
71 use hyphenation::Language;
72 use textwrap::Wrapper;
75 let corpus = hyphenation::load(Language::English_US).unwrap();
76 let wrapper = Wrapper::with_splitter(18, corpus);
77 let text = "textwrap: a small library for wrapping text.";
78 println!("{}", wrapper.fill(text))
82 The output now looks like this:
89 The hyphenation uses high-quality TeX hyphenation patterns.
93 The library comes with some small example programs that shows various
98 The `layout` example shows how a fixed example string is wrapped at
99 different widths. Run the example with:
102 $ cargo run --features hyphenation --example layout
105 The program will use the following string:
107 > Memory safety without garbage collection. Concurrency without data
108 > races. Zero-cost abstractions.
110 The string is wrapped at all widths between 15 and 60 columns. With
111 narrow columns the output looks like this:
133 Later, longer lines are used and the output now looks like this:
136 .-------------------- Width: 49 --------------------.
137 | Memory safety without garbage collection. Concur- |
138 | rency without data races. Zero-cost abstractions. |
139 .---------------------- Width: 53 ----------------------.
140 | Memory safety without garbage collection. Concurrency |
141 | without data races. Zero-cost abstractions. |
142 .------------------------- Width: 59 -------------------------.
143 | Memory safety without garbage collection. Concurrency with- |
144 | out data races. Zero-cost abstractions. |
147 Notice how words are split at hyphens (such as "zero-cost") but also
148 how words are hyphenated using automatic/machine hyphenation.
150 ### Terminal Width Example
152 The `termwidth` example simply shows how the width can be set
153 automatically to the current terminal width. Run it with this command:
156 $ cargo run --example termwidth
159 If you run it in a narrow terminal, you'll see output like this:
161 Formatted in within 60 columns:
163 Memory safety without garbage collection. Concurrency
164 without data races. Zero-cost abstractions.
168 If `stdout` is not connected to the terminal, the program will use a
169 default of 80 columns for the width:
172 $ cargo run --example termwidth | cat
173 Formatted in within 80 columns:
175 Memory safety without garbage collection. Concurrency without data races. Zero-
182 This section lists the largest changes per release.
184 ### Version 0.9.0 — October 5th, 2017
186 The dependency on `term_size` is now optional, and by default this
187 feature is not enabled. This is a *breaking change* for users of
188 `Wrapper::with_termwidth`. Enable the `term_size` feature to restore
189 the old functionality.
191 Added a regression test for case where width is set to usize::MAX.
192 Thanks @Fraser999! All public structs now implement `Debug`.
196 * Fixed [#101][issue-101]: Remove `term_size` as a (hard required)
199 ### Version 0.8.0 — September 4th, 2017
201 The `Wrapper` stuct is now generic over the type of word splitter
202 being used. This means less boxing and a nicer API. The
203 `Wrapper::word_splitter` method has been removed. This is a *breaking
204 API change* if you used the method to change the word splitter.
206 The `Wrapper` struct has two new methods that will wrap the input text
207 lazily: `Wrapper::wrap_iter` and `Wrapper::into_wrap_iter`. Use those
208 if you will be iterating over the wrapped lines one by one.
212 * Fixed [#59][issue-59]: `wrap` could return an iterator. Thanks
215 * Fixed [#81][issue-81]: Set `html_root_url`
217 ### Version 0.7.0 — July 20th, 2017
219 Version 0.7.0 changes the return type of `Wrapper::wrap` from
220 `Vec<String>` to `Vec<Cow<'a, str>>`. This means that the output lines
221 borrow data from the input string. This is a *breaking API change* if
222 you relied on the exact return type of `Wrapper::wrap`. Callers of the
223 `textwrap::fill` convenience function will see no breakage.
225 The above change and other optimizations makes version 0.7.0 roughly
226 15-30% faster than version 0.6.0.
228 The `squeeze_whitespace` option has been removed since it was
229 complicating the above optimization. Let us know if this option is
230 important for you so we can provide a work around.
234 * Fixed [#58][issue-58]: Add a "fast_wrap" function that reuses the
237 * Fixed [#61][issue-61]: Documentation errors
239 ### Version 0.6.0 — May 22nd, 2017
241 Version 0.6.0 adds builder methods to `Wrapper` for easy one-line
242 initialization and configuration:
245 let wrapper = Wrapper::new(60).break_words(false);
248 It also add a new `NoHyphenation` word splitter that will never split
249 words, not even at existing hyphens.
253 * Fixed [#28][issue-28]: Support not squeezing whitespace
255 ### Version 0.5.0 — May 15th, 2017
257 Version 0.5.0 has *breaking API changes*. However, this only affects
258 code using the hyphenation feature. The feature is now optional, so
259 you will first need to enable the `hyphenation` feature as described
260 above. Afterwards, please change your code from
262 wrapper.corpus = Some(&corpus);
266 wrapper.splitter = Box::new(corpus);
269 Other changes include optimizations, so version 0.5.0 is roughly
270 10-15% faster than version 0.4.0.
274 * Fixed [#19][issue-19]: Add support for finding terminal size
275 * Fixed [#25][issue-25]: Handle words longer than `self.width`
276 * Fixed [#26][issue-26]: Support custom indentation
277 * Fixed [#36][issue-36]: Support building without `hyphenation`
278 * Fixed [#39][issue-39]: Respect non-breaking spaces
280 ### Version 0.4.0 — January 24th, 2017
282 Documented complexities and tested these via `cargo bench`.
284 * Fixed [#13][issue-13]: Immediatedly add word if it fits
285 * Fixed [#14][issue-14]: Avoid splitting on initial hyphens in `--foo-bar`
287 ### Version 0.3.0 — January 7th, 2017
289 Added support for automatic hyphenation.
291 ### Version 0.2.0 — December 28th, 2016
293 Introduced `Wrapper` struct. Added support for wrapping on hyphens.
295 ### Version 0.1.0 — December 17th, 2016
297 First public release with support for wrapping strings on whitespace.
301 Textwrap can be distributed according to the [MIT license][mit].
302 Contributions will be accepted under the same license.
304 [crates-io]: https://crates.io/crates/textwrap
305 [travis-ci]: https://travis-ci.org/mgeisler/textwrap
306 [appveyor]: https://ci.appveyor.com/project/mgeisler/textwrap
307 [py-textwrap]: https://docs.python.org/library/textwrap
308 [patterns]: https://github.com/tapeinosyne/hyphenation/tree/master/patterns-tex
309 [api-docs]: https://docs.rs/textwrap/
310 [issue-13]: https://github.com/mgeisler/textwrap/issues/13
311 [issue-14]: https://github.com/mgeisler/textwrap/issues/14
312 [issue-19]: https://github.com/mgeisler/textwrap/issues/19
313 [issue-25]: https://github.com/mgeisler/textwrap/issues/25
314 [issue-26]: https://github.com/mgeisler/textwrap/issues/26
315 [issue-28]: https://github.com/mgeisler/textwrap/issues/28
316 [issue-36]: https://github.com/mgeisler/textwrap/issues/36
317 [issue-39]: https://github.com/mgeisler/textwrap/issues/39
318 [issue-58]: https://github.com/mgeisler/textwrap/issues/58
319 [issue-59]: https://github.com/mgeisler/textwrap/issues/59
320 [issue-61]: https://github.com/mgeisler/textwrap/issues/61
321 [issue-81]: https://github.com/mgeisler/textwrap/issues/81
322 [issue-101]: https://github.com/mgeisler/textwrap/issues/101