
                              S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr  SSKJ	r	  S r
S	 rS
 rS rS rSrSrSrSrSrS rS rS rS rg)z*OS specific console_attr helper functions.    )absolute_import)division)unicode_literalsN)encoding)	platformsc                      Sn [         [        [        [        4 H  n U" 5       n U (       a    OM     U =(       d    S$ !    M(  = f)zGets the terminal x and y dimensions in characters.

_GetTermSize*() helper functions taken from:
  http://stackoverflow.com/questions/263890/

Returns:
  (columns, lines): A tuple containing the terminal x and y dimensions.
N)P      )_GetTermSizePosix_GetTermSizeWindows_GetTermSizeEnvironment_GetTermSizeTput)xyget_terminal_sizes     2lib/googlecloudsdk/core/console/console_attr_os.pyGetTermSizer      sP     "-/3,.b	 
. 
x
s	   <Ac                    ^^^ SSK mSSKmSSKmUUU4S jn U " S5      =(       d    U " S5      =(       d    U " S5      nU(       d^  Sn [        R                  " [        R
                  " 5       [        R                  5      nU " U5      nUb  [        R                  " U5        U$ U$ !   Sn N#= f! Ub  [        R                  " U5        f f = f)z.Returns the Posix terminal x and y dimensions.r   Nc                    >  TR                  STR                  U TR                  S5      5      nU(       a
  US   US   4$ S$ !    g= f)zReturns the terminal (x,y) size for fd.

Args:
  fd: The terminal file descriptor.

Returns:
  The terminal (x,y) size for fd or None on error.
s   hhjunk   r   N)unpackioctl
TIOCGWINSZ)fdrcfcntlstructtermioss     r   _GetXY!_GetTermSizePosix.<locals>._GetXY>   sP     ==B0B0BF KLb!beRU^+t+s   =A A Ar      )r   r   r   osopenctermidO_RDONLYclose)r   r   r   r   r   r   s      @@@r   r   r   5   s     " ay*F1I*"		B772::<-b":b 

	))b	
 
s   A B% %B+)B. .C
c                     SSK n SSKJn  SSKJn  UR                  R                  S5      nU" S5      nUR                  R                  X45      (       d  gU R                  SUR                  5      u  pVpxn	pppX-
  S-   nX-
  S-   nUU4$ )	z0Returns the Windows terminal x and y dimensions.r   N)create_string_buffer)windlli   s   hhhhHhhhhhhr   )	r   ctypesr(   r)   kernel32GetStdHandleGetConsoleScreenBufferInfor   raw)r   r(   r)   hcsbiunused_bufxunused_bufyunused_curxunused_curyunused_wattrlefttoprightbottomunused_maxxunused_maxyxys                     r   r   r   ]   s     ) oo""3'!	b	!$		3	3A	<	<  &}}^TXXF;[|elQ!lQ!
