TextWrap.jl documentation

This Julia package provides the function wrap which parses an input text and reorganizes its white space so that it can be printed with a fixed screen width, optionally indenting it. It also provides the two convenience functions print_wrapped and println_wrapped.

Here is a quick example:

julia> using TextWrap

julia> text = "This text is going to be wrapped around in lines no longer than 20 characters.";

julia> println_wrapped(text, width=20)
This text is going
to be wrapped around
in lines no longer
than 20 characters.

It's very similar to Python's textwrap module, but the interface is slightly different.

Installation and usage

To install the module, use Julia's package manager: start pkg mode by pressing ] and then enter:

(v1.3) pkg> add TextWrap

Dependencies will be installed automatically. The module can then be loaded like any other Julia module:

julia> using TextWrap

Functions reference

TextWrap.wrapFunction
wrap(string; keywords...)

Parses string and returns a new string in which newlines are inserted as appropriate in order for each line to fit within a specified width.

The behaviour can be controlled via optional keyword arguments:

  • width (deafult=70): the maximum width of the wrapped text, including indentation.
  • initial_indent (default=""): indentation of the first line. This can be any string (shorter than width), or it can be an integer number (smaller than width).
  • subsequent_indent (default=""): indentation of all lines except the first. Works the same as initial_indent.
  • break_on_hyphens (default=true): this flag determines whether words can be broken on hyphens, e.g. whether "high-precision" can be split into "high-" and "precision".
  • break_long_words (default=true): this flag determines what to do when a word is too long to fit in any line. If true, the word will be broken, otherwise it will go beyond the desired text width.
  • replace_whitespace (default=true): if this flag is true, all whitespace characters in the original text (including newlines) will be replaced by spaces. Otherwise, they'll be preserved, except at the beginning or end of a line.
  • expand_tabs (default=true): if this flag is true, tabs will be expanded in-place into spaces. Otherwise a tab is counted as a single character. The expansion happens before whitespace replacement.
  • fix_sentence_endings (default=false): if this flag is true, the wrapper will try to recognize sentence endings in the middle of a paragraph and put two spaces before the next sentence in case only one is present.
  • recognize_escapes (default=true): if true, compute all lengths ignoring ANSI escape codes (special character sequences used e.g. to modify the text color or other properties; they look e.g. like "\e[94m")
source
TextWrap.print_wrappedFunction
print_wrapped([io,] text...; keywords...)

This is just like the standard print function (it prints multiple arguments and accepts an optional IO first argument), except that it wraps the result, and accepts keyword arguments to pass to wrap.

source