o
    Ďi"                     @   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  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   X/home/jeff/fluffinator/venv/lib/python3.10/site-packages/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}|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   hhs   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   4   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
   u   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 dimensions from tput(1).r   Ntputcols)stderrlines)
subprocessr   Decodecheck_outputSTDOUTr;   )rA   outputr>   rowsr   r   r   r   z   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   rQ   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   rB   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_keyrg   old_settingscprev_cr   ttyrf   r   _GetRawKeyPosix   sR   




$z0_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix)r}   r   )r~   r   r|   r   rN      s   5rN   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.
    rR   rS   rT   rU   rV   rW   rX   rY   )r`   PKrb   IQGOc                      s   t   S rL   )r   rB   getchr   msvcrtr   r   rg      s   zI_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindows.<locals>._GetKeyCharri   N)rj   rk   rl   _WINDOWS_CSI_1_WINDOWS_CSI_2ru   rv   rw   )windows_to_keyrg   rz   r   r   r   _GetRawKeyWindows   s   
z4_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindowsr   )r   r   r   r   rO      s   rO   )__doc__
__future__r   r   r   r   rj   fire.consoler   r   r   r	   r
   r   rq   rv   rw   r   r   rQ   rN   rO   r   r   r   r   <module>   s(   (?