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

Simplify API with only top-level functions #213

Merged
merged 3 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
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