o
    Th                     @   s  d Z ddlmZ ddlmZmZmZmZ ddlZddl	Z	ddl
Z
ddlmZmZ e
jdkZ	 e
jdkrPddlZddlmZmZmZmZmZ dd	lmZmZmZ nddlZddlZddlZddlZe
jdkrud
eee ee f fddZ nd
eee ee f fddZ d
eeef fddZ!ded
e"fddZ#ded
e"fddZ$eded
ed fddZ%ded
e"fddZ&ded
efddZ'dS )z
Utility functions surrounding terminal devices & I/O.

Much of this code performs platform-sensitive branching, e.g. Windows support.

This is its own module to abstract away what would otherwise be distracting
logic-flow interruptions.
    )contextmanager)	GeneratorIOOptionalTupleN   )
has_filenoisattywin32)	Structurec_ushortwindllPOINTERbyref)HANDLE_COORD_SMALL_RECTreturnc                  C   s   G dd dt } tjj}tjj}t|_tt| g|_|d}|  }||t	|}|rB|j
j|j
j d }|j
j|j
j d }||fS dS )Nc                   @   s.   e Zd ZdefdefdefdefdefgZdS )z-_pty_size.<locals>.CONSOLE_SCREEN_BUFFER_INFOdwSizedwCursorPositionwAttributessrWindowdwMaximumWindowSizeN)__name__
__module____qualname__r   r   r   _fields_ r   r   C/var/www/html/venv/lib/python3.10/site-packages/invoke/terminals.pyCONSOLE_SCREEN_BUFFER_INFO3   s    r   ir   NN)r   r   kernel32GetStdHandleGetConsoleScreenBufferInfor   restyper   argtypesr   r   RightLeftBottomTop)r   r"   r#   hstdcsbiretsizexsizeyr   r   r   	_pty_size2   s   	r/   c                  C   sb   d} d}t |dd}zttjtj|}t ||\}}||fW S  t j	t
ttfy0   Y | S w )zS
        Suitable for most POSIX platforms.

        .. versionadded:: 1.0
        r    HHr   )structpackfcntlioctlsysstdouttermios
TIOCGWINSZunpackerror	TypeErrorIOErrorAttributeError)sizefmtbufresultrowscolsr   r   r   r/   Q   s   
	c                  C   s   t  \} }| pd|pdfS )z
    Determine current local pseudoterminal dimensions.

    :returns:
        A ``(num_cols, num_rows)`` two-tuple describing PTY size. Defaults to
        ``(80, 24)`` if unable to get a sensible result dynamically.

    .. versionadded:: 1.0
    P      )r/   )rC   rB   r   r   r   pty_sizes   s   

rF   streamc                 C   s"   t | sdS t t|  kS )a  
    Detect if given stdin ``stream`` seems to be in the foreground of a TTY.

    Specifically, compares the current Python process group ID to that of the
    stream's file descriptor to see if they match; if they do not match, it is
    likely that the process has been placed in the background.

    This is used as a test to determine whether we should manipulate an active
    stdin so it runs in a character-buffered mode; touching the terminal in
    this way when the process is backgrounded, causes most shells to pause
    execution.

    .. note::
        Processes that aren't attached to a terminal to begin with, will always
        fail this test, as it starts with "do you have a real ``fileno``?".

    .. versionadded:: 1.0
    F)r   osgetpgrp	tcgetpgrpfileno)rG   r   r   r   stdin_is_foregrounded_tty   s   rL   c                 C   sd   t | }|d |d }}t|t j@ }t|t j@ }| | |t j dv |t j dv f}t|S )N      )r      )r       )r7   	tcgetattrboolECHOICANONVMINVTIMEall)rG   attrslflagsccechoicanon	sentinelsr   r   r   cbreak_already_set   s   
r^   )NNNc                 c   sl    t st| rt| rt| rdV  dS t| }t|  zdV  W t| tj	| dS t| tj	| w )z
    Force local terminal ``stream`` be character, not line, buffered.

    Only applies to Unix-based systems; on Windows this is a no-op.

    .. versionadded:: 1.0
    N)
WINDOWSr	   rL   r^   r7   rQ   tty	setcbreak	tcsetattr	TCSADRAIN)rG   old_settingsr   r   r   character_buffered   s   


&re   input_c                 C   sJ   t | sdS tjdkrt S t| gg g d\}}}t|o#|d | u S )z
    Test ``input_`` to determine whether a read action will succeed.

    :param input_: Input stream object (file-like).

    :returns: ``True`` if a read should succeed, ``False`` otherwise.

    .. versionadded:: 1.0
    Tr
   g        r   )r   r5   platformmsvcrtkbhitselectrR   )rf   reads_r   r   r   ready_for_reading   s   
rm   c                 C   s<   t st| rt| rt| tjd}tt	d|d S dS )a|  
    Query stream ``input_`` to see how many bytes may be readable.

    .. note::
        If we are unable to tell (e.g. if ``input_`` isn't a true file
        descriptor or isn't a valid TTY) we fall back to suggesting reading 1
        byte only.

    :param input: Input stream object (file-like).

    :returns: `int` number of bytes to read.

    .. versionadded:: 1.0
    s     hr   r   )
r_   r	   r   r3   r4   r7   FIONREADintr1   r9   )rf   fionreadr   r   r   bytes_to_read   s   rr   )(__doc__
contextlibr   typingr   r   r   r   rH   rj   r5   utilr   r	   rg   r_   rh   ctypesr   r   r   r   r   ctypes.wintypesr   r   r   r3   r1   r7   r`   rp   r/   rF   rR   rL   r^   re   rm   rr   r   r   r   r   <module>   s>    	



 "