Skip to content

Commit

Permalink
Replace Wrapper struct with Options
Browse files Browse the repository at this point in the history
This is a breaking change which simplifies and streamlines the API in
several ways:

* Instead of having both Wrapper::wrap and a top-level wrap function,
we now only have the wrap function. This should simplify things a
little since there is now just one way to use the API.

* The Wrapper struct has been turned into an Options struct, which
  carries all the configuration settings.

* The API of the wrap and fill functions have changed in a backwards
  compatible fashion. They still take a string and a width:

        textwrap::fill("some string", 10);

* In addition to an usize width, you can now also pass Options as the
  second argument:

        textwrap::fill("some string", Options::new(10).break_words(false));
  • Loading branch information
mgeisler committed Sep 30, 2020
1 parent 9274f25 commit 835d190
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 302 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ hyphenation for [about 70 languages][patterns] via high-quality TeX
hyphenation patterns.

Your program must load the hyphenation pattern and configure
`Wrapper::splitter` to use it:
`Options::splitter` to use it:

```rust
use hyphenation::{Language, Load, Standard};
use textwrap::Wrapper;
use textwrap::Options;

fn main() {
let hyphenator = Standard::from_embedded(Language::EnglishUS).unwrap();
let wrapper = Wrapper::new(18).splitter(Box::new(hyphenator));
let options = Options::new(18).splitter(Box::new(hyphenator));
let text = "textwrap: a small library for wrapping text.";
println!("{}", wrapper.fill(text))
println!("{}", fill_with(text, &options);
}
```

Expand Down
13 changes: 7 additions & 6 deletions benches/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ pub fn benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("String lengths");
for length in [100, 200, 400, 800, 1600].iter() {
let text = lorem_ipsum(*length);
let mut wrapper = textwrap::Wrapper::new(LINE_LENGTH);
let mut options = textwrap::Options::new(LINE_LENGTH);
group.bench_with_input(BenchmarkId::new("fill", length), &text, |b, text| {
b.iter(|| wrapper.fill(text));
b.iter(|| textwrap::fill(text, &options));
});
group.bench_with_input(BenchmarkId::new("wrap", length), &text, |b, text| {
b.iter(|| wrapper.wrap(text));

group.bench_with_input(BenchmarkId::new("fill_usize", length), &text, |b, text| {
b.iter(|| textwrap::fill(text, LINE_LENGTH));
});

#[cfg(feature = "hyphenation")]
Expand All @@ -38,9 +39,9 @@ pub fn benchmark(c: &mut Criterion) {
.join("benches")
.join("la.standard.bincode");
let dictionary = Standard::from_path(Language::Latin, &path).unwrap();
wrapper.splitter = Box::new(dictionary);
options.splitter = Box::new(dictionary);
group.bench_with_input(BenchmarkId::new("hyphenation", length), &text, |b, text| {
b.iter(|| wrapper.fill(text));
b.iter(|| textwrap::fill(text, &options));
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/hyphenation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ fn main() {
fn main() {
let text = "textwrap: a small library for wrapping text.";
let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap();
let wrapper = textwrap::Wrapper::new(18).splitter(Box::new(dictionary));
println!("{}", wrapper.fill(text));
let options = textwrap::Options::new(18).splitter(Box::new(dictionary));
println!("{}", textwrap::fill(text, &options));
}
10 changes: 5 additions & 5 deletions examples/layout.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use textwrap::Wrapper;
use textwrap::{wrap, Options};

fn main() {
let example = "Memory safety without garbage collection. \
Concurrency without data races. \
Zero-cost abstractions.";
let mut prev_lines = vec![];

let mut wrapper = Wrapper::new(0);
let mut options = Options::new(0);
#[cfg(feature = "hyphenation")]
{
use hyphenation::Load;
let language = hyphenation::Language::EnglishUS;
let dictionary = hyphenation::Standard::from_embedded(language).unwrap();
wrapper.splitter = Box::new(dictionary);
options.splitter = Box::new(dictionary);
}

for width in 15..60 {
wrapper.width = width;
let lines = wrapper.wrap(example).collect::<Vec<_>>();
options.width = width;
let lines = wrap(example, &options).collect::<Vec<_>>();
if lines != prev_lines {
let title = format!(" Width: {} ", width);
println!(".{:-^1$}.", title, width + 2);
Expand Down
12 changes: 6 additions & 6 deletions examples/termwidth.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "terminal_size")]
use textwrap::Wrapper;
use textwrap::{fill, Options};

#[cfg(not(feature = "terminal_size"))]
fn main() {
Expand All @@ -13,21 +13,21 @@ fn main() {
Zero-cost abstractions.";

#[cfg(not(feature = "hyphenation"))]
let (msg, wrapper) = ("without hyphenation", Wrapper::with_termwidth());
let (msg, options) = ("without hyphenation", Options::with_termwidth());

#[cfg(feature = "hyphenation")]
use hyphenation::Load;

#[cfg(feature = "hyphenation")]
let (msg, wrapper) = (
let (msg, options) = (
"with hyphenation",
Wrapper::with_termwidth().splitter(Box::new(
Options::with_termwidth().splitter(Box::new(
hyphenation::Standard::from_embedded(hyphenation::Language::EnglishUS).unwrap(),
)),
);

println!("Formatted {} in {} columns:", msg, wrapper.width);
println!("Formatted {} in {} columns:", msg, options.width);
println!("----");
println!("{}", wrapper.fill(example));
println!("{}", fill(example, &options));
println!("----");
}
Loading

0 comments on commit 835d190

Please sign in to comment.