
    AHj?                    L   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZ d dlmZmZ d dlZd dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ erd dl%m&Z& ejN                  jQ                  d       G d de             Z)y)    )annotationsN)getincrementaldecoder)EventThread)TYPE_CHECKINGAny)events)	loop_last)
ParseError)XTermParser)Driver)WriterThread)Size)Message)InBandWindowResize)AppT)angularc                  0    e Zd ZdZdddd	 	 	 	 	 	 	 	 	 d! fdZd"dZd"dZed#d	       Zd$d
Z	d%dZ
d"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd&dZd Zd"dZed'd       Zed'd       Zd"dZd"dZd"dZd"dZd"dZd( fd Z  xZ!S ))LinuxDriverz*Powers display and input for Linux / MacOSFTNdebugmousesizec                  t         |   ||||       t        j                  | _        t        j
                  j                         | _        t        j
                  j                         | _        d| _	        t               | _        d| _        d| _        d| _        d| _        d| _        t#        j"                  t"        j$                  | j&                         t#        j"                  t"        j(                  | j*                         y)zInitialize Linux driver.

        Args:
            app: The App instance.
            debug: Enable debug mode.
            mouse: Enable mouse support.
            size: Initial size of the terminal or `None` to detect.
        r   NF)super__init__sys
__stderr___file	__stdin__filenoisatty	input_ttyattrs_beforer   
exit_event_key_thread_writer_thread_must_signal_resume_in_band_window_resize_mouse_pixelssignalSIGTSTP_sigtstp_applicationSIGCONT_sigcont_application)selfappr   r   r   	__class__s        T/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/drivers/linux_driver.pyr   zLinuxDriver.__init__"   s      	ETB^^
mm**,--/.2'*.37 $) &+#" 	fnnd&?&?@fnnd&?&?@    c                    | j                   r| j                          d| _        t        j                  t        j
                         t        j                         y)zHandle a SIGTSTP signal.TN)_auto_restartsuspend_application_moder(   oskillgetpidr+   SIGSTOPr0   _s     r3   r-   z LinuxDriver._sigtstp_applicationJ   s=     ))+ (,D$ 			V^^,r4   c                >    | j                   r| j                          yy)zHandle a SICONT application.N)r6   resume_application_moder<   s     r3   r/   z LinuxDriver._sigcont_applicationW   s    ((* r4   c                     y)zCan this driver be suspended?T r0   s    r3   can_suspendzLinuxDriver.can_suspend\   s     r4   c              #  (   K   | j                    y wN)_apprB   s    r3   __rich_repr__zLinuxDriver.__rich_repr__a   s     iis   c                    d}d}ddl }	 |j                         \  }}|xs d}|xs d}||fS # t        t        t        f$ r2 	 |j                         \  }}n# t        t        t        f$ r Y nw xY wY Uw xY w)zxDetect the terminal size.

        Returns:
            The size of the terminal as a tuple of (WIDTH, HEIGHT).
        P      r   N)shutilget_terminal_sizeAttributeError
ValueErrorOSError)r0   widthheightrK   s       r3   _get_terminal_sizezLinuxDriver._get_terminal_sized   s     	"446ME6 2f} 
G4 	 & 8 8 :v"J8 	s3   - A3AA3A-*A3,A--A32A3c                    | j                   sy| j                  } |d        |d        |d        |d       | j                          y)z!Enable reporting of mouse events.Nz[?1000hz[?1003hz[?1015hz[?1006h_mousewriteflushr0   rV   s     r3   _enable_mouse_supportz!LinuxDriver._enable_mouse_supporty   s@    {{

mmmm 	

