Repository: mgeisler/textwrap (language: rust)

Issue:
Add support for finding terminal size and use that for splitting
We could ship with a convenience function that will automatically find the terminal width on Unix and Windows and wrap the text accordingly.


Discussion / hints:
Take a look at [term_size](https://crates.io/crates/term_size) if you haven't already :wink: I'd also welcome any contributions to that crate.
@kbknapp Thanks, I was already thinking of using that!

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index a8d76ea4..c8e46fcf 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,4 +16,5 @@ appveyor = { repository = "mgeisler/textwrap" }
 
 [dependencies]
 unicode-width = "0.1.3"
+term_size = "0.3.0"
 hyphenation = { version = "0.6.1", optional = true }
diff --git a/README.md b/README.md
index 76f56ad9..41b52101 100644
--- a/README.md
+++ b/README.md
@@ -83,8 +83,13 @@ The hyphenation uses high-quality TeX hyphenation patterns.
 
 ## Examples
 
-The library comes with a small example program that shows how a fixed
-example string is wrapped at different widths. Run the example with:
+The library comes with a small example programs that shows various
+features.
+
+### Layout Example
+
+The `layout` example shows how a fixed example string is wrapped at
+different widths. Run the example with:
 
 ```shell
 $ cargo run --features hyphenation --example layout
@@ -135,6 +140,36 @@ Later, longer lines are used and the output now looks like this:
 Notice how words are split at hyphens (such a s "zero-cost") but also
 how words are hyphenated using automatic/machine hyphenation.
 
+### Terminal Width Example
+
+The `termwidth` example simply shows how the width can be set
+automatically to the current terminal width. Run it with this command:
+
+```
+$ cargo run --example termwidth
+```
+
+If you run it in a narrow terminal, you'll see output like this:
+```
+Formatted in within 60 columns:
+----
+Memory safety without garbage collection. Concurrency
+without data races. Zero-cost abstractions.
+----
+```
+
+If `stdout` is not connected to the terminal, the program will use a
+default of 80 columns for the width:
+
+```
+$ cargo run --example termwidth | cat
+Formatted in within 80 columns:
+----
+Memory safety without garbage collection. Concurrency without data races. Zero-
+cost abstractions.
+----
+```
+
 ## Release History
 
 This section lists the largest changes per release.
diff --git a/examples/termwidth.rs b/examples/termwidth.rs
new file mode 100644
index 00000000..2d4b5db4
--- /dev/null
+++ b/examples/termwidth.rs
@@ -0,0 +1,18 @@
+extern crate textwrap;
+
+use textwrap::Wrapper;
+
+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 = Wrapper::with_termwidth();
+    println!("Formatted in within {} columns:", wrapper.width);
+    println!("----");
+    println!("{}", wrapper.fill(example));
+    println!("----");
+}
diff --git a/src/lib.rs b/src/lib.rs
index bb3ebe55..10522434 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,6 +22,7 @@
 
 
 extern crate unicode_width;
+extern crate term_size;
 #[cfg(feature = "hyphenation")]
 extern crate hyphenation;
 
@@ -127,6 +128,15 @@ impl Wrapper {
         }
     }
 
+    /// Crate a new Wrapper for wrapping text at the current terminal
+    /// width. If the terminal width cannot be determined (typically
+    /// because the standard input and output is not connected to a
+    /// terminal), a width of 80 characters will be used. Other
+    /// settings use the same defaults as `Wrapper::new`.
+    pub fn with_termwidth() -> Wrapper {
+        Wrapper::new(term_size::dimensions_stdout().map_or(80, |(w, _)| w))
+    }
+
     /// Fill a line of text at `self.width` characters. Strings are
     /// wrapped based on their displayed width, not their size in
     /// bytes.


Test patch: