Skip to content

Commit

Permalink
Support terminal types, such as kermit and avatar, that use bytes 127…
Browse files Browse the repository at this point in the history
…-255 in their escape sequences. Close erikrose#47.
  • Loading branch information
erikrose committed Nov 6, 2013
2 parents e104550 + dadefbb commit 1a463ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ Version History
* Add ``fullscreen()`` and ``hidden_cursor()`` to the auto-generated docs.
* Fall back to ``LINES`` and ``COLUMNS`` environment vars to find height and
width. (jquast)
* Support terminal types, such as kermit and avatar, that use bytes 127-255
in their escape sequences. (jquast)

1.5.1
* Clean up fabfile, removing the redundant ``test`` command.
Expand Down
16 changes: 11 additions & 5 deletions blessings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,8 @@ def _resolve_capability(self, atom):
"""
code = tigetstr(self._sugar.get(atom, atom))
if code:
# We can encode escape sequences as UTF-8 because they never
# contain chars > 127, and UTF-8 never changes anything within that
# range..
return code.decode('utf-8')
# See the comment in ParametrizingString for why this is latin1.
return code.decode('latin1')
return u''

def _resolve_color(self, color):
Expand Down Expand Up @@ -448,7 +446,15 @@ def __call__(self, *args):
# Re-encode the cap, because tparm() takes a bytestring in Python
# 3. However, appear to be a plain Unicode string otherwise so
# concats work.
parametrized = tparm(self.encode('utf-8'), *args).decode('utf-8')
#
# We use *latin1* encoding so that bytes emitted by tparm are
# encoded to their native value: some terminal kinds, such as
# 'avatar' or 'kermit', emit 8-bit bytes in range 0x7f to 0xff.
# latin1 leaves these values unmodified in their conversion to
# unicode byte values. The terminal emulator will "catch" and
# handle these values, even if emitting utf8-encoded text, where
# these bytes would otherwise be illegal utf8 start bytes.
parametrized = tparm(self.encode('latin1'), *args).decode('latin1')
return (parametrized if self._normal is None else
FormattingString(parametrized, self._normal))
except curses.error:
Expand Down
4 changes: 2 additions & 2 deletions blessings/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@

def unicode_cap(cap):
"""Return the result of ``tigetstr`` except as Unicode."""
return tigetstr(cap).decode('utf-8')
return tigetstr(cap).decode('latin1')


def unicode_parm(cap, *parms):
"""Return the result of ``tparm(tigetstr())`` except as Unicode."""
return tparm(tigetstr(cap), *parms).decode('utf-8')
return tparm(tigetstr(cap), *parms).decode('latin1')


def test_capability():
Expand Down

0 comments on commit 1a463ac

Please sign in to comment.