sequences.py
Module providing ‘sequence awareness’.
- class Sequence(sequence_text, term)[source]
A “sequence-aware” version of the base
str
class.This unicode-derived class understands the effect of escape sequences of printable length, allowing a properly implemented
rjust()
,ljust()
,center()
, andlength()
.Class constructor.
- Parameters:
- truncate(width)[source]
Truncate a string in a sequence-aware manner.
Any printable characters beyond
width
are removed, while all sequences remain in place. Horizontal Sequences are first expanded bypadd()
.
- length()[source]
Return the printable length of string containing sequences.
Strings containing
term.left
or\b
will cause “overstrike”, but a length less than 0 is not ever returned. So_\b+
is a length of 1 (displays as+
), but\b
alone is simply a length of 0.Some characters may consume more than one cell, mainly those CJK Unified Ideographs (Chinese, Japanese, Korean) defined by Unicode as half or full-width characters.
For example:
>>> from blessed import Terminal >>> from blessed.sequences import Sequence >>> term = Terminal() >>> msg = term.clear + term.red(u'コンニチハ') >>> Sequence(msg, term).length() 10
Note
Although accounted for, strings containing sequences such as
term.clear
will not give accurate returns, it is not considered lengthy (a length of 0).
- class SequenceTextWrapper(width, term, **kwargs)[source]
Object for wrapping/filling text. The public interface consists of the wrap() and fill() methods; the other methods are just there for subclasses to override in order to tweak the default behaviour. If you want to completely replace the main wrapping algorithm, you’ll probably have to override _wrap_chunks().
- Several instance attributes control various aspects of wrapping:
- width (default: 70)
the maximum width of wrapped lines (unless break_long_words is false)
- initial_indent (default: “”)
string that will be prepended to the first line of wrapped output. Counts towards the line’s width.
- subsequent_indent (default: “”)
string that will be prepended to all lines save the first of wrapped output; also counts towards each line’s width.
- expand_tabs (default: true)
Expand tabs in input text to spaces before further processing. Each tab will become 0 .. ‘tabsize’ spaces, depending on its position in its line. If false, each tab is treated as a single character.
- tabsize (default: 8)
Expand tabs in input text to 0 .. ‘tabsize’ spaces, unless ‘expand_tabs’ is false.
- replace_whitespace (default: true)
Replace all whitespace characters in the input text by spaces after tab expansion. Note that if expand_tabs is false and replace_whitespace is true, every tab will be converted to a single space!
- fix_sentence_endings (default: false)
Ensure that sentence-ending punctuation is always followed by two spaces. Off by default because the algorithm is (unavoidably) imperfect.
- break_long_words (default: true)
Break words longer than ‘width’. If false, those words will not be broken, and some lines might be longer than ‘width’.
- break_on_hyphens (default: true)
Allow breaking hyphenated words. If true, wrapping will occur preferably on whitespaces and right after hyphens part of compound words.
- drop_whitespace (default: true)
Drop leading and trailing whitespace from lines.
- max_lines (default: None)
Truncate wrapped lines.
- placeholder (default: ‘ […]’)
Append to the last line of truncated text.
Class initializer.
This class supports the
wrap()
method.- _wrap_chunks(chunks)[source]
Sequence-aware variant of
textwrap.TextWrapper._wrap_chunks()
.- Raises:
ValueError –
self.width
is not a positive integer- Return type:
- Returns:
text chunks adjusted for width
This simply ensures that word boundaries are not broken mid-sequence, as standard python textwrap would incorrectly determine the length of a string containing sequences, and may also break consider sequences part of a “word” that may be broken by hyphen (
-
), where this implementation corrects both.
- _handle_long_word(reversed_chunks, cur_line, cur_len, width)[source]
Sequence-aware
textwrap.TextWrapper._handle_long_word()
.This simply ensures that word boundaries are not broken mid-sequence, as standard python textwrap would incorrectly determine the length of a string containing sequences, and may also break consider sequences part of a “word” that may be broken by hyphen (
-
), where this implementation corrects both.