Qore TextWrap Module Reference  1.0
TextWrap Namespace Reference

Main namespace for all public symbols in the TextWrap module. More...

Classes

class  TextWrapper
 

Functions

string dedent (string text)
 Remove any common leading whitespace from every line in 'text'. More...
 
string expandtabs (string text, int tabsize=8)
 Return a copy of the string where all tab characters are expanded using spaces. More...
 
string fill (string text, int width=70, *hash opts)
 Fill a single paragraph of text, returning a new string. More...
 
string indent (string text, string prefix, *code predicate)
 Adds 'prefix' to the beginning of selected lines in 'text'. More...
 
string shorten (string text, int width, *hash opts)
 Collapse and truncate the given text to fit in the given width. More...
 
list wrap (string text, int width=70, *hash opts)
 Wrap a single paragraph of text, returning a list of wrapped lines. More...
 

Detailed Description

Main namespace for all public symbols in the TextWrap module.

Function Documentation

◆ dedent()

string TextWrap::dedent ( string  text)

Remove any common leading whitespace from every line in 'text'.

This can be used to make multi-line strings line up with the left edge of the display, while still presenting them in the source code in indented form.

Note that tabs and spaces are both treated as whitespace, but they are not equal – the lines " hello" and "\thello" are considered to have no common leading whitespace.

◆ expandtabs()

string TextWrap::expandtabs ( string  text,
int  tabsize = 8 
)

Return a copy of the string where all tab characters are expanded using spaces.

Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every tabsize characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab (\t), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline (\n) or return (\r), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed.

Parameters
texta string to process
tabsizenumber of spaces to use in place of one tabulator; default: 8
Returns
a new string with all tabulators changed to spaces
Example:
>>> expandtabs("01\t012\t0123\t01234")
"01 012 0123 01234"
>>> expandtabs("01\t012\t0123\t01234", 4)
"01 012 0123 01234"

◆ fill()

string TextWrap::fill ( string  text,
int  width = 70,
*hash  opts 
)

Fill a single paragraph of text, returning a new string.

Reformat the single paragraph in 'text' to fit in lines of no more than 'width' columns, and return a new string containing the entire wrapped paragraph. As with wrap(), tabs are expanded and other whitespace characters converted to space. See TextWrapper class for available keyword args to customize wrapping behaviour.

◆ indent()

string TextWrap::indent ( string  text,
string  prefix,
*code  predicate 
)

Adds 'prefix' to the beginning of selected lines in 'text'.

If 'predicate' is provided, 'prefix' will only be added to the lines where predicate(line) is True. If 'predicate' is not provided, it will default to adding 'prefix' to all non-empty lines that do not consist solely of whitespace characters.

◆ shorten()

string TextWrap::shorten ( string  text,
int  width,
*hash  opts 
)

Collapse and truncate the given text to fit in the given width.

The text first has its whitespace collapsed. If it then fits in the 'width', it is returned as is. Otherwise, as many words as possible are joined and then the placeholder is appended:

>>> TextWrap::shorten("Hello world!", 12)
"Hello world!"
>>> TextWrap::shorten("Hello world!", 11)
"Hello [...]"

◆ wrap()

list TextWrap::wrap ( string  text,
int  width = 70,
*hash  opts 
)

Wrap a single paragraph of text, returning a list of wrapped lines.

Reformat the single paragraph in 'text' so it fits in lines of no more than 'width' columns, and return a list of wrapped lines. By default, tabs in 'text' are expanded with expandtabs(), and all other whitespace characters (including newline) are converted to space. See TextWrapper class for available keyword args to customize wrapping behaviour.