Textwrap.wrap

You're seeing just the function wrap, go back to Textwrap module for more information.
Link to this function

wrap(text, width_or_opts)

View Source

Specs

wrap(text :: String.t(), opts :: wrap_opts()) :: [String.t()]

Wraps text to the given width.

wrap/2 returns a list of Strings, each of no more than width charecters.

Options can be either passed as a keyword list (which must include the key :width), or, if using no options other than :width, the width can be passed on its own as the second argument.

width can either by a positive integer or the atom :termwidth. See the module docs on :termwidth for more details.

Options

  • :width — the width to wrap at, a positive integer.
  • :break_words — allow long words to be broken, if they won't fit on a single line. Setting this to false may cause some lines to be longer than :width.
  • :inital_indent — will be added as a prefix to the first line of the result.
  • :subsequent_indent — will be added as a prefix to each line other than the first line of the result.
  • :splitter — when set to false, hyphens within words won't be treated specially as a place to split words. When set to :en_us, a language-aware hyphenation system will be used to try to break words in appropriate places.
  • :wrap_algorithm — by default, or when set to :optimal_fit, wrap/2 will do its best to balance the gaps left at the ends of lines. When set to :first_fit, a simpler greedy algorithm is used instead. See the docs in the textwrap crate for more details.

Examples

iex> Textwrap.wrap("hello world", 5)
["hello", "world"]

iex> Textwrap.wrap("hello world", width: 5)
["hello", "world"]

iex> Textwrap.wrap("Antidisestablishmentarianism", width: 10)
["Antidisest", "ablishment", "arianism"]

iex> Textwrap.wrap("Antidisestablishmentarianism", width: 10, break_words: false)
["Antidisestablishmentarianism"]

iex> Textwrap.wrap("Antidisestablishmentarianism", width: 10, splitter: :en_us)
["Antidis-", "establish-", "mentarian-", "ism"]

iex> Textwrap.wrap("foo bar baz",
...>      width: 5,
...>      initial_indent: "> ",
...>      subsequent_indent: "  ")
["> foo", "  bar", "  baz"]

iex> Textwrap.wrap("Lorem ipsum dolor sit amet, consectetur adipisicing elit",
...>      width: 25,
...>      wrap_algorithm: :optimal_fit)
["Lorem ipsum dolor", "sit amet, consectetur", "adipisicing elit"]

iex> Textwrap.wrap("Lorem ipsum dolor sit amet, consectetur adipisicing elit",
...>      width: 25,
...>      wrap_algorithm: :first_fit)
["Lorem ipsum dolor sit", "amet, consectetur", "adipisicing elit"]