o
                          @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddlm	Z	 dd	 Z
d
d Zdd Zdd Zdd ZdZdZdZdZdZdd Zdd Zdd Zdd ZdS )z*OS specific console_attr helper functions.    )absolute_import)division)unicode_literalsN)encoding)	platformsc                  C   s<   d} t tttfD ]}z
| } | rW  nW q   Y q| pdS )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_size r   H/tmp/google-cloud-sdk/lib/googlecloudsdk/core/console/console_attr_os.pyGetTermSize   s   	r   c               	      s   ddl  ddlddl fdd} | dp| dp| d}|sRd}z"ztt tj}| |}W n   d}Y W |durEt| |S |durQt| w w |S )z.Returns the Posix terminal x and y dimensions.r   Nc                    sB   z d | jd}|r|d |d fW S dW S    Y dS )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termiosr   r   _GetXY>   s
   	z!_GetTermSizePosix.<locals>._GetXYr      )r   r   r   osopenctermidO_RDONLYclose)r   r   r   r   r   r   r	   5   s(   
r	   c                  C   s   ddl } ddlm} ddlm} |jd}|d}|j||s#dS | d|j\}}}}}	}
}}}}}||
 d }|| d }||fS )	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yr   r   r   r
   ]   s   r
   c                   C   s   t tjd t tjd fS )z=Returns the terminal x and y dimensions from the environment.COLUMNSLINES)intr   environr   r   r   r   r   v   s   r   c                  C   sT   ddl } t| jddg| jd}t|}t| jddg| jd}t|}||fS )z5Returns the terminal x and y dimemsions from tput(1).r   Ntputcols)stderrlines)
subprocessr   Decodecheck_outputSTDOUTr=   )rC   outputr@   rowsr   r   r   r   {   s   r       àc                  C   s,   t tfD ]} z|  W   S    Y qdd S )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                   S   s   d S Nr   r   r   r   r   <lambda>   s    z#GetRawKeyFunction.<locals>.<lambda>)_GetRawKeyFunctionPosix_GetRawKeyFunctionWindows)get_raw_key_functionr   r   r   GetRawKeyFunction   s   rS   c                     s"   ddl ddl  fdd} | S )z+_GetRawKeyFunction helper using Posix APIs.r   Nc                     s  dddddddddddd	} t j  t j   fd
d} }zUzD  | }|tkrg| }	 |tkrE|W W  j	| S |
 rJn|}| }|dkrV|}nq5| |dW W  j	| S W n   d}Y W  j	| n	 j	| w |ttfv rdS |S )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                      s   t t dS )Nr   )r   rD   r   readr   r   r   r   _GetKeyChar   s   zE_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix.<locals>._GetKeyCharT~ N)sysstdoutflushstdinfileno	tcgetattrsetraw	_ANSI_CSI	tcsetattr	TCSADRAINisalphaget
_CONTROL_D
_CONTROL_Z)ansi_to_keyri   old_settingscprev_cr   ttyrh   r   _GetRawKeyPosix   sR   




$z0_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix)r   r   )r   r   r~   r   rP      s   5rP   c                     s   ddl   fdd} | S )z-_GetRawKeyFunction helper using Windows APIs.r   Nc               	      s`   ddddddddd	} t j   fd
d}| }|ttfv r&| | dS |ttfv r.dS |S )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.
    rT   rU   rV   rW   rX   rY   rZ   r[   )rb   PKrd   IQGOc                      s   t   S rN   )r   rD   getchr   msvcrtr   r   ri      s   zI_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindows.<locals>._GetKeyCharrk   N)rl   rm   rn   _WINDOWS_CSI_1_WINDOWS_CSI_2rw   rx   ry   )windows_to_keyri   r|   r   r   r   _GetRawKeyWindows   s   
z4_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindowsr   )r   r   r   r   rQ      s   rQ   c               	   C   s   t j t jjkrdS z1ddl} d}| jjd}| j	 }| jj
|| |r7| jj||j|B r:W dS W dS W dS  ttfyH   Y dS w )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_processingr,   old_moder   r   r   ForceEnableAnsi  s(   

r   )__doc__
__future__r   r   r   r   rl   googlecloudsdk.core.utilr   r   r   r	   r
   r   r   rs   rx   ry   r   r   rS   rP   rQ   r   r   r   r   r   <module>   s,   (?'