r4   c                N    | j                   sy| j                  d       d| _        y)z!Enable mouse reporting as pixels.Nz[?1016hT)rU   rV   r*   rB   s    r3   _enable_mouse_pixelsz LinuxDriver._enable_mouse_pixels   s     {{

=!!r4   c                &    | j                  d       y)zEnable bracketed paste mode.z[?2004hNrV   rB   s    r3   _enable_bracketed_pastez#LinuxDriver._enable_bracketed_paste       

=!r4   c                &    | j                  d       y )Nz	[?2048$pr]   rB   s    r3   _query_in_band_window_resizez(LinuxDriver._query_in_band_window_resize   s    

>"r4   c                &    | j                  d       y )Nz[?2048hr]   rB   s    r3   _enable_in_band_window_resizez)LinuxDriver._enable_in_band_window_resize   s    

=!r4   c                &    | j                  d       y )Nz[?7hr]   rB   s    r3   _enable_line_wrapzLinuxDriver._enable_line_wrap       

:r4   c                &    | j                  d       y )Nz[?7lr]   rB   s    r3   _disable_line_wrapzLinuxDriver._disable_line_wrap   rf   r4   c                @    | j                   r| j                  d       y y )Nz[?2048l)r)   rV   rB   s    r3   _disable_in_band_window_resizez*LinuxDriver._disable_in_band_window_resize   s    &&JJ}% 'r4   c                &    | j                  d       y)zDisable bracketed paste mode.z[?2004lNr]   rB   s    r3   _disable_bracketed_pastez$LinuxDriver._disable_bracketed_paste   r_   r4   c                    | j                   sy| j                  } |d        |d        |d        |d       | j                          y)z"Disable reporting of mouse events.Nz[?1000lz[?1003lz[?1015lz[?1006lrT   rX   s     r3   _disable_mouse_supportz"LinuxDriver._disable_mouse_support   s>    {{

mmmm

r4   c                `    | j                   J d       | j                   j                  |       y)zTWrite data to the output device.

        Args:
            data: Raw data.
        Nz"Driver must be in application mode)r'   rV   )r0   datas     r3   rV   zLinuxDriver.write   s0     "".T0TT.!!$'r4   c                2
    dd}t        j                   j                        rt        j                  t        j                  |       t        j                  t        j
                  |       	 t        j                   j                  t        j                  t        j                   j                               	 t        j                  t        j                  t        j                         t        j                  t        j
                  t        j                         t        j                         d fdt         j                         _         j                   j#                          d fd}t        j                  t        j$                  |        j'                  d        j)                          	 t        j                   j                         _        	 t        j                   j                        } j-                  |t.        j0                           |t.        j0                  <    j3                  |t.        j4                           |t.        j4                  <   d|t.        j6                     t        j8                  <   	 t        j                   j                  t        j                  |        j'                  d        j'                  d        j'                  d	        j;                          t=         j>                  d
       _                   j@                  j#                           jC                           jE                           jG                           jI                           j)                           jJ                  rFd _%        t        jL                   jN                  jQ                   jS                                      yy# t        j                  $ rg Y t        j                  t        j                  t        j                         t        j                  t        j
                  t        j                         yw xY w# t        j                  t        j                  t        j                         t        j                  t        j
                  t        j                         w xY w# t        j                  $ r d _        Y  w xY w# t        j                  $ r Y @w xY w# t        j                  $ r Y Zw xY w)zStart application mode.c                 p    t        j                  t        j                         t        j                         y)z;Signal handler that will put the application back to sleep.N)r8   r9   r:   r+   r;   )r=   s    r3   _stop_againz7LinuxDriver.start_application_mode.<locals>._stop_again   s    GGBIIK0r4   Nc                     j                         } | \  }}t        ||      }t        j                  ||      }t	        j
                  j                  j                  |             y )Nloop)rR   r   r	   Resizeasynciorun_coroutine_threadsaferF   _post_message)terminal_sizerP   rQ   textual_sizeeventrv   r0   s        r3   send_size_eventz;LinuxDriver.start_application_mode.<locals>.send_size_event   sY     335M)ME6v.LMM,=E,,		''.r4   c                .    j                   s         y y rE   )r)   )signumstackr0   r~   s     r3   on_terminal_resizez>LinuxDriver.start_application_mode.<locals>.on_terminal_resize   s    ..! /r4   z[?1049h   z[?25lz[?1004hz[>1uztextual-input)targetnameFru   returnNone)*r8   r"   r!   r+   SIGTTOUSIGTTINtermios	tcsetattrTCSANOW	tcgetattrerrorSIG_DFLrx   get_running_loopr   r   r'   startSIGWINCHrV   rY   r$   _patch_lflagttyLFLAG_patch_iflagIFLAGCCVMINrW   r   _run_input_threadr&   #_request_terminal_sync_mode_supportra   r^   rh   r(   ry   rF   rz   SignalResume)r0   rs   r   newattrrv   r~   s   `   @@r3   start_application_modez"LinuxDriver.start_application_mode   s]   	1 99T[[! MM&..+6MM&..+6> !!KK'2C2CDKK2P fnnfnn=fnnfnn='')	 +4::6!!#	" 	foo'9:

=!""$	% ' 1 1$++ >D
	''4G "&!2!273993E!FGCII!%!2!273993E!FGCII -.GCFFOGLL)!!$++wH 	

;

=!

:

!)?)?oV 002))+$$&! 	""$ ##',D$,,		''(9(9(;< $] ==   fnnfnn=fnnfnn= fnnfnn=fnnfnn=8 }} 	% $D	%* ==  }} 		s\   0AO =$S "S? /S% Q1Q QQ A&SS"!S"%S<;S<?TTc                    | j                   syt        j                  j                  dd      dk7  r"| j	                  d       | j                          yy)zNWrites an escape sequence to query the terminal support for the sync protocol.NTERM_PROGRAM Apple_Terminalz	[?2026$p)r#   r8   environgetrV   rW   rB   s    r3   r   z/LinuxDriver._request_terminal_sync_mode_support+  sA    
 ~~::>>."-1AAJJ~&JJL Br4   c                    t         j                  j                  d      rdnt        j                  }|t        j
                  t        j                  z  t        j                  z  |z   z  S )zPatch termios lflag.

        Args:
            attributes: New set attributes.

        Returns:
            New lflag.

        TEXTUAL_ALLOW_SIGNALSr   )r8   r   r   r   ISIGECHOICANONIEXTEN)clsattrsr   s      r3   r   zLinuxDriver._patch_lflag6  sH     JJNN#:;q6G$NOOOr4   c                    |t         j                  t         j                  z  t         j                  z  t         j                  z  t         j
                  z   z  S rE   )r   IXONIXOFFICRNLINLCRIGNCR)r   r   s     r3   r   zLinuxDriver._patch_iflagF  sS     LLmm MM	
 mm mm	
 
 	
r4   c                "   	 | j                   j                         st        j                  t        j                  t        j                         | j                          | j                   j                          | j                  | j                  j                          | j                   j                          	 t        j                  | j                  t        j                         yy# t        j                  $ r Y yw xY w# t        $ r Y yw xY w)zDisable further input.N)r%   is_setr+   r   r   rn   setr&   joinclearr   tcflushr!   TCIFLUSHr   	ExceptionrB   s    r3   disable_inputzLinuxDriver.disable_inputU  s    	??))+foov~~>++-##%##/$$))+%%'OODKK1A1AB , }}  		s0   B6D 9.C) )C?<D >C??D 	DDc                   | j                          | j                          | j                          | j                          | j                  :	 t        j                  | j                  t
        j                  | j                         | j                  d       | j                  d       | j                  d       | j                  d       | j                          y# t
        j                  $ r Y jw xY w)z%Stop application mode, restore state.Nz[<uz[?1049lz[?25hz[?1004l)rl   re   rj   r   r$   r   r   r!   r   r   rV   rW   rB   s    r3   stop_application_modez!LinuxDriver.stop_application_modeg  s    %%' ++-(!!$++w@Q@QR 	

9 	

=!

;

=!

 == s   9C C21C2c                R    | j                   | j                   j                          yy)zPerform cleanup.N)r'   stoprB   s    r3   closezLinuxDriver.close~  s%    *$$& +r4   c                    	 | j                          y# t        $ rO ddl}| j                  j	                  | j                  j
                  |j                  j                                Y yw xY w)zu
        Key thread target that wraps run_input_thread() to die gracefully if it raises
        an exception
        r   N)run_input_threadBaseExceptionrich.tracebackrF   
