a
    lc@                     @   s   d dl Z e jdksJ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZmZmZ dgZG dd deeZdS )    Nwin32)contextmanager)windll)HANDLE)CallableContextManagerIteratorList)create_win32_event   )KeyPressDummyContext   )	PipeInput)Vt100Parser)_Win32InputBaseattach_win32_inputdetach_win32_inputWin32PipeInputc                       s*  e Zd ZdZdZedd fddZeee	d  ddd	Z
eedd
dZedddZeedddZeg df ed dddZed dddZee dddZee dddZeddddZeddddZed ddd Zed dd!d"Zddd#d$Zedd%d&Z   Z!S )'r   a  
    This is an input pipe that works on Windows.
    Text or bytes can be feed into the pipe, and key strokes can be read from
    the pipe. This is useful if we want to send the input programmatically into
    the application. Mostly useful for unit testing.

    Notice that even though it's Windows, we use vt100 escape sequences over
    the pipe.

    Usage::

        input = Win32PipeInput()
        input.send_text('inputdata')
    r   N)_eventreturnc                    sN   t    t  _d _g  _t fdd _ j j	d7  _	 jj	 _	d S )NFc                    s    j | S N)_bufferappend)keyself [/var/www/html/django/DPS/env/lib/python3.9/site-packages/prompt_toolkit/input/win32_pipe.py<lambda>4       z)Win32PipeInput.__init__.<locals>.<lambda>r   )
super__init__r
   r   _closedr   r   vt100_parser	__class___id)r   r   r&   r   r   r#   '   s    
zWin32PipeInput.__init__)r   c              	   c   s6   t  }zt|dV  W tj| ntj| 0 d S )Nr   )r
   r   r   kernel32ZCloseHandle)clseventr   r   r   create:   s    zWin32PipeInput.createc                 C   s   | j S r   )r$   r   r   r   r   closedC   s    zWin32PipeInput.closedc                 C   s   t dS )zE
        The windows pipe doesn't depend on the file handle.
        N)NotImplementedErrorr   r   r   r   filenoG   s    zWin32PipeInput.filenoc                 C   s   | j S )z<The handle used for registering this pipe in the event loop.r)   r   r   r   r   handleM   s    zWin32PipeInput.handle)input_ready_callbackr   c                 C   s
   t | |S )zj
        Return a context manager that makes this input active in the current
        event loop.
        )r   )r   r2   r   r   r   attachR   s    zWin32PipeInput.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   r   r   r   r   detachY   s    zWin32PipeInput.detachc                 C   s$   | j }g | _ | js tj| j |S )zRead list of KeyPress.)r   r$   r   r*   Z
ResetEventr   r   resultr   r   r   	read_keys`   s
    zWin32PipeInput.read_keysc                 C   s   | j   | j}g | _|S )zc
        Flush pending keys and return them.
        (Used for flushing the 'escape' key.)
        )r%   flushr   r5   r   r   r   
flush_keysn   s    
zWin32PipeInput.flush_keys)datar   c                 C   s   |  |dd dS )zSend bytes to the input.zutf-8ignoreN)	send_textdecode)r   r:   r   r   r   
send_bytes|   s    zWin32PipeInput.send_bytes)textr   c                 C   s,   | j rtd| j| tj| j dS )zSend text to the input.z$Attempt to write into a closed pipe.N)r$   
ValueErrorr%   feedr   r*   SetEventr   )r   r?   r   r   r   r<      s    zWin32PipeInput.send_textc                 C   s   t  S r   r   r   r   r   r   raw_mode   s    zWin32PipeInput.raw_modec                 C   s   t  S r   r   r   r   r   r   cooked_mode   s    zWin32PipeInput.cooked_modec                 C   s   d| _ tj| j dS )zClose write-end of the pipe.TN)r$   r   r*   rB   r   r   r   r   r   close   s    zWin32PipeInput.closec                 C   s   d| j  S )z@
        This needs to be unique for every `PipeInput`.
        zpipe-input-)r'   r   r   r   r   typeahead_hash   s    zWin32PipeInput.typeahead_hash)"__name__
__module____qualname____doc__r'   r   r#   classmethodr   r   r-   propertyboolr.   intr0   r1   r   r   r3   r4   r	   r   r7   r9   bytesr>   strr<   rC   rD   rE   rF   __classcell__r   r   r(   r   r      s*   )sysplatform
contextlibr   ctypesr   Zctypes.wintypesr   typingr   r   r   r	   Zprompt_toolkit.eventloop.win32r
   Zkey_bindingr   utilsr   baser   r%   r   r   r   r   r   __all__r   r   r   r   r   <module>   s   