• Home
Name
Date
Size
#Lines
LOC

..--

src/03-May-2024-3,5041,675

tests/03-May-2024-11283

.cargo_vcs_info.jsonD03-May-202474 65

.gitignoreD03-May-202430 64

Android.bpD03-May-2024771 3228

CHANGELOG.mdD03-May-202414.3 KiB394292

Cargo.tomlD03-May-20241.7 KiB6555

LICENSED03-May-20241 KiB2217

METADATAD03-May-2024409 2019

MODULE_LICENSE_MITD03-May-20240

NOTICED03-May-20241 KiB2217

OWNERSD03-May-202440 21

README.mdD03-May-20245.2 KiB155118

TEST_MAPPINGD03-May-2024257 1514

cargo2android.jsonD03-May-2024160 1010

README.md

1 # Textwrap
2 
3 [![](https://github.com/mgeisler/textwrap/workflows/build/badge.svg)][build-status]
4 [![](https://codecov.io/gh/mgeisler/textwrap/branch/master/graph/badge.svg)][codecov]
5 [![](https://img.shields.io/crates/v/textwrap.svg)][crates-io]
6 [![](https://docs.rs/textwrap/badge.svg)][api-docs]
7 
8 Textwrap is a library for wrapping and indenting text. It is most
9 often used by command-line programs to format dynamic output nicely so
10 it looks good in a terminal. However, you can use the library to wrap
11 arbitrary things by implementing the `Fragment` trait — an example
12 would be wrapping text for PDF files.
13 
14 ## Usage
15 
16 To use the textwrap crate, add this to your `Cargo.toml` file:
17 ```toml
18 [dependencies]
19 textwrap = "0.13"
20 ```
21 
22 By default, this enables word wrapping with support for Unicode
23 strings. Extra features can be enabled with Cargo features — and the
24 Unicode support can be disabled if needed. This allows you slim down
25 the library and so you will only pay for the features you actually
26 use. Please see the [_Cargo Features_ in the crate
27 documentation](https://docs.rs/textwrap/#cargo-features) for a full
28 list of the available features.
29 
30 ## Documentation
31 
32 **[API documentation][api-docs]**
33 
34 ## Getting Started
35 
36 Word wrapping is easy using the `fill` function:
37 
38 ```rust
39 fn main() {
40     let text = "textwrap: an efficient and powerful library for wrapping text.";
41     println!("{}", textwrap::fill(text, 28));
42 }
43 ```
44 
45 The output is wrapped within 28 columns:
46 
47 ```
48 textwrap: an efficient
49 and powerful library for
50 wrapping text.
51 ```
52 
53 Sharp-eyed readers will notice that the first line is 22 columns wide.
54 So why is the word “and” put in the second line when there is space
55 for it in the first line?
56 
57 The explanation is that textwrap does not just wrap text one line at a
58 time. Instead, it uses an optimal-fit algorithm which looks ahead and
59 chooses line breaks which minimize the gaps left at ends of lines.
60 
61 Without look-ahead, the first line would be longer and the text would
62 look like this:
63 
64 ```
65 textwrap: an efficient and
66 powerful library for
67 wrapping text.
68 ```
69 
70 The second line is now shorter and the text is more ragged. The kind
71 of wrapping can be configured via `Option::wrap_algorithm`.
72 
73 If you enable the `hyphenation` Cargo feature, you get support for
74 automatic hyphenation for [about 70 languages][patterns] via
75 high-quality TeX hyphenation patterns.
76 
77 Your program must load the hyphenation pattern and configure
78 `Options::splitter` to use it:
79 
80 ```rust
81 use hyphenation::{Language, Load, Standard};
82 use textwrap::Options;
83 
84 fn main() {
85     let hyphenator = Standard::from_embedded(Language::EnglishUS).unwrap();
86     let options = Options::new(28).splitter(hyphenator);
87     let text = "textwrap: an efficient and powerful library for wrapping text.";
88     println!("{}", fill(text, &options);
89 }
90 ```
91 
92 The output now looks like this:
93 ```
94 textwrap: an efficient and
95 powerful library for wrap-
96 ping text.
97 ```
98 
99 The US-English hyphenation patterns are embedded when you enable the
100 `hyphenation` feature. They are licensed under a [permissive
101 license][en-us license] and take up about 88 KB in your binary. If you
102 need hyphenation for other languages, you need to download a
103 [precompiled `.bincode` file][bincode] and load it yourself. Please
104 see the [`hyphenation` documentation] for details.
105 
106 ## Wrapping Strings at Compile Time
107 
108 If your strings are known at compile time, please take a look at the
109 procedural macros from the [`textwrap-macros` crate].
110 
111 ## Examples
112 
113 The library comes with [a
114 collection](https://github.com/mgeisler/textwrap/tree/master/examples)
115 of small example programs that shows various features. You’re invited
116 to clone the repository and try them out for yourself!
117 
118 Of special note is the `interactive` example. This is a demo program
119 which demonstrates most of the available features: you can enter text
120 and adjust the width at which it is wrapped interactively. You can
121 also adjust the `Options` used to see the effect of different
122 `WordSplitter`s and wrap algorithms.
123 
124 Run the demo with
125 
126 ```sh
127 $ cargo run --example interactive
128 ```
129 
130 The demo needs a Linux terminal to function.
131 
132 ## Release History
133 
134 Please see the [CHANGELOG file] for details on the changes made in
135 each release.
136 
137 ## License
138 
139 Textwrap can be distributed according to the [MIT license][mit].
140 Contributions will be accepted under the same license.
141 
142 [crates-io]: https://crates.io/crates/textwrap
143 [build-status]: https://github.com/mgeisler/textwrap/actions?query=workflow%3Abuild+branch%3Amaster
144 [codecov]: https://codecov.io/gh/mgeisler/textwrap
145 [`textwrap-macros` crate]: https://crates.io/crates/textwrap-macros
146 [`hyphenation` example]: https://github.com/mgeisler/textwrap/blob/master/examples/hyphenation.rs
147 [`termwidth` example]: https://github.com/mgeisler/textwrap/blob/master/examples/termwidth.rs
148 [patterns]: https://github.com/tapeinosyne/hyphenation/tree/master/patterns-tex
149 [en-us license]: https://github.com/hyphenation/tex-hyphen/blob/master/hyph-utf8/tex/generic/hyph-utf8/patterns/tex/hyph-en-us.tex
150 [bincode]: https://github.com/tapeinosyne/hyphenation/tree/master/dictionaries
151 [`hyphenation` documentation]: http://docs.rs/hyphenation
152 [api-docs]: https://docs.rs/textwrap/
153 [CHANGELOG file]: https://github.com/mgeisler/textwrap/blob/master/CHANGELOG.md
154 [mit]: LICENSE
155