Iterator that returns the next meaningful input given to a terminal,
    whenever a key is pressed.
    `term` is an instance of terminfo.Term, needed to understand what the
    escape sequences mean.
    
    The yielded value will be one of
    - PrintableChar
    - ControlKey
    - EscapeSequence
    
    Note that only PrintableChar has a non-empty string representation,
    so something like the following may come in handy
    >>> print(''.join(get_rich_char(term)))

def get_rich_char(prompt=u'', term=None):
    """Iterator that returns the next meaningful input given to a terminal,
    whenever a key is pressed.
    `term` is an instance of terminfo.Term, needed to understand what the
    escape sequences mean.
    
    The yielded value will be one of
    - PrintableChar
    - ControlKey
    - EscapeSequence
    
    Note that only PrintableChar has a non-empty string representation,
    so something like the following may come in handy
    >>> print(''.join(get_rich_char(term)))
    """

    if not term:
        term = terminfo.load_terminfo()

    iterator = get_char(prompt)
    for c in iterator:
        try:
            raise_if_start_escape_sequence(c)
            
            if is_char_printable(c):
                yield PrintableChar(c)
            else:
                yield ControlKey(c)
        
        except StartEscapeSequenceException as e:
            # We use a cicle because the input may be in the form
            # ESC-ESC-* rest of the sequence
            while True:
                try:
                    sequence = consume_escape_sequence(iterator, c)
                    yield EscapeSequence(term.detect(sequence))
                    break
                except StartEscapeSequenceException as e:
                    c = e.value

    raise StopIteration