Skip to content

Commit

Permalink
Merge pull request #22 from jquast/proxy-ansi-support-for-cursor
Browse files Browse the repository at this point in the history
proxy support for civis and cnorm on 'ansi' terms.
  • Loading branch information
jquast committed Sep 1, 2014
2 parents f47ce00 + 35c88ae commit 8dc2cde
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
38 changes: 25 additions & 13 deletions blessed/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,30 @@ def __call__(self, *args):


def get_proxy_string(term, attr):
""" Returns an instance of ParameterizingProxyString
for (some kinds) of terminals and attributes.
""" Proxy and return callable StringClass for proxied attributes.
We know that some kinds of terminal kinds support sequences that the
terminfo database often doesn't report -- such as the 'move_x' attribute
for terminal type 'screen', or 'hide_cursor' for 'ansi'.
Returns instance of ParameterizingProxyString or NullCallableString.
"""
if term._kind == 'screen' and attr in ('hpa', 'vpa'):
if attr == 'hpa':
fmt = u'\x1b[{0}G'
elif attr == 'vpa':
fmt = u'\x1b[{0}d'
fmt_arg = lambda *arg: (arg[0] + 1,)
return ParameterizingProxyString((fmt, fmt_arg),
term.normal, 'hpa')
return None
return {
'screen': {
# proxy move_x/move_y for 'screen' terminal type.
'hpa': ParameterizingProxyString(
(u'\x1b[{0}G', lambda *arg: (arg[0] + 1,)), term.normal, attr),
'vpa': ParameterizingProxyString(
(u'\x1b[{0}d', lambda *arg: (arg[0] + 1,)), term.normal, attr),
},
'ansi': {
# proxy show/hide cursor for 'ansi' terminal type.
'civis': ParameterizingProxyString(
(u'\x1b[?25l', lambda *arg: ()), term.normal, attr),
'cnorm': ParameterizingProxyString(
(u'\x1b[?25h', lambda *arg: ()), term.normal, attr),
}
}.get(term._kind, {}).get(attr, None)


class FormattingString(text_type):
Expand Down Expand Up @@ -299,8 +311,8 @@ def resolve_attribute(term, attr):
return FormattingString(u''.join(resolution), term.normal)
else:
# and, for special terminals, such as 'screen', provide a Proxy
# ParameterizingString for attributes they do not claim to support, but
# actually do! (such as 'hpa' and 'vpa').
# ParameterizingString for attributes they do not claim to support,
# but actually do! (such as 'hpa' and 'vpa').
proxy = get_proxy_string(term, term._sugar.get(attr, attr))
if proxy is not None:
return proxy
Expand Down
16 changes: 16 additions & 0 deletions blessed/tests/test_sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ def child(kind):
child('screen')


def test_inject_civis_and_cnorm_for_ansi():
"""Test injection of cvis attribute for ansi."""
@as_subprocess
def child(kind):
t = TestTerminal(kind=kind, stream=StringIO(), force_styling=True)
with t.hidden_cursor():
pass
expected_output = u''.join(
(unicode_cap('sc'),
u'\x1b[?25l\x1b[?25h',
unicode_cap('rc')))
assert (t.stream.getvalue() == expected_output)

child('ansi')


def test_zero_location(all_standard_terms):
"""Make sure ``location()`` pays attention to 0-valued args."""
@as_subprocess
Expand Down

0 comments on commit 8dc2cde

Please sign in to comment.