a
    õ£Be@d  ã                   @  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sZJ ‚esrd 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!d"œd#d$„ƒZ:e	dd!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                   @  s4   e Zd ZdZddœdd„Zeeddœdd„ƒƒZd	S )
Ú_Win32InputBasez;
    Base class for `Win32Input` and `Win32PipeInput`.
    ÚNone©Úreturnc                 C  s   t ƒ | _d S ©N)Ú_Win32HandlesÚwin32_handles©Úself© r3   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/input/win32.pyÚ__init__=   s    z_Win32InputBase.__init__r   c                 C  s   d S r.   r3   r1   r3   r3   r4   Úhandle@   s    z_Win32InputBase.handleN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   Úpropertyr   r6   r3   r3   r3   r4   r*   8   s
   r*   c                      sÎ   e Zd ZdZd'dddœ‡ fdd„Zdd	d
œdd„Zd	dœdd„Zddœdd„Zddœdd„Ze	ddœdd„ƒZ
d	dœdd„Zd	dœdd„Zddœdd„Zddœd d!„Zddœd"d#„Ze	d$dœd%d&„ƒZ‡  ZS )(r#   z<
    `Input` class that reads from the Windows console.
    NzTextIO | Noner+   )Ústdinr-   c                   s   t ƒ  ¡  tƒ | _d S r.   )Úsuperr5   r$   Úconsole_input_reader)r2   r<   ©Ú	__class__r3   r4   r5   K   s    
