o
    PhXH                     @   s  d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 e
 dkr1ddlZddlmZ n
ddlZddlmZ G dd deZd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdZee  ZeD ]Zed7 Zee de < qng e	dej fe	dej fe	dej!fe	de"fe	d ej#fe	d!ej!fd"ej$fd#ej%fd$ej&fd%ej'fd&ej(fd'ej)fd(ej*fd)ej+fd*ej,fd+ej-fd,ej,fd-ej.fd.ej/fd/ej fd0e0fd1e1fd2e2fd3e3fd4e4fd5e5fd6e6fd7e7fd8e8fd9e9fd:e:fd;e;fd<e<fd=e=fd>e>fd?e?fd@e@fdAejAfdBejBfdCejCfdDejDfdEej/fdFej.fdGej-fdHej,fdIej$fdJej%fdKej&fdLej'fdMej,fdNej-fdOejEfdPejFfdQejGfdRejHfR ZIdSejCfdTejBfdUej/fdVej.fdWej#fdXej(fdYej)fdZejJfd[ejKfd\ejLfd]ejMffZNd^aOd_d` ZPeP  daZQdS )bz*Sub-module providing 'keyboard awareness'.    N)OrderedDict)TextTypeunicode_chrWindows)_capability_namesc                   @   sN   e Zd ZdZdddZedd Zdd	 Zeje_ed
d Z	edd Z
dS )	Keystrokea  
    A unicode-derived class for describing a single keystroke.

    A class instance describes a single keystroke received on input,
    which may contain multiple characters as a multibyte sequence,
    which is indicated by properties :attr:`is_sequence` returning
    ``True``.

    When the string is a known sequence, :attr:`code` matches terminal
    class attributes for comparison, such as ``term.KEY_LEFT``.

    The string-name of the sequence, such as ``u'KEY_LEFT'`` is accessed
    by property :attr:`name`, and is used by the :meth:`__repr__` method
    to display a human-readable form of the Keystroke this class
    instance represents. It may otherwise by joined, split, or evaluated
    just as as any other unicode string.
     Nc                 C   s   t | |}||_||_|S )zClass constructor.)r   __new___name_code)clsucscodenamenew r   K/var/www/html/env_mimamsha/lib/python3.10/site-packages/blessed/keyboard.pyr	   +   s   zKeystroke.__new__c                 C   s
   | j duS )z9Whether the value represents a multibyte sequence (bool).Nr   selfr   r   r   is_sequence2   s   
zKeystroke.is_sequencec                 C   s   | j du r
t| S | j S )zDocstring overwritten.N)r
   r   __repr__r   r   r   r   r   7   s   zKeystroke.__repr__c                 C      | j S )z;String-name of key sequence, such as ``u'KEY_LEFT'`` (str).)r
   r   r   r   r   r   =      zKeystroke.namec                 C   r   )z2Integer keycode value of multibyte sequence (int).r   r   r   r   r   r   B   r   zKeystroke.code)r   NN)__name__
__module____qualname____doc__r	   propertyr   r   r   r   r   r   r   r   r   r      s    


r   c                  C   s    dd t tD } dd | D S )a1  
    Return mapping of curses key-names paired by their keycode integer value.

    :rtype: dict
    :returns: Dictionary of (name, code) pairs for curses keyboard constant
        values and their mnemonic name. Such as code ``260``, with the value of
        its key-name identity, ``u'KEY_LEFT'``.
    c                 S   s   g | ]	}| d r|qS )KEY_
startswith).0attrr   r   r   
<listcomp>Q   s    
z'get_curses_keycodes.<locals>.<listcomp>c                 S   s   i | ]}|t t|qS r   )getattrcurses)r"   keynamer   r   r   
<dictcomp>S   s    z'get_curses_keycodes.<locals>.<dictcomp>)dirr&   )	_keynamesr   r   r   get_curses_keycodesH   s   	r+   c                  C   sH   t t } | t | dd t   D  tt| 	 | 
 S )a?  
    Return mapping of keycode integer values paired by their curses key-name.

    :rtype: dict
    :returns: Dictionary of (code, name) pairs for curses keyboard constant
        values and their mnemonic name. Such as key ``260``, with the value of
        its identity, ``u'KEY_LEFT'``.

    These keys are derived from the attributes by the same of the curses module,
    with the following exceptions:

    * ``KEY_DELETE`` in place of ``KEY_DC``
    * ``KEY_INSERT`` in place of ``KEY_IC``
    * ``KEY_PGUP`` in place of ``KEY_PPAGE``
    * ``KEY_PGDOWN`` in place of ``KEY_NPAGE``
    * ``KEY_ESCAPE`` in place of ``KEY_EXIT``
    * ``KEY_SUP`` in place of ``KEY_SR``
    * ``KEY_SDOWN`` in place of ``KEY_SF``

    This function is the inverse of :func:`get_curses_keycodes`.  With the
    given override "mixins" listed above, the keycode for the delete key will
    map to our imaginary ``KEY_DELETE`` mnemonic, effectively erasing the
    phrase ``KEY_DC`` from our code vocabulary for anyone that wishes to use
    the return value to determine the key-name by keycode.
    c                 s   s&    | ]\}}| d r||fV  qdS )r   Nr    )r"   r   valuer   r   r   	<genexpr>s   s    
