sequences.py — Blessed 1.17.7 documentation

# sequences.py

Module providing ‘sequence awareness’.

class `SequenceTextWrapper`(width, term, **kwargs)

  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)

    Sequence-aware variant of `textwrap.TextWrapper._wrap_chunks()`.

    Raises

      ValueError – `self.width` is not a positive integer

    Return 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)

    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`

  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()`, and `length()`.

  Class constructor.

  Parameters

    - sequence_text (str) – A string that may contain sequences.

    - term (blessed.Terminal) – `Terminal` instance.

  `ljust`(width, fillchar=' ')

    Return string containing sequences, left-adjusted.

    Parameters

      - width (int) – Total width given to left-adjust `text`. If unspecified, the width of the attached terminal is used (default).

      - fillchar (str) – String for padding right-of `text`.

    Returns

      String of `text`, left-aligned by `width`.

    Return type

      str

  `rjust`(width, fillchar=' ')

    Return string containing sequences, right-adjusted.

    Parameters

      - width (int) – Total width given to right-adjust `text`. If unspecified, the width of the attached terminal is used (default).

      - fillchar (str) – String for padding left-of `text`.

    Returns

      String of `text`, right-aligned by `width`.

    Return type

      str

  `center`(width, fillchar=' ')

    Return string containing sequences, centered.

    Parameters

      - width (int) – Total width given to center `text`. If unspecified, the width of the attached terminal is used (default).

      - fillchar (str) – String for padding left and right-of `text`.

    Returns

      String of `text`, centered by `width`.

    Return type

      str

  `length`()

    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).

  `strip`(chars=None)

    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)

    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)

    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.

  `strip_seqs`()

    Return `text` stripped of only its terminal sequences.

    Return type

      str

    Returns

      Text with terminal sequences removed

  `padd`(strip=False)

    Return non-destructive horizontal movement as destructive spacing.

    Parameters

      strip (bool) – Strip terminal sequences

    Return type

      str

    Returns

      Text adjusted for horizontal movement

`iter_parse`(term, text)

  Generator yields (text, capability) for characters of `text`.

  value for `capability` may be `None`, where `text` is `str` of length 1. Otherwise, `text` is a full matching sequence of given capability.

`measure_length`(text, term)

  Deprecated since version 1.12.0..

  Return type

    int

  Returns

    Length of the first sequence in the string