o
    w¨ÊhLd  ã                   @  s¸  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 ddl
mZ ejdks-J ‚es9d dlZd d	lmZ d d
lmZmZ d dlmZmZ d dlmZmZmZmZmZ d dlmZ d dlmZmZ d dl m!Z! d dl"m#Z# d dl$m%Z%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 g d¢Z1dZ2dZ3dZ4dZ5G dd„ de0ƒZ6G dd„ de6ƒZ7G dd„ dƒZ8G dd„ dƒZ9e	d-d%d&„ƒZ:e	d.d'd(„ƒZ;G d)d*„ d*ƒZ<G d+d,„ d,e<ƒZ=dS )/é    )ÚannotationsN)Úabstractmethod)Úget_running_loop)Úcontextmanageré   )ÚSPHINX_AUTODOC_RUNNINGÚwin32)Úwindll)ÚArrayÚpointer)ÚDWORDÚHANDLE)ÚCallableÚContextManagerÚIterableÚIteratorÚTextIO©Úrun_in_executor_with_context)Úcreate_win32_eventÚwait_for_handles)ÚKeyPress)ÚKeys)ÚMouseButtonÚMouseEventType)ÚINPUT_RECORDÚKEY_EVENT_RECORDÚMOUSE_EVENT_RECORDÚSTD_INPUT_HANDLEÚ
EventTypesé   )ÚREVERSE_ANSI_SEQUENCES)ÚInput)Ú
Win32InputÚConsoleInputReaderÚraw_modeÚcooked_modeÚattach_win32_inputÚdetach_win32_inputé   c                   @  s,   e Zd ZdZd
dd„Zeeddd„ƒƒZd	S )Ú_Win32InputBasez;
    Base class for `Win32Input` and `Win32PipeInput`.
    ÚreturnÚNonec                 C  s   t ƒ | _d S ©N)Ú_Win32HandlesÚwin32_handles©Úself© r2   úL/var/www/html/env/lib/python3.10/site-packages/prompt_toolkit/input/win32.pyÚ__init__=   s   z_Win32InputBase.__init__r   c                 C  ó   d S r-   r2   r0   r2   r2   r3   Úhandle@   s   z_Win32InputBase.handleN©r+   r,   ©r+   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r4   Úpropertyr   r6   r2   r2   r2   r3   r*   8   s    
r*   c                      sš   e Zd ZdZd'd(‡ fdd„Zd)dd„Zd*dd„Zd+dd„Zd,dd„Ze	d-dd„ƒZ
d*dd„Zd*dd„Zd.dd„Zd/d d!„Zd,d"d#„Ze	d0d%d&„ƒZ‡  ZS )1r#   z<
    `Input` class that reads from the Windows console.
    NÚstdinúTextIO | Noner+   r,   c                   s   t ƒ  ¡  tƒ | _d S r-   )Úsuperr4   r$   Úconsole_input_reader)r1   r>   ©Ú	__class__r2   r3   r4   K   s   
zWin32Input.__init__Úinput_ready_callbackúCallable[[], None]úContextManager[None]c                 C  s
   t | |ƒS )zj
        Return a context manager that makes this input active in the current
        event loop.
        )r'   )r1   rD   r2   r2   r3   ÚattachO   ó   
