
    Ri                        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    rd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    rd
eee         ee         f         fdZ nd
eee         ee         f         fdZ d
eeef         fdZ!ded
e"fdZ#ded
e"fdZ$eded
ed         fd            Z%ded
e"fdZ&ded
ef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                      G d dt                     } t          j        j        }t          j        j        }t
          |_        t
          t          |           g|_         |d          } |             } ||t          |                    }|r<|j
        j        |j
        j        z
  dz   }|j
        j        |j
        j        z
  dz   }||fS dS )Nc                   0    e Zd ZdefdefdefdefdefgZdS )-_pty_size.<locals>.CONSOLE_SCREEN_BUFFER_INFOdwSizedwCursorPositionwAttributessrWindowdwMaximumWindowSizeN)__name__
__module____qualname__r   r   r   _fields_     H/srv/django_bis/venv311/lib/python3.11/site-packages/invoke/terminals.pyCONSOLE_SCREEN_BUFFER_INFOr   3   s=        6"#V,)[)&/HHHr"   r$   ir   NN)r   r   kernel32GetStdHandleGetConsoleScreenBufferInfor   restyper   argtypesr   r   RightLeftBottomTop)r$   r'   r(   hstdcsbiretsizexsizeys           r#   	_pty_sizer4   2   s    	 	 	 	 	 	 	 	 3%+_%O"%.///
"+
 |C  ))++((uT{{;; 	 M'$-*<<q@EM(4=+<<q@E%<<r"   c                     d} d}t          j        |dddd          }	 t          j        t          j        t          j        |          }t          j        ||          ^}}}||fS # t           j	        t          t          t          f$ r Y nw xY w| S )zS
        Suitable for most POSIX platforms.

        .. versionadded:: 1.0
        r%   HHHHr   )structpackfcntlioctlsysstdouttermios
TIOCGWINSZunpackerror	TypeErrorIOErrorAttributeError)sizefmtbufresultrowscols_s          r#   r4   r4   Q   s      k#q!Q**	[W-?EEF $]377ND$$< i.A 	 	 	D	s   AA$ $"B	B	c                  4    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      )r4   )rI   rH   s     r#   pty_sizerN   t   s$     JD$JB
##r"   streamc                     t          |           sdS t          j                    t          j        |                                           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)rO   s    r#   stdin_is_foregrounded_ttyrU      s<    & f u:<<2<8888r"   c                 0   t          j        |           }|d         |d         }}t          |t           j        z            }t          |t           j        z            }| | |t           j                 dv |t           j                 dv f}t          |          S )N      )r      )r       )r=   	tcgetattrboolECHOICANONVMINVTIMEall)rO   attrslflagsccechoicanon	sentinelss          r#   cbreak_already_setrh      s     f%%Eq58BF%&&D&7>)**F
 

7<L(
7=\)	I y>>r"   )NNNc              #   b  K   t           s-t          |           rt          |           rt          |           rdV  dS t	          j        |           }t          j        |            	 dV  t	          j        | t          j	        |           dS # t	          j        | t          j	        |           w xY 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
   rU   rh   r=   r[   tty	setcbreak	tcsetattr	TCSADRAIN)rO   old_settingss     r#   character_bufferedrp      s       	Gf~~G )00G f%%	G 	(00f	GEEEfg&7FFFFFGfg&7FFFFs   &B "B.input_c                     t          |           sdS t          j        dk    rt          j                    S t          j        | 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	   r;   platformmsvcrtkbhitselectr\   )rq   readsrJ   s      r#   ready_for_readingrx      sk     f t
|w|~~mVHb"c::q!E0eAh&0111r"   c                     t           sft          |           rWt          |           rHt          j        | t
          j        d          }t          t          j	        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   )
rj   r
   r	   r9   r:   r=   FIONREADintr7   r?   )rq   fionreads     r#   bytes_to_readr~      sb    $  4vf~~ 4*V*<*< 4;vw'7??6=h//23331r"   )(__doc__
contextlibr   typingr   r   r   r   rQ   rv   r;   utilr	   r
   rs   rj   rt   ctypesr   r   r   r   r   ctypes.wintypesr   r   r   r9   r7   r=   rk   r|   r4   rN   r\   rU   rh   rp   rx   r~   r!   r"   r#   <module>r      s    & % % % % % 1 1 1 1 1 1 1 1 1 1 1 1 				  



 % $ $ $ $ $ $ $ ,'
! <7MMM              <;;;;;;;;;;LLLMMMNNNJJJ <7 uXc]HSM9:          > uXc]HSM9:        F$%S/ $ $ $ $9b 9T 9 9 9 90r d    , GG G G G G42b 2T 2 2 2 2."       r"   