Text¶
flutils offers the following text functions and objects:
len_without_ansi
(seq)[source]¶Return the character length of the given
Sequence
without counting any ANSI codes.New in version 0.6
Example
>>> from flutils.txtutils import len_without_ansi >>> text = '\x1b[38;5;209mfoobar\x1b[0m' >>> len_without_ansi(text) 6
- class
AnsiTextWrapper
(width=70, initial_indent='', subsequent_indent='', expand_tabs=True, replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, drop_whitespace=True, break_on_hyphens=True, tabsize=8, *, max_lines=None, placeholder=' [...]')[source]¶A
TextWrapper
object that correctly wraps text containing ANSI codes.New in version 0.6
- Parameters
width (int, optional) – The maximum length of wrapped lines. As long as there are no individual words in the input text longer than this given
width
,AnsiTextWrapper
guarantees that no output line will be longer thanwidth
characters. Defaults to:70
initial_indent (str, optional) – Text that will be prepended to the first line of wrapped output. Counts towards the length of the first line. An empty string value will not indent the first line. Defaults to:
''
an empty string.subsequent_indent (str, optional) – Text that will be prepended to all lines of wrapped output except the first. Counts towards the length of each line except the first. Defaults to:
''
an empty string.expand_tabs (bool, optional) – If
True
, then all tab characters in text will be expanded to spaces using theexpandtabs
. Also see thetabsize
argument. Defaults to:True
.replace_whitespace (bool, optional) – If
True
, after tab expansion but before wrapping, the wrap() method will replace each whitespace character with a single space. The whitespace characters replaced are as follows: tab, newline, vertical tab, form-feed, and carriage return ('\t\n\v\f\r'
). Defaults to:True
.fix_sentence_endings (bool, optional) – If
True
,AnsiTextWrapper
attempts to detect sentence endings and ensure that sentences are always separated by exactly two spaces. This is generally desired for text in a monospaced font. However, the sentence detection algorithm is imperfect; it assumes that a sentence ending consists of a lowercase letter followed by one of ‘.’, ‘!’, or ‘?’, possibly followed by one of ‘”’ or “’”, followed by a space. Defaults to:False
.break_long_words (bool, optional) – If
True
, then words longer than width will be broken in order to ensure that no lines are longer than width. If it isFalse
, long words will not be broken, and some lines may be longer than width. (Long words will be put on a line by themselves, in order to minimize the amount by which width is exceeded.) Defaults to:True
.drop_whitespace (bool, optional) – If
True
, whitespace at the beginning and ending of every line (after wrapping but before indenting) is dropped. Whitespace at the beginning of the paragraph, however, is not dropped if non-whitespace follows it. If whitespace being dropped takes up an entire line, the whole line is dropped. Defaults to:True
break_on_hyphens (bool, optional) – If
True
, wrapping will occur preferably on whitespaces and right after hyphens in compound words, as it is customary in English. Iffalse
, only whitespaces will be considered as potentially good places for line breaks, but you need to setbreak_long_words
toFalse
if you want truly insecable words. Defaults to:True
.tabsize (int, optional) – If
expand_tabs
isTrue
, then all tab characters in text will be expanded to zero or more spaces, depending on the current column and the given tab size. Defaults to:8
.max_lines (
int
orNone
, optional) – If notNone
, then the output will contain at mostmax_lines lines
, withplaceholder
appearing at the end of the output. Defaults to:None
.placeholder (str, optional) – Text that will appear at the end of the output text if it has been truncated. Defaults to:
' [...]'
Note
The
initial_indent
,subsequent_indent
andplaceholder
parameters can also contain ANSI codes.Note
If
expand_tabs
isFalse
andreplace_whitespace
isTrue
, each tab character will be replaced by a single space, which is not the same as tab expansion.Note
If
replace_whitespace
isFalse
, newlines may appear in the middle of a line and cause strange output. For this reason, text should be split into paragraphs (usingstr.splitlines
or similar) which are wrapped separately.Example
Use
AnsiTextWrapper
the same way as usingTextWrapper
:from flutils.txtutils import AnsiTextWrapper text = ( '\x1b[31m\x1b[1m\x1b[4mLorem ipsum dolor sit amet, ' 'consectetur adipiscing elit. Cras fermentum maximus ' 'auctor. Cras a varius ligula. Phasellus ut ipsum eu ' 'erat consequat posuere.\x1b[0m Pellentesque habitant ' 'morbi tristique senectus et netus et malesuada fames ac ' 'turpis egestas. Maecenas ultricies lacus id massa ' 'interdum dignissim. Curabitur \x1b[38;2;55;172;230m ' 'efficitur ante sit amet nibh consectetur, consequat ' 'rutrum nunc\x1b[0m egestas. Duis mattis arcu eget orci ' 'euismod, sit amet vulputate ante scelerisque. Aliquam ' 'ultrices, turpis id gravida vestibulum, tortor ipsum ' 'consequat mauris, eu cursus nisi felis at felis. ' 'Quisque blandit lacus nec mattis suscipit. Proin sed ' 'tortor ante. Praesent fermentum orci id dolor ' '\x1b[38;5;208meuismod, quis auctor nisl sodales.\x1b[0m' ) wrapper = AnsiTextWrapper(width=40) wrapped_text = wrapper.fill(text) print(wrapped_text)The output:
wrap
(text)[source]¶Wraps the single paragraph in the given
text
so every line is at mostwidth
characters long. All wrapping options are taken from instance attributes of theAnsiTextWrapper
instance.