diff options
Diffstat (limited to 'third_party/rust/textwrap/examples')
-rw-r--r-- | third_party/rust/textwrap/examples/layout.rs | 39 | ||||
-rw-r--r-- | third_party/rust/textwrap/examples/termwidth.rs | 21 |
2 files changed, 60 insertions, 0 deletions
diff --git a/third_party/rust/textwrap/examples/layout.rs b/third_party/rust/textwrap/examples/layout.rs new file mode 100644 index 0000000000..6d3d10242d --- /dev/null +++ b/third_party/rust/textwrap/examples/layout.rs @@ -0,0 +1,39 @@ +#[cfg(feature = "hyphenation")] +extern crate hyphenation; +extern crate textwrap; + +#[cfg(feature = "hyphenation")] +use hyphenation::Language; +use textwrap::Wrapper; + + +#[cfg(not(feature = "hyphenation"))] +fn new_wrapper<'a>() -> Wrapper<'a, textwrap::HyphenSplitter> { + Wrapper::new(0) +} + +#[cfg(feature = "hyphenation")] +fn new_wrapper<'a>() -> Wrapper<'a, hyphenation::Corpus> { + let corpus = hyphenation::load(Language::English_US).unwrap(); + Wrapper::with_splitter(0, corpus) +} + +fn main() { + let example = "Memory safety without garbage collection. \ + Concurrency without data races. \ + Zero-cost abstractions."; + let mut prev_lines = vec![]; + let mut wrapper = new_wrapper(); + for width in 15..60 { + wrapper.width = width; + let lines = wrapper.wrap(example); + if lines != prev_lines { + let title = format!(" Width: {} ", width); + println!(".{:-^1$}.", title, width + 2); + for line in &lines { + println!("| {:1$} |", line, width); + } + prev_lines = lines; + } + } +} diff --git a/third_party/rust/textwrap/examples/termwidth.rs b/third_party/rust/textwrap/examples/termwidth.rs new file mode 100644 index 0000000000..7d81a5c654 --- /dev/null +++ b/third_party/rust/textwrap/examples/termwidth.rs @@ -0,0 +1,21 @@ +#[cfg(feature = "term_size")] +extern crate textwrap; + +#[cfg(not(feature = "term_size"))] +fn main() { + println!("Please enable the term_size feature to run this example."); +} + +#[cfg(feature = "term_size")] +fn main() { + 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 wrapper = textwrap::Wrapper::with_termwidth(); + println!("Formatted in within {} columns:", wrapper.width); + println!("----"); + println!("{}", wrapper.fill(example)); + println!("----"); +} |