aboutsummaryrefslogtreecommitdiff
path: root/textwrap/examples/termwidth.rs
blob: 75db3aa7e4060bbe6e6d022c5f5b9dde8fa8541d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#[cfg(feature = "hyphenation")]
extern crate hyphenation;
extern crate textwrap;

#[cfg(feature = "hyphenation")]
use hyphenation::{Language, Load, Standard};
#[cfg(feature = "term_size")]
use textwrap::Wrapper;

#[cfg(not(feature = "term_size"))]
fn main() {
    println!("Please enable the term_size feature to run this example.");
}

#[cfg(feature = "term_size")]
fn main() {
    #[cfg(not(feature = "hyphenation"))]
    fn new_wrapper<'a>() -> (&'static str, Wrapper<'a, textwrap::HyphenSplitter>) {
        ("without hyphenation", Wrapper::with_termwidth())
    }

    #[cfg(feature = "hyphenation")]
    fn new_wrapper<'a>() -> (&'static str, Wrapper<'a, Standard>) {
        let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap();
        (
            "with hyphenation",
            Wrapper::with_splitter(textwrap::termwidth(), dictionary),
        )
    }

    let example = "Memory safety without garbage collection. \
                   Concurrency without data races. \
                   Zero-cost abstractions.";
    // Create a new Wrapper -- automatically set the width to the
    // current terminal width.
    let (msg, wrapper) = new_wrapper();
    println!("Formatted {} in {} columns:", msg, wrapper.width);
    println!("----");
    println!("{}", wrapper.fill(example));
    println!("----");
}