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.wrap
— Functionwrap(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 thanwidth
), or it can be an integer number (smaller thanwidth
).subsequent_indent
(default=""
): indentation of all lines except the first. Works the same asinitial_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. Iftrue
, the word will be broken, otherwise it will go beyond the desired text width.replace_whitespace
(default=true
): if this flag istrue
, 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 istrue
, 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 istrue
, 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
): iftrue
, 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"
)
TextWrap.print_wrapped
— Functionprint_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
.
TextWrap.println_wrapped
— Functionprintln_wrapped([io,] text...; keywords...)
Like print_wrapped
, but adds a newline at the end.