z%get_keyboard_codes.<locals>.<genexpr>)r   r+   updateCURSES_KEYCODE_OVERRIDE_MIXINglobalscopyitemsdictzipvalueskeys)keycodesr   r   r   get_keyboard_codesV   s   


r8   c                 C   s@   i }| j r| j dkrtj|| j < | jr| jdkrtj|| j< |S )a  
    Determine and return mapping of left and right arrow keys sequences.

    :arg blessed.Terminal term: :class:`~.Terminal` instance.
    :rtype: dict
    :returns: Dictionary of sequences ``term._cuf1``, and ``term._cub1``,
        valued as ``KEY_RIGHT``, ``KEY_LEFT`` (when appropriate).

    This function supports :func:`get_terminal_sequences` to discover
    the preferred input sequence for the left and right application keys.

    It is necessary to check the value of these sequences to ensure we do not
    use ``u' '`` and ``u'\b'`` for ``KEY_RIGHT`` and ``KEY_LEFT``,
    preferring their true application key sequence, instead.
     )_cuf1r&   	KEY_RIGHT_cub1KEY_LEFT)termkeymapr   r   r   _alternative_left_right|   s   rA   c                    sf   t | jrdd dd t D D nd  t|   t t fddt 	 t
ddD S )a  
    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.
    c                 s   s&    | ]\}}|r| d |fV  qdS )latin1N)decode)r"   seqvalr   r   r   r-      s    
z)get_keyboard_sequences.<locals>.<genexpr>c                 s   s"    | ]\}}t ||fV  qd S N)r&   tigetstr)r"   rE   capr   r   r   r-      s
    
r   c                 3   s    | ]	}| | fV  qd S rF   r   )r"   rD   sequence_mapr   r   r-      s    
T)keyreverse)r3   does_stylingcapability_namesr2   r.   rA   DEFAULT_SEQUENCE_MIXINr   sortedr6   len)r?   r   rI   r   get_keyboard_sequences   s    

rR   c                 C   s   dd | D S )a  
    Return a set of proper prefixes for given sequence of strings.

    :arg iterable sequences
    :rtype: set
    :return: Set of all string prefixes

    Given an iterable of strings, all textparts leading up to the final
    string is returned as a unique set.  This function supports the
    :meth:`~.Terminal.inkey` method by determining whether the given
    input is a sequence that **may** lead to a final matching pattern.

    >>> prefixes(['abc', 'abdf', 'e', 'jkl'])
    set([u'a', u'ab', u'abd', u'j', u'jk'])
    c                 S   s,   h | ]}t d t|D ]}|d| qqS )   N)rangerQ   )r"   rD   ir   r   r   	<setcomp>   s   , z'get_leading_prefixes.<locals>.<setcomp>r   )	sequencesr   r   r   get_leading_prefixes   s   rX   c                 C   sH   |  D ]\}}| |rt|||| d  S qt| r | d p!ddS )a  
    Return a single :class:`Keystroke` instance for given sequence ``text``.

    :arg str text: string of characters received from terminal input stream.
    :arg OrderedDict mapper: unicode multibyte sequences, such as ``u'\x1b[D'``
        paired by their integer value (260)
    :arg dict codes: a :type:`dict` of integer values (such as 260) paired
        by their mnemonic name, such as ``'KEY_LEFT'``.
    :rtype: Keystroke
    :returns: Keystroke instance for the given sequence

    The given ``text`` may extend beyond a matching sequence, such as
    ``u\x1b[Dxxx`` returns a :class:`Keystroke` instance of attribute
    :attr:`Keystroke.sequence` valued only ``u\x1b[D``.  It is up to
    calls to determine that ``xxx`` remains unresolved.
    )r   r   r   r   r   )r   )r2   r!   r   )textmappercodessequencer   r   r   r   resolve_sequence   s
   