zWin32Input.__init__úCallable[[], None]zContextManager[None])Úinput_ready_callbackr-   c                 C  s
   t | |ƒS )zj
        Return a context manager that makes this input active in the current
        event loop.
        )r'   )r2   rB   r3   r3   r4   ÚattachO   s    zWin32Input.attachr,   c                 C  s   t | ƒS )z{
        Return a context manager that makes sure that this input is not active
        in the current event loop.
        )r(   r1   r3   r3   r4   ÚdetachV   s    zWin32Input.detachúlist[KeyPress]c                 C  s   t | j ¡ ƒS r.   )Úlistr>   Úreadr1   r3   r3   r4   Ú	read_keys]   s    zWin32Input.read_keysc                 C  s   d S r.   r3   r1   r3   r3   r4   Úflush`   s    zWin32Input.flushÚboolc                 C  s   dS )NFr3   r1   r3   r3   r4   Úclosedc   s    zWin32Input.closedc                 C  s   t ƒ S r.   )r%   r1   r3   r3   r4   r%   g   s    zWin32Input.raw_modec                 C  s   t ƒ S r.   )r&   r1   r3   r3   r4   r&   j   s    zWin32Input.cooked_modeÚintc                 C  s
   t j ¡ S r.   )Úsysr<   Úfilenor1   r3   r3   r4   rN   m   s    zWin32Input.filenoÚstrc                 C  s   dS )Nzwin32-inputr3   r1   r3   r3   r4   Útypeahead_hasht   s    zWin32Input.typeahead_hashc                 C  s   | j  ¡  d S r.   )r>   Úcloser1   r3   r3   r4   rQ   w   s    zWin32Input.closer   c                 C  s   | j jS r.   )r>   r6   r1   r3   r3   r4   r6   z   s    zWin32Input.handle)N)r7   r8   r9   r:   r5   rC   rD   rH   rI   r;   rK   r%   r&   rN   rP   rQ   r6   Ú__classcell__r3   r3   r?   r4   r#   F   s   r#   c                "   @  s¤  e Zd ZdZejejejejej	ej
ejejejejejejejejejejejejejejejejejejejejejej ej!ej"ej#ej$ej%dœ!Z&ej'ej(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<dœZ=dZ>dZ?dZ@dZAdZBd.d
ddœdd„ZCddœdd„ZDddœdd„ZEdddœdd„ZFddddœdd„ZGeHddd œd!d"„ƒZIeHdd
d#œd$d%„ƒZJd&dd'œd(d)„ZKd*dd'œd+d,„ZLd-S )/r$   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)   TrJ   r+   )Úrecognize_paster-   c                 C  sZ   d | _ || _|  tj ¡ r.ttj t	¡ƒ| _
n(t dtjtjB ¡| _ tt | j ¡ƒ| _
d S )NzCONIN$)Ú_fdconrŒ   rM   r<   Úisattyr   r	   Úkernel32ÚGetStdHandler   r6   ÚosÚopenÚO_RDWRÚO_BINARYÚmsvcrtZget_osfhandle)r2   rŒ   r3   r3   r4   r5   Í   s    
zConsoleInputReader.__init__r,   c                 C  s   | j durt | j ¡ dS )zClose fdcon.N)r�   r‘   rQ   r1   r3   r3   r4   rQ   Ú   s    
zConsoleInputReader.closezIterable[KeyPress]c           	   	   #  s0  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 ]„}g }|röt|jtƒrÄ|jtjtjhv rö| |j¡ zt|ƒ}W q¢ tyò   d}Y q¢0 q¢|�rttjd |¡ƒV  |durš|V  qšn
|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 r3   )Ú_insert_key_data)Ú.0Úkeyr1   r3   r4   Ú
<listcomp>ÿ   ó    z+ConsoleInputReader.read.<locals>.<listcomp>Ú )r   r   r   r6   r	   r�   ZReadConsoleInputWr   rF   Ú	_get_keysÚ_merge_paired_surrogatesrŒ   Ú	_is_pasteÚiterÚ
isinstancer™   r   ÚControlJÚControlMÚappendÚdataÚnextÚStopIterationr   ZBracketedPasteÚjoin)	r2   Ú	max_countrG   ZarrtypeÚinput_recordsÚall_keysÚgenÚkr¥   r3   r1   r4   rG   ß   s>    ÿ
ÿþ
zConsoleInputReader.readr   )Ú	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   )r2   r®   r¥   r3   r3   r4   r—     s    z#ConsoleInputReader._insert_key_datar   zArray[INPUT_RECORD]zIterator[KeyPress])rG   rª   r-   c                 c  sx   t |jƒD ]h}|| }|jtv r
t|jt|j ƒ}t|ƒtkrV|jrV|  	|¡E dH  q
t|ƒt
kr
|  |¡E dH  q
dS )zR
        Generator that yields `KeyPress` objects from the input records.
        N)ÚrangeÚvalueZ	EventTyper   ÚgetattrÚEventÚtyper   ÚKeyDownÚ_event_to_key_pressesr   Ú_handle_mouse)r2   rG   rª   ÚiZirÚevr3   r3   r4   r�   +  s    
zConsoleInputReader._get_keysrE   )Úkey_pressesr-   c                 c  s®   d}| D ]–}t |jtƒ }|o6d|j  ko2dkn  }|oTd|j  koPdkn  }|rŽ|r„|j|j  dd¡ d¡}t||ƒ}n|V  d}|r˜|}q|V  q|rª|V  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º   Zbuffered_high_surrogater™   Zis_textZis_high_surrogateZis_low_surrogateZfullcharr3   r3   r4   rž   C  s*    ÿþÿz+ConsoleInputReader._merge_paired_surrogates)Úkeysr-   c                 C  sJ   d}d}| D ],}t |jtƒs$|d7 }|jtjkr|d7 }q|dkoH|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¾   Z
text_countZnewline_countr­   r3   r3   r4   rŸ   d  s    
zConsoleInputReader._is_paster   )r¹   r-   c                 C  sÈ  t |ƒtkr|jsJ ‚d}|j}|jj}| dd¡}|dkr\|j| jv r–t	| j|j dƒ}n:|| j
v rŒ| j
| tjkrzd}t	| j
| |ƒ}n
t	||ƒ}|| j@ s¬|| 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@ �s4|| 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@ �s8|| j@ �rV|�rV|j;dk�rVt	tj<dƒ}|| j@ �sn|| j@ �r’|�r’|j#tjk�r’t	tj=dƒ|gS |�rÀ|| j>@ }|�r¸t	tj=dƒ|gS |gS ng S dS )zU
        For this `KEY_EVENT_RECORD`, return a list of `KeyPress` instances.
        Nzutf-8r»   ú rœ   Ú
ú )?r´   r   rµ   ZControlKeyStateZuCharZUnicodeCharr¼   ZVirtualKeyCodeÚkeycodesr   Úmappingsr   r¢   ÚLEFT_CTRL_PRESSEDÚRIGHT_CTRL_PRESSEDÚSHIFT_PRESSEDÚLeftZControlShiftLeftÚRightZControlShiftRightÚUpZControlShiftUpÚDownZControlShiftDownÚHomeZControlShiftHomeÚEndZControlShiftEndÚInsertZControlShiftInsertÚPageUpZControlShiftPageUpÚPageDownZControlShiftPageDownr¯   r™   ZControlLeftZControlRightZ	ControlUpZControlDownZControlHomeZ
ControlEndZControlInsertÚDeleteZControlDeleteZControlPageUpZControlPageDownZTabZBackTabZ	ShiftLeftZ
ShiftRightZShiftUpZ	ShiftDownZ	ShiftHomeZShiftEndZShiftInsertZShiftDeleteZShiftPageUpZShiftPageDownr¥   ÚControlSpaceÚEscapeÚLEFT_ALT_PRESSED)r2   r¹   ÚresultZcontrol_key_stateZu_charZ
ascii_charÚmappingZmeta_pressedr3   r3   r4   r¶   z  s²    
ÿ
þýûú	÷ÿþýöõþýûúþýû
ú

z(ConsoleInputReader._event_to_key_pressesr   c                 C  s´   |j }|j}d}tj}|t@ r6|dkr.tj}qTtj}n|tkrFtj	}n|t
krTtj}|t@ rbtj}|du r€|dkrzt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   ú;)Z
EventFlagsZButtonStater   ÚNONEÚMOUSE_WHEELEDr   Z	SCROLL_UPZSCROLL_DOWNÚFROM_LEFT_1ST_BUTTON_PRESSEDÚLEFTÚRIGHTMOST_BUTTON_PRESSEDÚRIGHTÚMOUSE_MOVEDZ
MOUSE_MOVEZ
MOUSE_DOWNZMOUSE_UPr¨   r±   rO   ZMousePositionÚXÚYr   r   ZWindowsMouseEvent)r2   r¹   Zevent_flagsZbutton_stateZ
event_typeZbuttonr¥   r3   r3   r4   r·   þ  s4    

üÿz ConsoleInputReader._handle_mouseN)T)Mr7   r8   r9   r:   r   rÒ   rÑ   ZControlAZControlBZControlCZControlDZControlEZControlFZControlGZControlHZControlIr¢   ZControlKZControlLr£   ZControlNZControlOZControlPZControlQZControlRZControlSZControlTZControlUZControlVZControlWZControlXZControlYZControlZZControlBackslashZControlSquareCloseZControlCircumflexZControlUnderscoreZ	BackspacerÃ   rÎ   rÏ   rÌ   rË   rÇ   rÉ   rÈ   rÊ   rÍ   rÐ   ZF1ZF2ZF3ZF4ZF5ZF6ZF7ZF8ZF9ZF10ZF11ZF12rÂ   rÓ   ZRIGHT_ALT_PRESSEDrÆ   rÄ   rÅ   r5   rQ   rG   r—   r�   Ústaticmethodrž   rŸ   r¶   r·   r3   r3   r3   r4   r$      s–   ß'ç>  r$   c                   @  s@   e Zd ZdZddœdd„Zddddœd	d
„Zdd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_eventsr1   r3   r3   r4   r5   >  s    z_Win32Handles.__init__r   rA   )r6   Úcallbackr-   c                   s~   ˆj }|du rtdƒ‚|  ˆ¡ tƒ ‰ˆ | j|< tƒ ‰ˆ| j|< ddœ‡ ‡‡fd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� nt ˆˆd� 0 d S )N©Úloopr   r3   )rã   ræ   Úwaitr3   r4   ÚreadyY  s    z-_Win32Handles.add_win32_handle.<locals>.readyc                    s2   t ˆˆ gƒ} | ˆu r$tj ˆ¡ d S ˆ ˆ¡ d S r.   )r   r	   r�   ZCloseHandleÚcall_soon_threadsafe)rÔ   )r6   ræ   rè   Úremove_eventr3   r4   rç   c  s
    z,_Win32Handles.add_win32_handle.<locals>.waitrå   )r±   Ú
ValueErrorÚremove_win32_handler   rá   r   râ   r   )r2   r6   rã   Zhandle_valuer3   )rã   r6   ræ   rè   rê   rç   r4   Úadd_win32_handleE  s    



z_Win32Handles.add_win32_handlezCallable[[], None] | None)r6   r-   c                 C  sj   |j du rdS z| j |j ¡}W n ty2   Y n0 tj |¡ z| j |j ¡W S  tyd   Y dS 0 dS )zt
        Remove a Win32 handle from the event loop.
        Return either the registered handler or `None`.
        N)r±   râ   ÚpopÚKeyErrorr	   r�   ZSetEventrá   )r2   r6   Úeventr3   r3   r4   rì   p  s    
z!_Win32Handles.remove_win32_handleN)r7   r8   r9   r:   r5   rí   rì   r3   r3   r3   r4   r/   .  s   +r/   rA   zIterator[None])Úinputrã   r-   c              
   c  sz   | j }| j}|jdu rtdƒ‚| |¡}| ||¡ z$dV  W | |¡ |rv| ||¡ n| |¡ |rt| ||¡ 0 dS )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.
    Nrä   ©r0   r6   r±   rë   rì   rí   )rñ   rã   r0   r6   Úprevious_callbackr3   r3   r4   r'   †  s    



ý
r'   )rñ   r-   c              
   c  sZ   | j }| j}|jd u rtdƒ‚| |¡}zd V  W |rV| ||¡ n|rT| ||¡ 0 d S )Nrä   rò   )rñ   r0   r6   ró   r3   r3   r4   r(   £  s    

ÿr(   c                   @  sN   e Zd ZdZddddœdd„Zddœd	d
„Zddœdd„Zdd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`.
    Nz