zWin32Input.attachc                 C  s   t | ƒS )z{
        Return a context manager that makes sure that this input is not active
        in the current event loop.
        )r(   r0   r2   r2   r3   ÚdetachV   s   zWin32Input.detachúlist[KeyPress]c                 C  s   t | j ¡ ƒS r-   )ÚlistrA   Úreadr0   r2   r2   r3   Ú	read_keys]   ó   zWin32Input.read_keysc                 C  r5   r-   r2   r0   r2   r2   r3   Úflush`   ó   zWin32Input.flushÚboolc                 C  ó   dS )NFr2   r0   r2   r2   r3   Úclosedc   s   zWin32Input.closedc                 C  ó   t ƒ S r-   )r%   r0   r2   r2   r3   r%   g   ó   zWin32Input.raw_modec                 C  rT   r-   )r&   r0   r2   r2   r3   r&   j   rU   zWin32Input.cooked_modeÚintc                 C  s
   t j ¡ S r-   )Úsysr>   Úfilenor0   r2   r2   r3   rX   m   rH   zWin32Input.filenoÚstrc                 C  rR   )Nzwin32-inputr2   r0   r2   r2   r3   Útypeahead_hasht   rP   zWin32Input.typeahead_hashc                 C  s   | j  ¡  d S r-   )rA   Úcloser0   r2   r2   r3   r[   w   rN   zWin32Input.closer   c                 C  s   | j jS r-   )rA   r6   r0   r2   r2   r3   r6   z   s   zWin32Input.handler-   )r>   r?   r+   r,   )rD   rE   r+   rF   )r+   rF   )r+   rJ   r7   )r+   rQ   )r+   rV   )r+   rY   r8   )r9   r:   r;   r<   r4   rG   rI   rM   rO   r=   rS   r%   r&   rX   rZ   r[   r6   Ú__classcell__r2   r2   rB   r3   r#   F   s     








r#   c                   @  s>  e Zd ZdZi dej“dej“dej“dej“dej	“dej
“dej“d	ej“d
ej“dej“dej“dej“dej“dej“dej“dej“dej“i dej“dej“dej“dej“dej“dej“dej“dej“dej“dej“dej “dej!“dej"“d ej#“d!ej$“d"ej%“¥Z&i d#ej'“d$ej(“d%ej)“d&ej*“d'ej+“d(ej,“d)ej-“d*ej.“d+ej/“d,ej0“d-ej1“d.ej2“d/ej3“d0ej4“d1ej5“d2ej6“d3ej7“ej8ej9ej:ej;ej<d4œ¥Z=d5Z>d6Z?d7Z@d8ZAd9ZBd`dad?d@„ZCdbdAdB„ZDdcdDdE„ZEdddHdI„ZFdedOdP„ZGeHdfdSdT„ƒZIeHdgdVdW„ƒZJdhdZd[„ZKdid]d^„ZLd_S )jr$   z|
    :param recognize_paste: When True, try to discover paste actions and turn
        the event into a BracketedPaste.
    ó   ó    ó   ó   ó   ó   ó   ó   ó   ó   ó   	ó   
ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   ó   é!   é"   é#   é$   é%   é&   é'   é(   é-   é.   ép   éq   ér   és   ét   éu   év   )éw   éx   éy   éz   é{   r   r    é   é   r)   TÚrecognize_pasterQ   r+   r,   c                 C  s\   d | _ || _|  tj ¡ rttj t	¡ƒ| _
d S t dtjtjB ¡| _ tt | j ¡ƒ| _
d S )NzCONIN$)Ú_fdconr–   rW   r>   Úisattyr   r	   Úkernel32ÚGetStdHandler   r6   ÚosÚopenÚO_RDWRÚO_BINARYÚmsvcrtÚget_osfhandle)r1   r–   r2   r2   r3   r4   Í   s   
zConsoleInputReader.__init__c                 C  s   | j durt | j ¡ dS dS )zClose fdcon.N)r—   r›   r[   r0   r2   r2   r3   r[   Ú   s   
ÿzConsoleInputReader.closeúIterable[KeyPress]c           	   	   #  sN  � d}t dƒ}t| }|ƒ }tˆ jgdd�sdS tj ˆ jt|ƒ|t|ƒ¡ tˆ  	||¡ƒ}‡ fdd„|D ƒ}tˆ  
|¡ƒ}ˆ jr ˆ  |¡r t|ƒ}|D ]Q}g }|rŠt|jtƒra|jtjtjhv rŠ| |j¡ zt|ƒ}W n tyx   d}Y nw |rŠt|jtƒra|jtjtjhv sa|r–ttjd |¡ƒV  |dur�|V  qLdS |E dH  dS )zñ
        Return a list of `KeyPress` instances. It won't return anything when
        there was nothing to read.  (This function doesn't block.)

        http://msdn.microsoft.com/en-us/library/windows/desktop/ms684961(v=vs.85).aspx
        i   r   )ÚtimeoutNc                   s   g | ]}ˆ   |¡‘qS r2   )Ú_insert_key_data)Ú.0Úkeyr0   r2   r3   Ú
<listcomp>ÿ   s    z+ConsoleInputReader.read.<locals>.<listcomp>Ú )r   r   r   r6   r	   r™   ÚReadConsoleInputWr   rK   Ú	_get_keysÚ_merge_paired_surrogatesr–   Ú	_is_pasteÚiterÚ
isinstancer¥   r   ÚControlJÚControlMÚappendÚdataÚnextÚStopIterationr   ÚBracketedPasteÚjoin)	r1   Ú	max_countrL   ÚarrtypeÚinput_recordsÚall_keysÚgenÚkr±   r2   r0   r3   rL   ß   sL   €ÿ
ÿÿù
ÿ€ïzConsoleInputReader.readÚ	key_pressr   c                 C  s6   |j r|S t|jtƒrt |jd¡}nd}t|j|ƒS )z@
        Insert KeyPress data, for vt100 compatibility.
        r§   )r±   r­   r¥   r   r!   Úgetr   )r1   r¼   r±   r2   r2   r3   r£     s   z#ConsoleInputReader._insert_key_datarL   r   r¸   úArray[INPUT_RECORD]úIterator[KeyPress]c                 c  sv   � t |jƒD ]2}|| }|jtv r8t|jt|j ƒ}t|tƒr+|jr+|  	|¡E dH  qt|t
ƒr8|  |¡E dH  qdS )zR
        Generator that yields `KeyPress` objects from the input records.
        N)ÚrangeÚvalueÚ	EventTyper   ÚgetattrÚEventr­   r   ÚKeyDownÚ_event_to_key_pressesr   Ú_handle_mouse)r1   rL   r¸   ÚiÚirÚevr2   r2   r3   r©   +  s   €

€ðzConsoleInputReader._get_keysÚkey_pressesrJ   c                 c  s´   � d}| D ]K}t |jtƒ }|od|j  kodkn  }|o+d|j  ko)dkn  }|rH|rC|j|j  dd¡ d¡}t||ƒ}n|V  d}|rM|}q|V  q|rX|V  dS dS )zm
        Combines consecutive KeyPresses with high and low surrogates into
        single characters
        Nu   í €u   í¯¿u   í°€u   í¿¿z	utf-16-leÚsurrogatepass)r­   r¥   r   ÚencodeÚdecoder   )rË   Úbuffered_high_surrogater¥   Úis_textÚis_high_surrogateÚis_low_surrogateÚfullcharr2   r2   r3   rª   C  s*   €
ý
ÿz+ConsoleInputReader._merge_paired_surrogatesÚkeysc                 C  sJ   d}d}| D ]}t |jtƒs|d7 }|jtjkr|d7 }q|dko$|dkS )a<  
        Return `True` when we should consider this list of keys as a paste
        event. Pasted text on windows will be turned into a
        `Keys.BracketedPaste` event. (It's not 100% correct, but it is probably
        the best possible way to detect pasting of text and handle that
        correctly.)
        r   r    )r­   r¥   r   r¯   )rÔ   Ú
text_countÚnewline_countr»   r2   r2   r3   r«   d  s   €zConsoleInputReader._is_pasterÊ   r   c                 C  s´  t |tƒr|js
J ‚d}|j}|jj}| dd¡}|dkr-|j| jv r,t	| j|j dƒ}n|| j
v rE| j
| tjkr<d}t	| j
| |ƒ}nt	||ƒ}|| j@ sT|| j@ rŠ|| j@ rŠ|rŠtjtjtjtjtjtjtjtjtjtjtjtjtjtjtjtjtj tj!i	}| "|j#|j#¡|_#|| j@ s”|| j@ rÉ|rÉtjtj$tjtj%tjtj&tjtj'tjtj(tjtj)tjtj*tj+tj,tjtj-tj tj.i
}| "|j#|j#¡|_#|| j@ �r	|�r	tj/tj0tjtj1tjtj2tjtj3tjtj4tjtj5tjtj6tjtj7tj+tj8tjtj9tj tj:i}| "|j#|j#¡|_#|| j@ �s|| j@ �r$|�r$|j;dk�r$t	tj<dƒ}|| j@ �s0|| j@ �rB|�rB|j#tjk�rBt	tj=dƒ|gS |�rX|| j>@ }|�rUt	tj=dƒ|gS |gS g S )zU
        For this `KEY_EVENT_RECORD`, return a list of `KeyPress` instances.
        Nzutf-8rÌ   ú r§   Ú
ú )?r­   r   rÅ   ÚControlKeyStateÚuCharÚUnicodeCharrÍ   ÚVirtualKeyCodeÚkeycodesr   Úmappingsr   r®   ÚLEFT_CTRL_PRESSEDÚRIGHT_CTRL_PRESSEDÚSHIFT_PRESSEDÚLeftÚControlShiftLeftÚRightÚControlShiftRightÚUpÚControlShiftUpÚDownÚControlShiftDownÚHomeÚControlShiftHomeÚEndÚControlShiftEndÚInsertÚControlShiftInsertÚPageUpÚControlShiftPageUpÚPageDownÚControlShiftPageDownr½   r¥   ÚControlLeftÚControlRightÚ	ControlUpÚControlDownÚControlHomeÚ
ControlEndÚControlInsertÚDeleteÚControlDeleteÚControlPageUpÚControlPageDownÚTabÚBackTabÚ	ShiftLeftÚ
ShiftRightÚShiftUpÚ	ShiftDownÚ	ShiftHomeÚShiftEndÚShiftInsertÚShiftDeleteÚShiftPageUpÚShiftPageDownr±   ÚControlSpaceÚEscapeÚLEFT_ALT_PRESSED)r1   rÊ   ÚresultÚcontrol_key_stateÚu_charÚ
ascii_charÚmappingÚmeta_pressedr2   r2   r3   rÆ   z  s°   €
ÿ
þýûú	÷ÿþýöõþýûþýû

z(ConsoleInputReader._event_to_key_pressesr   c                 C  s´   |j }|j}d}tj}|t@ r|dkrtj}ntj}n|tkr#tj	}n|t
kr*tj}|t@ r1tj}|du r@|dkr=tj}ntj}d |j|jt|jjƒt|jjƒg¡}ttj|ƒgS )zK
        Handle mouse events. Return a list of KeyPress instances.
        Nr   ú;)Ú
EventFlagsÚButtonStater   ÚNONEÚMOUSE_WHEELEDr   Ú	SCROLL_UPÚSCROLL_DOWNÚFROM_LEFT_1ST_BUTTON_PRESSEDÚLEFTÚRIGHTMOST_BUTTON_PRESSEDÚRIGHTÚMOUSE_MOVEDÚ
MOUSE_MOVEÚ
MOUSE_DOWNÚMOUSE_UPrµ   rÁ   rY   ÚMousePositionÚXÚYr   r   ÚWindowsMouseEvent)r1   rÊ   Úevent_flagsÚbutton_stateÚ
event_typeÚbuttonr±   r2   r2   r3   rÇ   þ  s4   

üÿz ConsoleInputReader._handle_mouseN)T)r–   rQ   r+   r,   r7   )r+   r¡   )r¼   r   r+   r   )rL   r   r¸   r¾   r+   r¿   )rË   rJ   r+   r¿   )rÔ   rJ   r+   rQ   )rÊ   r   r+   rJ   )rÊ   r   r+   rJ   )Mr9   r:   r;   r<   r   r  r  ÚControlAÚControlBÚControlCÚControlDÚControlEÚControlFÚControlGÚControlHÚControlIr®   ÚControlKÚControlLr¯   ÚControlNÚControlOÚControlPÚControlQÚControlRÚControlSÚControlTÚControlUÚControlVÚControlWÚControlXÚControlYÚControlZÚControlBackslashÚControlSquareCloseÚControlCircumflexÚControlUnderscoreÚ	Backspacerß   rñ   ró   rí   rë   rã   rç   rå   ré   rï   rü   ÚF1ÚF2ÚF3ÚF4ÚF5ÚF6ÚF7ÚF8ÚF9ÚF10ÚF11ÚF12rÞ   r  ÚRIGHT_ALT_PRESSEDrâ   rà   rá   r4   r[   rL   r£   r©   Ústaticmethodrª   r«   rÆ   rÇ   r2   r2   r2   r3   r$      sþ    ÿþýüûúùø	÷
öõôóòñðïîíìëêéèçæåäãâá à!ß%þýüûùø	÷
öõôòñðïîíìç


>
 
 r$   c                   @  s.   e Zd ZdZddd„Zdd
d„Zddd„ZdS )r.   a¨  
    Utility to keep track of which handles are connectod to which callbacks.

    `add_win32_handle` starts a tiny event loop in another thread which waits
    for the Win32 handle to become ready. When this happens, the callback will
    be called in the current asyncio event loop using `call_soon_threadsafe`.

    `remove_win32_handle` will stop this tiny event loop.

    NOTE: We use this technique, so that we don't have to use the
          `ProactorEventLoop` on Windows and we can wait for things like stdin
          in a `SelectorEventLoop`. This is important, because our inputhook
          mechanism (used by IPython), only works with the `SelectorEventLoop`.
    r+   r,   c                 C  s   i | _ i | _d S r-   )Ú_handle_callbacksÚ_remove_eventsr0   r2   r2   r3   r4   >  s   
z_Win32Handles.__init__r6   r   ÚcallbackrE   c                   sv   ˆj }|du rtdƒ‚|  ˆ¡ tƒ ‰ˆ | j|< tƒ ‰ˆ| j|< d
‡ ‡‡fdd„‰d
‡‡‡‡fdd„‰tˆˆd	� dS )z7
        Add a Win32 handle to the event loop.
        NúInvalid handle.r+   r,   c                
     s(   zˆ ƒ  W t ˆˆd� d S t ˆˆd� w )N©Úloopr   r2   )rY  r\  Úwaitr2   r3   ÚreadyY  s   z-_Win32Handles.add_win32_handle.<locals>.readyc                    s2   t ˆˆ gƒ} | ˆu rtj ˆ¡ d S ˆ ˆ¡ d S r-   )r   r	   r™   ÚCloseHandleÚcall_soon_threadsafe)r  )r6   r\  r^  Úremove_eventr2   r3   r]  c  s
   z,_Win32Handles.add_win32_handle.<locals>.waitr[  r7   )rÁ   Ú
ValueErrorÚremove_win32_handler   rW  r   rX  r   )r1   r6   rY  Úhandle_valuer2   )rY  r6   r\  r^  ra  r]  r3   Úadd_win32_handleE  s   



z_Win32Handles.add_win32_handleúCallable[[], None] | Nonec                 C  sf   |j du rdS z	| j |j ¡}W n	 ty   Y nw tj |¡ z| j |j ¡W S  ty2   Y dS w )zt
        Remove a Win32 handle from the event loop.
        Return either the registered handler or `None`.
        N)rÁ   rX  ÚpopÚKeyErrorr	   r™   ÚSetEventrW  )r1   r6   Úeventr2   r2   r3   rc  p  s   
ÿÿz!_Win32Handles.remove_win32_handleNr7   )r6   r   rY  rE   r+   r,   )r6   r   r+   rf  )r9   r:   r;   r<   r4   re  rc  r2   r2   r2   r3   r.   .  s
    