r]   c                 C   s   |rt d|t |   S |S )a  
    Return time remaining since ``stime`` before given ``timeout``.

    This function assists determining the value of ``timeout`` for
    class method :meth:`~.Terminal.kbhit` and similar functions.

    :arg float stime: starting time for measurement
    :arg float timeout: timeout period, may be set to None to
       indicate no timeout (where None is always returned).
    :rtype: float or int
    :returns: time remaining as float. If no time is remaining,
       then the integer ``0`` is returned.
    r   )maxtime)stimetimeoutr   r   r   
_time_left   s   rb   c                 C   s~   t   }d\}}	 | jt||d}|r ||7 }| jdd}|stj||d}|dur0	 ||fS |dur>t||s>	 ||fS q	)a?  
    Convenience read-until-pattern function, supporting :meth:`~.get_location`.

    :arg blessed.Terminal term: :class:`~.Terminal` instance.
    :arg float timeout: timeout period, may be set to None to indicate no
        timeout (where 0 is always returned).
    :arg str pattern: target regular expression pattern to seek.
    :rtype: tuple
    :returns: tuple in form of ``(match, str)``, *match*
        may be :class:`re.MatchObject` if pattern is discovered
        in input stream before timeout has elapsed, otherwise
        None. ``str`` is any remaining text received exclusive
        of the matching pattern).

    The reason a tuple containing non-matching data is returned, is that the
    consumer should push such data back into the input buffer by
    :meth:`~.Terminal.ungetch` if any was received.

    For example, when a user is performing rapid input keystrokes while its
    terminal emulator surreptitiously responds to this in-band sequence, we
    must ensure any such keyboard data is well-received by the next call to
    term.inkey() without delay.
    )Nr   T)ra   r   )patternstringN)r_   inkeyrb   research)r?   rc   ra   r`   matchbufr   r   r   r   _read_until  s    rj   )TABKP_MULTIPLYKP_ADDKP_SEPARATORKP_SUBTRACT
KP_DECIMAL	KP_DIVIDEKP_EQUALKP_0KP_1KP_2KP_3KP_4KP_5KP_6KP_7KP_8KP_9rS   r   
         	         z[Az[Bz[Cz[Dz[1;2Az[1;2Bz[1;2Cz[1;2Dz[Fz[Hz[Kz[Uz[VzOMzOjzOkzOlzOmzOnzOozOXzOpzOqzOrzOszOtzOuzOvzOwzOxzOyz[1~z[2~z[3~z[4~z[5~z[6~z[7~z[8~z[OAz[OBz[OCz[ODz[OFz[OHzOPzOQzORzOS
KEY_DELETE
KEY_INSERTKEY_PGUP
KEY_PGDOWN
KEY_ESCAPEKEY_SUP	KEY_SDOWNKEY_UP_LEFTKEY_UP_RIGHT
KEY_CENTER	KEY_BEGINgffffff?c                   C   s>   t jdrztt jd d aW d S  ty   Y d S w d S )NESCDELAYg     @@)osenvirongetintDEFAULT_ESCDELAY
ValueErrorr   r   r   r   _reinit_escdelay  s   r   )r   r8   rR   )Rr   r   rf   r_   platformcollectionsr   blessed._compatr   r   systemjinxedr&   jinxed.has_keyr   rN   curses.has_keyr   r+   r8   rA   rR   rX   r]   rb   rj   _CURSES_KEYCODE_ADDINSr^   r5   _LASTVALkeycode_namer0   	KEY_ENTERKEY_BACKSPACEKEY_TABKEY_EXITKEY_UPKEY_DOWNr<   r>   KEY_SRKEY_SF
KEY_SRIGHT	KEY_SLEFTKEY_ENDKEY_HOME	KEY_NPAGE	KEY_PPAGEKEY_KP_MULTIPLY
KEY_KP_ADDKEY_KP_SEPARATORKEY_KP_SUBTRACTKEY_KP_DECIMALKEY_KP_DIVIDEKEY_KP_EQUALKEY_KP_0KEY_KP_1KEY_KP_2KEY_KP_3KEY_KP_4KEY_KP_5KEY_KP_6KEY_KP_7KEY_KP_8KEY_KP_9KEY_FINDKEY_ICKEY_DC
KEY_SELECTKEY_F1KEY_F2KEY_F3KEY_F4rO   KEY_A1KEY_A3KEY_B2KEY_BEGr/   r   r   __all__r   r   r   r   <module>   s2   0&1@

 !"#$%&'()*+,-./23456789:;<=>?DEFGN