call_laterpanic	traceback	Traceback)r0   richs     r3   r   zLinuxDriver._run_input_thread  sR    
	!!# 	!II  		((*	s    AA+*A+c           	     v   	
 t        j                         }|j                   j                  t         j                          j                  	t         j                  t         j                        }|j                  |j                   t        d             j                  }|t        j                  
	 d	 	 	 	 	 d		
 fd}	  j                  j                         s2 ||j                  d              j                  j                         s2|j!                   j                          ||j                  d      d       |j#                          	  d      D ]  } y# t$        t&        f$ r Y yw xY w# |j#                          	  d      D ]  } w # t$        t&        f$ r Y w w xY wxY w)
z#Wait for input and dispatch events.zutf-8c                    t        |       D ]E  \  }\  }}|z  s  
d      |xr |      }|s n  	|      D ]  }j                  |        G         D ]  }j                  |        y)zProcess events from selector.

            Args:
                selector_events: List of selector events.
                final: True if this is the last call.

            i   finalN)r
   process_message)selector_eventsr   last_selector_keymaskunicode_datar}   
EVENT_READdecodefeedr!   readr0   ticks          r3   process_selector_eventsz=LinuxDriver.run_input_thread.<locals>.process_selector_events  s     09/I 4++}d*$#)$vx*@RV#WL'!%l!3 4,,U344  ,$$U+,r4   g?Tr   r   N)F)r   z'list[tuple[selectors.SelectorKey, int]]r   boolr   r   )	selectorsSelectSelectorregisterr!   r   r   _debugr   r   r   r   r8   r   r%   r   select
unregisterr   EOFErrorr   )r0   selectorparserutf8_decoderr   r}   r   r   r   r!   r   r   s   `     @@@@@@r3   r   zLinuxDriver.run_input_thread  s   ++-$++y';';<))
T[[){{{{5,W57>>ww  	,D	,	, 	, 	,,	oo,,.'(<= oo,,.,#HOOC$8E NN!"X Ej) 	 NN!"X Ej) sI   AF 4F  E. .F ?F F8F#"F8#F52F84F55F8c                   t        |t              ri|j                  r]d| _        |j                  rt
        |   |       n)| j                          t
        |   t        dd             | j                          y t
        |   |       y )NT)	
isinstancer   	supportedr)   enabledr   r   rc   r[   )r0   messager2   s     r3   r   zLinuxDriver.process_message  sr    g12  .2+??G+G4 668G+,>tT,JK))+(r4   )
r1   r   r   r   r   r   r   ztuple[int, int] | Noner   r   r   )r   r   )r   zrich.repr.Result)r   ztuple[int, int])rp   strr   r   )r   intr   r   )r   r   r   r   )"__name__
__module____qualname____doc__r   r-   r/   propertyrC   rG   rR   rY   r[   r^   ra   rc   re   rh   rj   rl   rn   rV   r   r   classmethodr   r   r   r   r   r   r   r   __classcell__)r2   s   @r3   r   r      s   4 '+&A&A 	&A
 &A %&A 
&AP-+
  *"""#"&"	(l\	 P P 
 
$.'
2h) )r4   r   )*
__future__r   rx   r8   r   r+   r   r   r   codecsr   	threadingr   r   typingr   r   	rich.reprr   textualr	   textual._loopr
   textual._parserr   textual._xterm_parserr   textual.driverr   textual.drivers._writer_threadr   textual.geometryr   textual.messager   textual.messagesr   textual.appr   reprautor   rA   r4   r3   <module>r     sw    "  	   
  
 ( # %   # & - ! 7 ! # / v)& v) v)r4   