
    kh                        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'y)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        |            }|rd|j                  j                  |j                  j                  z
  dz   }|j                  j                  |j                  j                  z
  dz   }||fS y)Nc                   .    e Zd ZdefdefdefdefdefgZy)-_pty_size.<locals>.CONSOLE_SCREEN_BUFFER_INFOdwSizedwCursorPositionwAttributessrWindowdwMaximumWindowSizeN)__name__
__module____qualname__r   r   r   _fields_     b/var/www/pru.catia.catastroantioquia-mas.com/tasa/lib/python3.12/site-packages/invoke/terminals.pyCONSOLE_SCREEN_BUFFER_INFOr   3   s1    6"#V,)[)&/Hr"   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3%+__%O%O"%.//
"+
 C )+(uT{;MM''$--*<*<<q@EMM((4==+<+<<q@E%<r"   c                  .   d} d}t        j                  |dd      }	 t        j                  t        j
                  t        j                  |      }t        j                  ||      \  }}||fS # t         j                  t        t        t        f$ r Y | S w xY 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colss         r#   r4   r4   Q   s     kk#q!$	[[W-?-?EF  sF3JD$$< i.A 		s   AA- -#BBc                  4    t               \  } }| xs d|xs 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_sizerM   s   s"     JD$JB
##r"   streamc                     t        |       syt        j                         t        j                  | 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)rN   s    r#   stdin_is_foregrounded_ttyrT      s/    & f::<2<<888r"   c                 "   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)rN   attrslflagsccechoicanon	sentinelss          r#   cbreak_already_setrg      s     f%Eq58BF%&D&7>>)*F
 

7<<L(
7==\)	I y>r"   )NNNc              #   `  K   t         s!t        |       rt        |       rt        |       rd yt	        j
                  |       }t        j                  |        	 d t	        j                  | t        j                  |       y# t	        j                  | t        j                  |       w xY w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
   rT   rg   r=   rZ   tty	setcbreak	tcsetattr	TCSADRAIN)rN   old_settingss     r#   character_bufferedro      sz      	f~(0f%((0f	Gfg&7&7FGfg&7&7Fs   AB.B &B.'B++B.input_c                     t        |       syt        j                  dk(  rt        j                         S t        j
                  | gg g d      \  }}}t        |xr |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[   )rp   reads_s      r#   ready_for_readingrx      s\     f
||w||~mmVHb"c:q!E0eAh&011r"   c                     t         s]t        |       rRt        |       rGt        j                  | t
        j                  d      }t        t        j                  d|      d         S y)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   )
ri   r
   r	   r9   r:   r=   FIONREADintr7   r?   )rp   fionreads     r#   bytes_to_readr~      sJ    $ vf~*V*<;;vw'7'7?6==h/233r"   )(__doc__
contextlibr   typingr   r   r   r   rP   ru   r;   utilr	   r
   rr   ri   rs   ctypesr   r   r   r   r   ctypes.wintypesr   r   r   r9   r7   r=   rj   r|   r4   rM   r[   rT   rg   ro   rx   r~   r!   r"   r#   <module>r      s@   & 1 1 	  
 % ,,'
! <<7  <; <<7 uXc]HSM9:  >uXc]HSM9: D$%S/ $9b 9T 90r d , GG G G42b 2T 2."  r"   