Q-    c                  r    [        [        R                  S   5      [        [        R                  S   5      4$ )z=Returns the terminal x and y dimensions from the environment.COLUMNSLINES)intr"   environ r?   r   r   r   v   s)    
bjj#
$c"**W*=&>	??r?   c                      SSK n [        R                  " U R                  SS/U R                  S95      n[        U5      n[        R                  " U R                  SS/U R                  S95      n[        U5      nX#4$ )z5Returns the terminal x and y dimemsions from tput(1).r   Ntputcols)stderrlines)
subprocessr   Decodecheck_outputSTDOUTrC   )rK   outputrH   rowss       r   r   r   {   s    ??:22FF3C:D:K:K 3 M N&	V$??:22FG3D:D:K:K 3 M N&	V$
r?       àc                  N    [         [        4 H  n  U " 5       s  $    S $ !    M  = f)zReturns a function that reads one keypress from stdin with no echo.

Returns:
  A function that reads one keypress from stdin with no echo or a function
  that always returns None if stdin does not support it.
c                      g NrE   rE   r?   r   <lambda>#GetRawKeyFunction.<locals>.<lambda>   s    r?   )_GetRawKeyFunctionPosix_GetRawKeyFunctionWindows)get_raw_key_functions    r   GetRawKeyFunctionr^      s7      78:!##: 

s   $c                  (   ^^ SSK mSSKmUU4S jn U $ )z+_GetRawKeyFunction helper using Posix APIs.r   Nc                    >^ SSSSSSSSSSSS	.n [         R                  R                  5         [         R                  R	                  5       mU4S
 jnTR                  T5      n TR                  T5        U" 5       nU[        :X  a  U" 5       n U[        :X  a  UTR                  TTR                  U5        $ UR                  5       (       a  OUnU" 5       nUS:X  a  UnOMS  U R                  US5      TR                  TTR                  U5        $  TR                  TTR                  U5        U[        [        4;   a  S$ U$ !   Sn N7= f! TR                  TTR                  U5        f = f)zReads and returns one keypress from stdin, no echo, using Posix APIs.

Returns:
  The key name, None for EOF, <*> for function keys, otherwise a
  character.

<UP-ARROW><DOWN-ARROW><LEFT-ARROW><RIGHT-ARROW>	<PAGE-UP><PAGE-DOWN><HOME><END>)ABDC56HFMSTc                  Z   > [         R                  " [        R                  " T S5      5      $ )Nr   )r   rL   r"   read)r   s   r   _GetKeyCharE_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix.<locals>._GetKeyChar   s    __RWWR^,,r?   ~ N)sysstdoutflushstdinfileno	tcgetattrsetraw	_ANSI_CSI	tcsetattr	TCSADRAINisalphaget
_CONTROL_D
_CONTROL_Z)ansi_to_keyrv   old_settingscprev_cr   r   ttys        @r   _GetRawKeyPosix0_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix   si    K  JJ				B- $$R(L=	jjn
-a	
iM)^ G--|< YY[[&m!#XA  q"% G--|<! 
  G--|<Z0047a7	
aG--|<s$   %5E 8;E E E	E E+)r   r   )r   r   r   s    @@r   r[   r[      s     38j 
r?   c                     ^ SSK mU4S jn U $ )z-_GetRawKeyFunction helper using Windows APIs.r   Nc            	         > SSSSSSSSS	.n [         R                  R                  5         U4S
 jnU" 5       nU[        [        4;   a  U R                  U" 5       S5      $ U[        [        4;   a  S$ U$ )zReads and returns one keypress from stdin, no echo, using Windows APIs.

Returns:
  The key name, None for EOF, <*> for function keys, otherwise a
  character.
ra   rb   rc   rd   re   rf   rg   rh   )ro   PKrq   IQGOc                  L   > [         R                  " T R                  5       5      $ rX   )r   rL   getchmsvcrts   r   rv   I_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindows.<locals>._GetKeyChar   s    __V\\^,,r?   ry   N)rz   r{   r|   _WINDOWS_CSI_1_WINDOWS_CSI_2r   r   r   )windows_to_keyrv   r   r   s      r   _GetRawKeyWindows4_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindows   s}     	N JJ- 	A^^,,r22Z0047a7r?   r   )r   r   s    @r   r\   r\      s     8> 
r?   c                     [         R                  R                  5       [         R                  R                  :w  a  g SSKn SnU R
                  R                  R                  S5      nU R                  R                  5       nU R
                  R                  R                  X R                  U5      5      (       a8  U R
                  R                  R                  X#R                  U-  5      (       a  gg! [        [        4 a     gf = f)zAttempts to enable virtual terminal processing on Windows.

Returns:
  bool: True if ANSI support is now active; False otherwise.
Fr   N   iT)r   OperatingSystemCurrentWINDOWSr+   r)   r,   r-   wintypesDWORDGetConsoleModebyrefSetConsoleModevalueOSErrorAttributeError)r+   "enable_virtual_terminal_processingr0   old_modes       r   ForceEnableAnsir     s     &&(I,E,E,M,MM	)/&++C0A$$&H}},,QX0FGG				.	.
^^@@
B 
B 
 >	" 			s   B5C4 4DD)__doc__
__future__r   r   r   r"   rz   googlecloudsdk.core.utilr   r   r   r   r   r   r   r   r   r   r   r   r^   r[   r\   r   rE   r?   r   <module>r      sk     1 &  ' 	 
 - .0%P2@
	 	

"<~$Nr?   