int | Noner+   )rN   r-   c                 C  s   t tj t¡ƒ| _d S r.   )r   r	   r�   r�   r   r6   )r2   rN   r3   r3   r4   r5   ¿  s    zraw_mode.__init__r,   c                 C  s,   t ƒ }tj | jt|ƒ¡ || _|  ¡  d S r.   )r   r	   r�   ZGetConsoleModer6   r   Úoriginal_modeÚ_patch)r2   rô   r3   r3   r4   Ú	__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   rô   r±   ©r2   ZENABLE_ECHO_INPUTZENABLE_LINE_INPUTZENABLE_PROCESSED_INPUTr3   r3   r4   rõ   Ê  s    ÿþzraw_mode._patchÚobject)Úar-   c                 G  s   t j | j| j¡ d S r.   )r	   r�   rù   r6   rô   )r2   rü   r3   r3   r4   Ú__exit__Ö  s    zraw_mode.__exit__)N)r7   r8   r9   r:   r5   rö   rõ   rý   r3   r3   r3   r4   r%   ´  s
   
r%   c                   @  s   e Zd ZdZd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 r÷   rø   rú   r3   r3   r4   rõ   ã  s    
ÿþzcooked_mode._patchN)r7   r8   r9   r:   rõ   r3   r3   r3   r4   r&   Û  s   r&   )>Ú
__future__r   r‘   rM   Úabcr   Úasyncior   Ú
contextlibr   Úutilsr   Úplatformr•   Úctypesr	   r
   r   Zctypes.wintypesr   r   Útypingr   r   r   r   r   Zprompt_toolkit.eventloopr   Zprompt_toolkit.eventloop.win32r   r   Z(prompt_toolkit.key_binding.key_processorr   Zprompt_toolkit.keysr   Zprompt_toolkit.mouse_eventsr   r   Zprompt_toolkit.win32_typesr   r   r   r   r   Zansi_escape_sequencesr!   Úbaser"   Ú__all__rÙ   rÛ   rÝ   rØ   r*   r#   r$   r/   r'   r(   r%   r&   r3   r3   r3   r4   Ú<module>   sN   9   2X'