
    {Pj"                         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 Z	d Z
d	 Zd
 Zd ZdZdZdZdZdZd Zd Zd Zy)z*OS specific console_attr helper functions.    )absolute_import)division)unicode_literalsN)encodingc                  n    d} t         t        t        t        fD ]  }	  |       } | r n | xs dS #  Y xY w)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     T/root/$HERMES_HOME/venv/lib/python3.12/site-packages/fire/console/console_attr_os.pyGetTermSizer      sU     "-/3,. 	b	 
	 
x
s   	04c                  n   ddl ddlddlfd}  | d      xs  | d      xs  | d      }|sZd}	 t        j                  t        j
                         t        j                        } | |      }|t        j                  |       |S |S #  d}Y !xY w# |t        j                  |       w w xY w)z.Returns the Posix terminal x and y dimensions.r   Nc                     	 j                  dj                  | j                  d            }|r
|d   |d   fS dS #  Y yxY w)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termioss     r   _GetXYz!_GetTermSizePosix.<locals>._GetXY=   sP     ==B0B0BG LMb!beRU^+t+s   8> > Ar      )r   r   r   osopenctermidO_RDONLYclose)r   r   r   r   r   r   s      @@@r   r
   r
   4   s     " ay*F1I*"		B772::<-b":b 

	))b	
 
s   >B BB B4c                     ddl } ddlm} ddlm} |j                  j                  d      } |d      }|j                  j                  ||      sy| j                  d|j                        \  }}}}}	}
}}}}}||
z
  dz   }||z
  dz   }||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ys                     r   r   r   \   s     ) oo""3'!	b	!$		3	3At	<  &}}^TXXF;[+|eVdlQ!slQ!
Q-    c                  r    t        t        j                  d         t        t        j                  d         fS )z=Returns the terminal x and y dimensions from the environment.COLUMNSLINES)intr   environ r<   r   r   r   u   s)    
bjj#
$c"**W*=&>	??r<   c                     ddl } t        j                  | j                  ddg| j                              }t        |      }t        j                  | j                  ddg| j                              }t        |      }||fS )z5Returns the terminal x and y dimensions from tput(1).r   Ntputcols)stderrlines)
subprocessr   Decodecheck_outputSTDOUTr@   )rH   outputrE   rowss       r   r   r   z   s    ??:22FF3C:D:K:K 3 M N&	V$??:22FG3D:D:K:K 3 M N&	V$
r<       àc                  J    t         t        fD ]  } 	  |        c S  d S #  Y xY w)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                       y NrB   rB   r<   r   <lambda>z#GetRawKeyFunction.<locals>.<lambda>   s    r<   )_GetRawKeyFunctionPosix_GetRawKeyFunctionWindows)get_raw_key_functions    r   GetRawKeyFunctionrZ      s<      78: !## 

s   "c                  &    ddl ddlfd} | S )z+_GetRawKeyFunction helper using Posix APIs.r   Nc                     dddddddddddd	} t         j                  j                          t         j                  j	                         fd
}j                        }	 j                          |       }|t        k(  r |       }	 |t        k(  r|j                  j                  |       S |j                         rn|} |       }|dk(  r|}nK| j                  |d      j                  j                  |       S 	 j                  j                  |       |t        t        fv rdS |S #  d}Y 5xY w# j                  j                  |       w xY w)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                  V    t        j                  t        j                   d            S )Nr   )r   rI   r   read)r   s   r   _GetKeyCharzE_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_keyrr   old_settingscprev_cr   r   ttys        @r   _GetRawKeyPosixz0_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix   sg    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$   $3D7 54D7 E  7D=;E   E)r   r   )r   r   r   s    @@r   rW   rW      s     38j 
r<   c                      ddl fd} | S )z-_GetRawKeyFunction helper using Windows APIs.r   Nc            	          ddddddddd	} t         j                  j                          fd
} |       }|t        t        fv r| j                   |       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.
    r]   r^   r_   r`   ra   rb   rc   rd   )rk   PKrm   IQGOc                  J    t        j                   j                               S rU   )r   rI   getchmsvcrts   r   rr   zI_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindows.<locals>._GetKeyChar   s    __V\\^,,r<   rt   N)ru   rv   rw   _WINDOWS_CSI_1_WINDOWS_CSI_2r   r   r   )windows_to_keyrr   r   r   s      r   _GetRawKeyWindowsz4_GetRawKeyFunctionWindows.<locals>._GetRawKeyWindows   s}     	N JJ- 	A^^,,r22Z0047a7r<   r   )r   r   s    @r   rX   rX      s     8> 
r<   )__doc__
__future__r   r   r   r   ru   fire.consoler   r   r
   r   r   r   r|   r   r   r   r   rZ   rW   rX   rB   r<   r   <module>r      sb     1 &  ' 	 
 !0%P2@
	 	

"<~$r<   