U
    ,c                     @   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 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S )z*OS specific console_attr helper functions.    )absolute_import)division)unicode_literalsN)encodingc                  C   s@   d} t tttfD ]&}z| } | r&W  q8W q   Y qX 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   @/tmp/pip-unpacked-wheel-1xt1w7un/fire/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}|sd}z4ztt tj}| |}W n   d}Y nX W 5 |dk	rt| X |S )z.Returns the Posix terminal x and y dimensions.r   Nc                    sF   z2 d | jd}|r.|d |d fndW S    Y dS X 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   hhZjunk   r   N)unpackZioctl
TIOCGWINSZ)fdrcfcntlstructtermiosr   r   _GetXY@   s
    	z!_GetTermSizePosix.<locals>._GetXYr      )r   r   r   oscloseopenctermidO_RDONLY)r   r   r   r   r   r   r   7   s    r   c                  C   s   ddl } ddlm} ddlm} |jd}|d}|j||sFd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"   kernel32ZGetStdHandleGetConsoleScreenBufferInfor   raw)r   r!   r"   hcsbiZunused_bufxZunused_bufyZunused_curxZunused_curyZunused_wattrlefttoprightZbottomZunused_maxxZ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
   x   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   NZtputcols)stderrlines)
subprocessr   Decodecheck_outputSTDOUTr1   )r6   outputr3   Zrowsr   r   r   r   }   s    r       àc                  C   s0   t tfD ]} z|  W   S    Y qX 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>       z#GetRawKeyFunction.<locals>.<lambda>)_GetRawKeyFunctionPosix_GetRawKeyFunctionWindows)Zget_raw_key_functionr   r   r   GetRawKeyFunction   s    rE   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} }zzj  | }|t	kr| }|t	krz|W W PS |
 rq|}| }|dkrh|}qqh| |dW W S W n   d}Y nX W 5  j| X |ttfk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   r7   r   readr   r   r   r   _GetKeyChar   s    zE_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix.<locals>._GetKeyChar~ N)sysstdoutflushstdinfileno	tcgetattr	tcsetattr	TCSADRAINZsetraw	_ANSI_CSIisalphaget
_CONTROL_D
_CONTROL_Z)Zansi_to_keyr[   Zold_settingscZprev_cr   ttyrZ   r   _GetRawKeyPosix   sF    




z0_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix)rm   r   )rn   r   rl   r   rC      s    5rC   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krL| | dS |ttfk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.
    rF   rG   rH   rI   rJ   rK   rL   rM   )rT   PKrV   IQGOc                      s   t   S r@   )r   r7   Zgetchr   msvcrtr   r   r[      s    zI_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindows.<locals>._GetKeyCharr]   N)r^   r_   r`   _WINDOWS_CSI_1_WINDOWS_CSI_2rh   ri   rj   )Zwindows_to_keyr[   rk   ru   r   r   _GetRawKeyWindows   s    
z4_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindowsru   )ry   r   ru   r   rD      s    rD   )__doc__
__future__r   r   r   r   r^   Zfire.consoler   r   r   r	   r
   r   rf   ri   rj   rw   rx   rE   rC   rD   r   r   r   r   <module>   s&   (?