def test_no_term(self): """ Raise exception if tigetstr() is called before setupterm() """ jinxed._terminal.TERM = None with self.assertRaises(jinxed.error, msg='Must call setupterm() first'): jinxed.tigetstr('bold')
def test_cap_missing(self): """ Return None if capability is missing for terminal """ jinxed.setupterm('xterm') self.assertEqual(jinxed.tigetstr('howmuchwoodawoodchuckwillchuck'), None)
def unicode_cap(cap): """Return the result of ``tigetstr`` except as Unicode.""" try: val = curses.tigetstr(cap) except curses.error: val = None if val: return val.decode('latin1') return u''
def unicode_parm(cap, *parms): """Return the result of ``tparm(tigetstr())`` except as Unicode.""" try: cap = curses.tigetstr(cap) except curses.error: cap = None if cap: try: val = curses.tparm(cap, *parms) except curses.error: val = None if val: return val.decode('latin1') return u''
def get_keyboard_sequences(term): r""" Return mapping of keyboard sequences paired by keycodes. :arg blessed.Terminal term: :class:`~.Terminal` instance. :returns: mapping of keyboard unicode sequences paired by keycodes as integer. This is used as the argument ``mapper`` to the supporting function :func:`resolve_sequence`. :rtype: OrderedDict Initialize and return a keyboard map and sequence lookup table, (sequence, keycode) from :class:`~.Terminal` instance ``term``, where ``sequence`` is a multibyte input sequence of unicode characters, such as ``u'\x1b[D'``, and ``keycode`` is an integer value, matching curses constant such as term.KEY_LEFT. The return value is an OrderedDict instance, with their keys sorted longest-first. """ # A small gem from curses.has_key that makes this all possible, # _capability_names: a lookup table of terminal capability names for # keyboard sequences (fe. kcub1, key_left), keyed by the values of # constants found beginning with KEY_ in the main curses module # (such as KEY_LEFT). # # latin1 encoding is used so that bytes in 8-bit range of 127-255 # have equivalent chr() and unichr() values, so that the sequence # of a kermit or avatar terminal, for example, remains unchanged # in its byte sequence values even when represented by unicode. # sequence_map = dict(( (seq.decode('latin1'), val) for (seq, val) in ( (curses.tigetstr(cap), val) for (val, cap) in capability_names.items() ) if seq ) if term.does_styling else ()) sequence_map.update(_alternative_left_right(term)) sequence_map.update(DEFAULT_SEQUENCE_MIXIN) # This is for fast lookup matching of sequences, preferring # full-length sequence such as ('\x1b[D', KEY_LEFT) # over simple sequences such as ('\x1b', KEY_EXIT). return OrderedDict(( (seq, sequence_map[seq]) for seq in sorted( sequence_map.keys(), key=len, reverse=True)))
def resolve_capability(term, attr): """ Resolve a raw terminal capability using :func:`tigetstr`. :arg Terminal term: :class:`~.Terminal` instance. :arg str attr: terminal capability name. :returns: string of the given terminal capability named by ``attr``, which may be empty (u'') if not found or not supported by the given :attr:`~.Terminal.kind`. :rtype: str """ if not term.does_styling: return u'' val = curses.tigetstr(term._sugar.get(attr, attr)) # pylint: disable=protected-access # Decode sequences as latin1, as they are always 8-bit bytes, so when # b'\xff' is returned, this is decoded as u'\xff'. return u'' if val is None else val.decode('latin1')
def test_cap_present(self): """ Return capability if present for terminal """ jinxed.setupterm('xterm') self.assertEqual(jinxed.tigetstr('bold'), b'\x1b[1m')