
    i"                         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dS )z*OS specific console_attr helper functions.    )absolute_import)division)unicode_literalsN)encodingc                  t    d} t           t          t          t          fD ]}	  |            } | r n#  Y xY w| 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_sizes     c/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fire/console/console_attr_os.pyGetTermSizer      sd     "-/3,. 	 	b	 
d	xs   /3c                  n   ddl ddlddlfd}  | d          p | d          p
 | d          }|s{d}	 t          j        t          j                    t          j                  } | |          }n	#  d}Y nxY w|t          j        |           n# |t          j        |           w w xY w|S )z.Returns the Posix terminal x and y dimensions.r   Nc                     	                      d                    | j        d                    }|r|d         |d         fndS #  Y dS 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=   s[     ==B0BG L LMMb!+beRU^^t+TTs   AA Ar      )r   r   r   osopenctermidO_RDONLYclose)r   r   r   r   r   r   s      @@@r   r
   r
   4   s     ,,,---...      " vayy*FF1II*"	 		B72:<<--b6"::bbbbb	
 

 
	)s   ;A9 8B 9A?=B B2c                     ddl } ddlm} ddlm} |j                            d          } |d          }|j                            ||          sdS |                     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     ---)))))) o""3''!		b	!	!$		3	3At	<	< 4  &}}^TXFF;[+|eVdlQ!slQ!
Q-    c                  ~    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+    
bj#
$
$c"*W*=&>&>	??r<   c                     ddl } t          j        |                     ddg| j                            }t          |          }t          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 3 M M N N&	V$?:22FG3D:D:K 3 M M N N&	V$
r<       àc                  P    t           t          fD ]} 	  |             c S #  Y xY w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                      d S NrB   rB   r<   r   <lambda>z#GetRawKeyFunction.<locals>.<lambda>   s     r<   )_GetRawKeyFunctionPosix_GetRawKeyFunctionWindows)get_raw_key_functions    r   GetRawKeyFunctionrZ      sL      78:  !!#####
d	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                                         t           j                                        fd
}                              }	                                 |            }|t          k    r |            }	 |t          k    r|                    j	        |           S |
                                rn|} |            }|dk    r|}nT|                     |d                              j	        |           S n	#  d}Y nxY w                    j	        |           n!#                     j	        |           w xY w|t          t          fv rdn|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                  R    t          j        t          j         d                    S )Nr   )r   rI   r   read)r   s   r   _GetKeyCharzE_GetRawKeyFunctionPosix.<locals>._GetRawKeyPosix.<locals>._GetKeyChar   s    _RWR^^,,,r<   T~ 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   s     K  J					B- - - - - $$R((L=	jjnnn
+--a	
iKMM		)^^ G-|<<<< YY[[ &kmm!#XXA		 q"%% G-|<<<<! 

aaaG-|<<<<gG-|<<<<Z00044a7s&   (AD$ A D$ #E
 $D*(E
 
E()r   r   )r   r   r   s    @@r   rW   rW      s?     ***...38 38 38 38 38 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                                         fd
} |            }|t          t          fv r|                      |            d          S |t          t          fv rdn|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                  P    t          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 J- - - - - 	A^^,,,r222Z00044a7r<   r   )r   r   s    @r   rX   rX      s/     ---8 8 8 8 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      s	    1 0 & & & & & &       ' ' ' ' ' ' 				 



 ! ! ! ! ! !  0% % %P  2@ @ @
	 	 	 	

  "< < <~$ $ $ $ $r<   