+r.   ÚinputrY  rE   r+   úIterator[None]c              
   c  s€   � | j }| j}|jdu rtdƒ‚| |¡}| ||¡ zdV  W | |¡ |r/| ||¡ dS dS | |¡ |r?| ||¡ w w )zÚ
    Context manager that makes this input active in the current event loop.

    :param input: :class:`~prompt_toolkit.input.Input` object.
    :param input_ready_callback: Called when the input is ready to read.
    NrZ  ©r/   r6   rÁ   rb  rc  re  )rk  rY  r/   r6   Úprevious_callbackr2   r2   r3   r'   †  s"   €



ÿ
þÿr'   c              
   c  s`   � | j }| j}|jd u rtdƒ‚| |¡}zd V  W |r$| ||¡ d S d S |r/| ||¡ w w )NrZ  rm  )rk  r/   r6   rn  r2   r2   r3   r(   £  s   €

ÿÿr(   c                   @  s:   e Zd ZdZdddd„Zdd	d
„Zddd„Zddd„ZdS )r%   zà
    ::

        with raw_mode(stdin):
            ''' the windows terminal is now in 'raw' mode. '''

    The ``fileno`` attribute is ignored. This is to be compatible with the
    `raw_input` method of `.vt100_input`.
    NrX   ú
int | Noner+   r,   c                 C  s   t tj t¡ƒ| _d S r-   )r   r	   r™   rš   r   r6   )r1   rX   r2   r2   r3   r4   ¿  s   zraw_mode.__init__c                 C  s,   t ƒ }tj | jt|ƒ¡ || _|  ¡  d S r-   )r   r	   r™   ÚGetConsoleModer6   r   Úoriginal_modeÚ_patch)r1   rq  r2   r2   r3   Ú	__enter__Â  s   zraw_mode.__enter__c                 C  s2   d}d}d}t j | j| jj||B |B  @ ¡ d S ©Nr)   r   r    ©r	   r™   ÚSetConsoleModer6   rq  rÁ   ©r1   ÚENABLE_ECHO_INPUTÚENABLE_LINE_INPUTÚENABLE_PROCESSED_INPUTr2   r2   r3   rr  Ê  s   ÿþzraw_mode._patchÚaÚobjectc                 G  s   t j | j| j¡ d S r-   )r	   r™   rv  r6   rq  )r1   r{  r2   r2   r3   Ú__exit__Ö  s   zraw_mode.__exit__r-   )rX   ro  r+   r,   r7   )r{  r|  r+   r,   )r9   r:   r;   r<   r4   rs  rr  r}  r2   r2   r2   r3   r%   ´  s    


r%   c                   @  s   e Zd ZdZddd„ZdS )r&   zx
    ::

        with cooked_mode(stdin):
            ''' The pseudo-terminal stdin is now used in cooked mode. '''
    r+   r,   c                 C  s0   d}d}d}t j | j| jj||B |B B ¡ d S rt  ru  rw  r2   r2   r3   rr  ã  s   
ÿþzcooked_mode._patchNr7   )r9   r:   r;   r<   rr  r2   r2   r2   r3   r&   Û  s    r&   )rk  r*   rY  rE   r+   rl  )rk  r*   r+   rl  )>Ú
__future__r   r›   rW   Úabcr   Úasyncior   Ú
contextlibr   Úutilsr   ÚplatformrŸ   Úctypesr	   r
   r   Úctypes.wintypesr   r   Útypingr   r   r   r   r   Úprompt_toolkit.eventloopr   Úprompt_toolkit.eventloop.win32r   r   Ú(prompt_toolkit.key_binding.key_processorr   Úprompt_toolkit.keysr   Úprompt_toolkit.mouse_eventsr   r   Úprompt_toolkit.win32_typesr   r   r   r   r   Úansi_escape_sequencesr!   Úbaser"   Ú__all__r  r  r   r  r*   r#   r$   r.   r'   r(   r%   r&   r2   r2   r2   r3   Ú<module>   sP    9   2X'