sequences.py¶
Module providing ‘sequence awareness’.
-
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 integerReturn type: list 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.
-
class
Sequence
[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: -
ljust
(width, fillchar=' ')[source]¶ Return string containing sequences, left-adjusted.
Parameters: Returns: String of
text
, left-aligned bywidth
.Return type:
-
rjust
(width, fillchar=' ')[source]¶ Return string containing sequences, right-adjusted.
Parameters: Returns: String of
text
, right-aligned bywidth
.Return type:
-
center
(width, fillchar=' ')[source]¶ Return string containing sequences, centered.
Parameters: Returns: String of
text
, centered bywidth
.Return type:
-
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'コンニチハ'), term >>> Sequence(msg).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).
-
strip
(chars=None)[source]¶ Return string of sequences, leading and trailing whitespace removed.
Parameters: chars (str) – Remove characters in chars instead of whitespace. Return type: str Returns: string of sequences with leading and trailing whitespace removed.
-
lstrip
(chars=None)[source]¶ Return string of all sequences and leading whitespace removed.
Parameters: chars (str) – Remove characters in chars instead of whitespace. Return type: str Returns: string of sequences with leading removed.
-
rstrip
(chars=None)[source]¶ Return string of all sequences and trailing whitespace removed.
Parameters: chars (str) – Remove characters in chars instead of whitespace. Return type: str Returns: string of sequences with trailing removed.
-