Struct textwrap::Wrapper
[−]
[src]
pub struct Wrapper<'a> { pub width: usize, pub initial_indent: &'a str, pub subsequent_indent: &'a str, pub break_words: bool, pub squeeze_whitespace: bool, pub splitter: Box<WordSplitter>, }
A Wrapper holds settings for wrapping and filling text. Use it
when the convenience wrap
and fill
functions are not
flexible enough.
The algorithm used by the wrap
method works by doing a single
scan over words in the input string and splitting them into one or
more lines. The time and memory complexity is O(n) where n is
the length of the input string.
Fields
width: usize
The width in columns at which the text will be wrapped.
initial_indent: &'a str
Indentation used for the first line of output.
subsequent_indent: &'a str
Indentation used for subsequent lines of output.
break_words: bool
Allow long words to be broken if they cannot fit on a line.
When set to false
, some lines be being longer than
self.width
.
squeeze_whitespace: bool
This crate treats every whitespace character (space, newline,
TAB, ...) as a space character. When this option is set to
true
, the whitespace between words will be squeezed into a
single space character. Otherwise, the whitespace is
significant and will be kept in the output. Whitespace
characters such as TAB will still be turned into a single
space -- consider expanding TABs manually if this is a
concern.
splitter: Box<WordSplitter>
The method for splitting words. If the hyphenation
feature
is enabled, you can use a hyphenation::language::Corpus
here
to get language-aware hyphenation.
Methods
impl<'a> Wrapper<'a>
[src]
fn new(width: usize) -> Wrapper<'a>
Create a new Wrapper for wrapping at the specified width. By
default, we allow words longer than width
to be broken. A
HyphenSplitter
will be used by default for splitting
words. See the WordSplitter
trait for other options.
fn with_termwidth() -> Wrapper<'a>
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
.
fn initial_indent(self, indent: &'a str) -> Wrapper<'a>
Change self.initial_indent
. The initial indentation is
used on the very first line of output. Setting it to something
like "* "
can be useful if you are formatting an item in a
bulleted list. You will then probably want to set
self.subsequent_indent
to " "
.
fn subsequent_indent(self, indent: &'a str) -> Wrapper<'a>
Change self.subsequent_indent
. The subsequent indentation
is used on lines following the first line of output. Setting
it to something like " "
can be useful if you are
formatting an item in a bulleted list.
fn break_words(self, setting: bool) -> Wrapper<'a>
Change self.break_words
. This controls if words longer
than self.width
can be broken, or if they will be left
sticking out into the right margin.
fn squeeze_whitespace(self, setting: bool) -> Wrapper<'a>
Change self.squeeze_whitespace
. This controls if
whitespace betweee words is squeezed together to a single
space. Regardless of this setting, all whitespace characters
are converted to space (' '
) characters.
fn word_splitter(self, splitter: Box<WordSplitter>) -> Wrapper<'a>
Change self.splitter
. The word splitter is consulted when
a word is too wide to fit the current line. By changing this,
you can decide if such words should be hyphenated or left
alone. Hyphenation can be done using existing hyphens (see
[HyphenSplitter
]) or it can be based on TeX hyphenation
patterns, if the hyphenation
feature is enabled.
fn fill(&self, s: &str) -> String
Fill a line of text at self.width
characters. Strings are
wrapped based on their displayed width, not their size in
bytes.
The result is a string with newlines between each line. Use
the wrap
method if you need access to the individual lines.
use textwrap::Wrapper; let wrapper = Wrapper::new(15); assert_eq!(wrapper.fill("Memory safety without garbage collection."), "Memory safety\nwithout garbage\ncollection.");
This method simply joins the lines produced by wrap
. As
such, it inherits the O(n) time and memory complexity where
n is the input string length.
fn wrap(&self, s: &str) -> Vec<String>
Wrap a line of text at self.width
characters. Strings are
wrapped based on their displayed width, not their size in
bytes.
use textwrap::Wrapper; let wrap15 = Wrapper::new(15); assert_eq!(wrap15.wrap("Concurrency without data races."), vec!["Concurrency", "without data", "races."]); let wrap20 = Wrapper::new(20); assert_eq!(wrap20.wrap("Concurrency without data races."), vec!["Concurrency without", "data races."]);
The WordSplitter
stored in self.splitter
is used
whenever when a word is too large to fit on the current line.
By changing the field, different hyphenation strategies can be
implemented.
This method does a single scan over the input string, it has an O(n) time and memory complexity where n is the input string length.