aboutsummaryrefslogtreecommitdiff
path: root/examples/termwidth.rs
blob: bd4070b3cad7da95530dfced4c95a691c7a269e4 (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
#[cfg(feature = "hyphenation")]
use hyphenation::{Language, Load, Standard};
#[cfg(feature = "terminal_size")]
use textwrap::Wrapper;

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

#[cfg(feature = "terminal_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!("----");
}