This file lists the most important changes made in each release of textwrap
.
This release fixes display_width
to ignore inline-hyperlinks. The minimum supported version of Rust is now documented to be 1.56.
display_width
: calculations.Options::width
setter method.WordSeparator
is an enum rather than a trait.This release marks Options
as non_exhaustive
and extends it to make line endings configurable, it adds new fast paths to fill
and wrap
, and it fixes crashes in unfill
and refill
.
Options
as non_exhaustive
. This will allow us to extend the struct in the future without breaking backwards compatibility.fill
and wrap
. This makes the functions 10-25 times faster when the no wrapping is needed.refill
to add back correct line ending.unfill
and refill
.This release is identical to 0.15.0 and is only there to give people a way to install crates which depend on the yanked 0.15.1 release. See #484 for details.
This release was yanked since it accidentally broke backwards compatibility with 0.15.0.
This is a major feature release with two main changes:
#421: Use f64
instead of usize
for fragment widths.
This fixes problems with overflows in the internal computations of wrap_optimal_fit
when fragments (words) or line lengths had extreme values, such as usize::MAX
.
#438: Simplify Options
by removing generic type parameters.
This change removes the new generic parameters introduced in version 0.14, as well as the original WrapSplitter
parameter which has been present since very early versions.
The result is a simplification of function and struct signatures across the board. So what used to be
let options: Options< wrap_algorithms::FirstFit, word_separators::AsciiSpace, word_splitters::HyphenSplitter, > = Options::new(80);
if types are fully written out, is now simply
let options: Options<'_> = Options::new(80);
The anonymous lifetime represent the lifetime of the initial_indent
and subsequent_indent
strings. The change is nearly performance neutral (a 1-2% regression).
Smaller improvements and changes:
Options
docstring.OptimalFit
in interactive example.wrap_optimal_fit
penalties to non-negative numbers.debug-words
example.The 0.14.1 release included more changes than intended and has been yanked. The change intended for 0.14.1 is now included in 0.14.2.
This release fixes a panic reported by @Makoto, thanks!
find_words
due to string access outside of a character boundary.This is a major feature release which makes Textwrap more configurable and flexible. The high-level API of textwrap::wrap
and textwrap::fill
remains unchanged, but low-level structs have moved around.
The biggest change is the introduction of new generic type parameters to the Options
struct. These parameters lets you statically configure the wrapping algorithm, the word separator, and the word splitter. If you previously spelled out the full type for Options
, you now need to take the extra type parameters into account. This means that
let options: Options<HyphenSplitter> = Options::new(80);
changes to
let options: Options< wrap_algorithms::FirstFit, word_separators::AsciiSpace, word_splitters::HyphenSplitter, > = Options::new(80);
This is quite a mouthful, so we suggest using type inference where possible. You won’t see any chance if you call wrap
directly with a width or with an Options
value constructed on the fly. Please open an issue if this causes problems for you!
WordSeparator
Trait#332: Add WordSeparator
trait to allow customizing how words are found in a line of text. Until now, Textwrap would always assume that words are separated by ASCII space characters. You can now customize this as needed.
#313: Add support for using the Unicode line breaking algorithm to find words. This is done by adding a second implementation of the new WordSeparator
trait. The implementation uses the unicode-linebreak crate, which is a new optional dependency.
With this, Textwrap can be used with East-Asian languages such as Chinese or Japanese where there are no spaces between words. Breaking a long sequence of emojis is another example where line breaks might be wanted even if there are no whitespace to be found. Feedback would be appreciated for this feature.
#353: Trim trailing whitespace from prefix
in indent
.
Before, empty lines would get no prefix added. Now, empty lines have a trimmed prefix added. This little trick makes indent
much more useful since you can now safely indent with "# "
without creating trailing whitespace in the output due to the trailing whitespace in your prefix.
#354: Make indent
about 20% faster by preallocating the output string.
#331: Remove outer boxing from Options
.
#357: Replace core::WrapAlgorithm
enum with a wrap_algorithms::WrapAlgorithm
trait. This allows for arbitrary wrapping algorithms to be plugged into the library.
#358: Switch wrapping functions to use a slice for line_widths
.
#368: Move WordSeparator
and WordSplitter
traits to separate modules. Before, Textwrap had several top-level structs such as NoHyphenation
and HyphenSplitter
. These implementations of WordSplitter
now lives in a dedicated word_splitters
module. Similarly, we have a new word_separators
module for implementations of WordSeparator
.
#369: Rename Options::splitter
to Options::word_splitter
for consistency with the other fields backed by traits.
This release removes println!
statements which was left behind in unfill
by mistake.
unfill
function.This release contains a bugfix for indent
and improved handling of emojis. We’ve also added a new function for formatting text in columns and functions for reformatting already wrapped text.
core::display_width
to handle emojis when the unicode-width Cargo feature is disabled.indent
preserve existing newlines in the input string. Before, indent("foo", "")
would return "foo\n"
by mistake. It now returns "foo"
instead.Options
fields have examples.wrap_columns
function.unfill
and refill
functions.This release primarily makes all dependencies optional. This makes it possible to slim down textwrap as needed.
impl WordSplitter
for Box<T> where T: WordSplitter
.wrap_optimal_fit
and wrap_first_fit
.This is a bugfix release which fixes a regression in 0.13.0. The bug meant that colored text was wrapped incorrectly.
This is a major release which rewrites the core logic, adds many new features, and fixes a couple of bugs. Most programs which use textwrap
stays the same, incompatibilities and upgrade notes are given below.
Clone the repository and run the following to explore the new features in an interactive demo (Linux only):
$ cargo run --example interactive --all-features
The core wrapping algorithm has been completely rewritten. This fixed bugs and simplified the code, while also making it possible to use textwrap
outside the context of the terminal.
As part of this, trailing whitespace is now discarded consistently from wrapped lines. Before we would inconsistently remove whitespace at the end of wrapped lines, except for the last. Leading whitespace is still preserved.
This release adds support for new wrapping algorithm which finds a globally optimal set of line breaks, taking certain penalties into account. As an example, the old algorithm would produce
"To be, or" "not to be:" "that is" "the" "question"
Notice how the fourth line with “the” is very short. The new algorithm shortens the previous lines slightly to produce fewer short lines:
"To be," "or not to" "be: that" "is the" "question"
Use the new textwrap::core::WrapAlgorithm
enum to select between the new and old algorithm. By default, the new algorithm is used.
The optimal-fit algorithm is inspired by the line breaking algorithm used in TeX, described in the 1981 article Breaking Paragraphs into Lines by Knuth and Plass.
fill_inplace
function.When the text you want to fill is already a temporary String
, you can now mutate it in-place with fill_inplace
:
let mut greeting = format!("Greetings {}, welcome to the game! You have {} lives left.", player.name, player.lives); fill_inplace(&mut greeting, line_width);
This is faster than calling fill
and it will reuse the memory already allocated for the string.
Wrapper
is replaced with Options
Options
(previously known as Wrapper
).fill
& wrap
.WrapOptions
with Into<Options>
.The Wrapper
struct held the options (line width, indentation, etc) for wrapping text. It was also the entry point for actually wrapping the text via its methods such as wrap
, wrap_iter
, into_wrap_iter
, and fill
methods.
The struct has been replaced by a simpler Options
struct which only holds options. The Wrapper
methods are gone, their job has been taken over by the top-level wrap
and fill
functions. The signature of these functions have changed from
fn fill(s: &str, width: usize) -> String; fn wrap(s: &str, width: usize) -> Vec<Cow<'_, str>>;
to the more general
fn fill<'a, S, Opt>(text: &str, options: Opt) -> String where S: WordSplitter, Opt: Into<Options<'a, S>>; fn wrap<'a, S, Opt>(text: &str, options: Opt) -> Vec<Cow<'_, str>> where S: WordSplitter, Opt: Into<Options<'a, S>>;
The Into<Options<'a, S>
bound allows you to pass an usize
(which is interpreted as the line width) and a full Options
object. This allows the new functions to work like the old, plus you can now fully customize the behavior of the wrapping via Options
when needed.
Code that call textwrap::wrap
or textwrap::fill
can remain unchanged. Code that calls into Wrapper::wrap
or Wrapper::fill
will need to be update. This is a mechanical change, please see #213 for examples.
Thanks to @CryptJar and @Koxiat for their support in the PRs above!
The wrap_iter
and into_wrap_iter
methods are gone. This means that lazy iteration is no longer supported: you always get all wrapped lines back as a Vec
. This was done to simplify the code and to support the optimal-fit algorithm.
The first-fit algorithm could still be implemented in an incremental fashion. Please let us know if this is important to you.
Wrapper.splitter
from T: WordSplitter
to Box<dyn WordSplitter>
.This is a bugfix release.
textwrap-macros
crate.break_words(false)
was broken and would cause extra whitespace to be inserted when words were longer than the line width.The code has been updated to the Rust 2018 edition and each new release of textwrap
will only support the latest stable version of Rust. Trying to support older Rust versions is a fool's errand: our dependencies keep releasing new patch versions that require newer and newer versions of Rust.
The term_size
feature has been replaced by terminal_size
. The API is unchanged, it is just the name of the Cargo feature that changed.
The hyphenation
feature now only embeds the hyphenation patterns for US-English. This slims down the dependency.
Due to our dependencies bumping their minimum supported version of Rust, the minimum version of Rust we test against is now 1.22.0.
dedent
handling of empty lines and trailing newlines. Thanks @bbqsrc!Due to our dependencies bumping their minimum supported version of Rust, the minimum version of Rust we test against is now 1.17.0.
The dependency on term_size
is now optional, and by default this feature is not enabled. This is a breaking change for users of Wrapper::with_termwidth
. Enable the term_size
feature to restore the old functionality.
Added a regression test for the case where width
is set to usize::MAX
, thanks @Fraser999! All public structs now implement Debug
, thanks @hcpl!
term_size
an optional dependency.The Wrapper
struct is now generic over the type of word splitter being used. This means less boxing and a nicer API. The Wrapper::word_splitter
method has been removed. This is a breaking API change if you used the method to change the word splitter.
The Wrapper
struct has two new methods that will wrap the input text lazily: Wrapper::wrap_iter
and Wrapper::into_wrap_iter
. Use those if you will be iterating over the wrapped lines one by one.
Version 0.7.0 changes the return type of Wrapper::wrap
from Vec<String>
to Vec<Cow<'a, str>>
. This means that the output lines borrow data from the input string. This is a breaking API change if you relied on the exact return type of Wrapper::wrap
. Callers of the textwrap::fill
convenience function will see no breakage.
The above change and other optimizations makes version 0.7.0 roughly 15-30% faster than version 0.6.0.
The squeeze_whitespace
option has been removed since it was complicating the above optimization. Let us know if this option is important for you so we can provide a work around.
Version 0.6.0 adds builder methods to Wrapper
for easy one-line initialization and configuration:
let wrapper = Wrapper::new(60).break_words(false);
It also add a new NoHyphenation
word splitter that will never split words, not even at existing hyphens.
Version 0.5.0 has breaking API changes. However, this only affects code using the hyphenation feature. The feature is now optional, so you will first need to enable the hyphenation
feature as described above. Afterwards, please change your code from
wrapper.corpus = Some(&corpus);
to
wrapper.splitter = Box::new(corpus);
Other changes include optimizations, so version 0.5.0 is roughly 10-15% faster than version 0.4.0.
self.width
.hyphenation
.Documented complexities and tested these via cargo bench
.
Added support for automatic hyphenation.
Introduced Wrapper
struct. Added support for wrapping on hyphens.
First public release with support for wrapping strings on whitespace.