summaryrefslogtreecommitdiffstats
path: root/third_party/rust/textwrap/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/textwrap/examples
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/textwrap/examples')
-rw-r--r--third_party/rust/textwrap/examples/layout.rs39
-rw-r--r--third_party/rust/textwrap/examples/termwidth.rs21
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!("----");
+}