
    MhW[              	         d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ d
dlmZ d
dlmZ dgZi ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/Zi dd0dd1dd2dd3dd4dd5dd6dd7dd8d d9d"d:d$d;d&d<d(d=d*d>d,d?d.d@Zi ddAddAddBd dCd.dDddEddFddGddHddIddJd"dKd$dLd&dMd(dNd*dOd,dPZ e e           e e          k    sJ  e e           e e          k    sJ  e e           e e          k    sJ dhdidZZ!ee"e#f         Z$ G d[ d\          Z% G d] d^eee"e"e"f         e"f                   Z& e%d_`          Z' e%da`          Z( e&            Z) G db dceee#f                   Z*djdfZ+ G dg de          Z,dS )kz
Output for vt100 terminals.

A lot of thanks, regarding outputting of colors, goes to the Pygments project:
(We don't rely on Pygments anymore, because many things are very custom, and
everything has been highly optimized.)
http://pygments.org/
    )annotationsN)CallableDictHashableIterableSequenceTextIOTuple)CursorShape)Size)Output)ANSI_COLOR_NAMESAttrs)is_dumb_terminal   )
ColorDepth)flush_stdoutVt100_Outputansidefault'   	ansiblack   ansired   	ansigreen    
ansiyellow!   ansiblue"   ansimagenta#   ansicyan$   ansigray%   ansibrightblackZ   ansibrightred[   ansibrightgreen\   ansibrightyellow]   ansibrightblue^   ansibrightmagenta_   ansibrightcyan`   	ansiwhitea   1   (   )   *   +   ,   -   .   /   d   e   f   g   h   i   j   k   r   r   r      rJ   rJ      rL   rL      rN   rN      r   r   r   rP   r   rP   rP   r   )r   r   rP   rP   r   rP   r   rP   rP   rN   r   r   r   rN   r   rN   rN   r   )r   r   rN   rN   r   rN   r   rN   rN    rintgbexcludeSequence[str]returnstrc                x   t          |          }t          | |z
            t          ||z
            z   t          || z
            z   }|dk    r|                    g d           d}d}t                                          D ]7\  }\  }}	}
|dk    r(||vr$| |z
  dz  ||	z
  dz  z   ||
z
  dz  z   }||k     r|}|}8|S )z
    Find closest ANSI color. Return it by name.

    :param r: Red (Between 0 and 255.)
    :param g: Green (Between 0 and 255.)
    :param b: Blue (Between 0 and 255.)
    :param exclude: A tuple of color names to exclude. (E.g. ``('ansired', )``.)
    r   )ansilightgrayansidarkgrayr5   r    r      )listabsextendANSI_COLORS_TO_RGBitems)r[   r]   r^   r_   
saturationdistancematchnamer2g2b2ds               [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/output/vt100.py_get_closest_ansi_colorrv   l   s     7mmG QUc!a%jj(3q1u::5JBRRRSSS HE06688  lr2r=  T%8%8RARA-RA=A8||L    c                  4    e Zd ZdZdddZ	 dddZ	 dddZdS )_16ColorCachez
    Cache which maps (r, g, b) tuples to 16 ansi colors.

    :param bg: Cache for background colors, instead of foreground.
    Fbgboolra   Nonec                "    || _         i | _        d S N)rz   _cache)selfrz   s     ru   __init__z_16ColorCache.__init__   s    9;rw   rZ   valuetuple[int, int, int]r_   r`   _ColorCodeAndNamec                |    |t          |          f}| j        }||vr|                     ||          ||<   ||         S )zx
        Return a (ansi_code, ansi_name) tuple. (E.g. ``(44, 'ansiblue')``.) for
        a given (r,g,b) value.
        )tupler   _get)r   r   r_   keycaches        ru   get_codez_16ColorCache.get_code   sG     g/e5'22E#JSzrw   c                    |\  }}}t          ||||          }| j        rt          |         }nt          |         }||fS )Nr_   )rv   rz   BG_ANSI_COLORSFG_ANSI_COLORS)r   r   r_   r[   r]   r^   ro   codes           ru   r   z_16ColorCache._get   sQ     1a'1aAAA 7 	)!%(DD!%(DU{rw   N)F)rz   r{   ra   r|   rZ   )r   r   r_   r`   ra   r   )__name__
__module____qualname____doc__r   r   r   rZ   rw   ru   ry   ry      sw         < < < < <
 EG      EG      rw   ry   c                  "    e Zd ZdZd
dZddZd	S )_256ColorCachez:
    Cache which maps (r, g, b) tuples to 256 colors.
    ra   r|   c                   g }|                     d           |                     d           |                     d           |                     d           |                     d           |                     d           |                     d           |                     d           |                     d	           |                     d
           |                     d           |                     d           |                     d           |                     d           |                     d           |                     d           d}t          d          D ]A}||dz  dz           }||dz  dz           }||dz           }|                     |||f           Bt          dd          D ]"}d|dz  z   }|                     |||f           #|| _        d S )NrH   rO   rQ   rR   )r   r      rS   rT   rI   rK   rU   rV   rW   )r,   r,   rN   rX   rY   rM   )r   r2            rN      r$      r         
   )appendrangecolors)r   r   
valuerangeir[   r]   r^   vs           ru   r   z_256ColorCache.__init__   s   -/ 	()))()))()))()))()))()))()))()))()))()))()))()))()))()))()))())) :
s 	% 	%AAGq=)AAFa<(A1q5!AMM1a)$$$$ q" 	% 	%AAF
AMM1a)$$$$rw   r   r   r\   c                    |\  }}}d}d}t          | j                  D ]3\  }\  }}	}
|dk    r$||z
  dz  ||	z
  dz  z   ||
z
  dz  z   }||k     r|}|}4|| |<   |S )Nrf   r      rg   )	enumerater   )r   r   r[   r]   r^   rn   ro   r   rq   rr   rs   rt   s               ru   __missing__z_256ColorCache.__missing__   s    1a !(55 	! 	!OA|BBww VMQVM1QVMAx<<E H Urw   Nra   r|   )r   r   ra   r\   )r   r   r   r   r   r   rZ   rw   ru   r   r      sG         $ $ $ $L     rw   r   F)rz   Tc                  2    e Zd ZdZddZdd
ZddZddZdS )_EscapeCodeCachez
    Cache for VT100 escape codes. It maps
    (fgcolor, bgcolor, bold, underline, strike, reverse) tuples to VT100
    escape sequences.

    :param true_color: When True, use 24bit colors instead of 256 colors.
    color_depthr   ra   r|   c                    || _         d S r~   )r   )r   r   s     ru   r   z_EscapeCodeCache.__init__
  s    &rw   attrsr   rb   c           	        |\	  }}}}}}}}	}
g }|                     |                     |pd|pd                     |r|                    d           |r|                    d           |r|                    d           |r|                    d           |	r|                    d           |
r|                    d           |r|                    d           |rd	d
                    |          z   dz   }nd}|| |<   |S )N 1354789z[0;;m[0m)rj   _colors_to_coder   join)r   r   fgcolorbgcolorbold	underlinestrikeitalicblinkreversehiddenpartsresults                ru   r   z_EscapeCodeCache.__missing__  sB    
	
T))'-RBGGHHH 	LL 	LL 	LL 	LL 	LL 	LL 	LL 	%036FFFUrw   colorr   c                v    	 t          |d          }|dz	  dz  }|dz	  dz  }|dz  }|||fS # t          $ r  w xY w)z'Turn 'ffffff', into (0xff, 0xff, 0xff).r   rN   r   )r\   
ValueError)r   r   rgbr[   r]   r^   s         ru   _color_name_to_rgbz#_EscapeCodeCache._color_name_to_rgb4  sf    	eR..C d"AT!Ad
Aa7N  	 	 		s   , 8fg_colorbg_colorIterable[str]c                     dd fd}g }|                      |d	                     |                      |d
                     t          t          |          S )zR
        Return a tuple with the vt100 values  that represent this color.
        r   r   rb   rz   r{   ra   	list[int]c                   |rt           nt          }| rj        t          j        k    rg S | |v r	||          gS 	                     |           }n# t          $ r g cY S w xY wj        t          j        k    rR|r.
k    rg}ng }t          	                    ||          \  }}|gS t          	                    |          \  }}||gS j        t          j        k    r|\  }}}	|rdndd|||	gS |rdnddt          |         gS )Nr   0   &   rg      )r   r   r   r   DEPTH_1_BITr   r   DEPTH_4_BIT_16_bg_colorsr   _16_fg_colorsDEPTH_24_BIT_256_colors)r   rz   tabler   r_   r   rp   r[   r]   r^   r   fg_ansir   r   s             ru   getz-_EscapeCodeCache._colors_to_code.<locals>.getI  sU    ')<NNnE #ED,
0FFF	 %e~%11%88CC!   III #z'=== 
&#x//'.iGG&(G%2%;%;C%;%Q%Q
d $v%2%;%;C%@%@
d"& $v %)@@@!GAq!#%-RR21a;; $&-RR2;s3CDDs   A AAFT)r   rb   rz   r{   ra   r   )rj   maprb   )r   r   r   r   r   r   s   ```  @ru   r   z _EscapeCodeCache._colors_to_code@  s     (	E (	E (	E (	E (	E (	E (	E (	E (	ET cc(E**+++cc(D))***3rw   N)r   r   ra   r|   )r   r   ra   rb   )r   rb   ra   r   )r   rb   r   rb   ra   r   )r   r   r   r   r   r   r   r   rZ   rw   ru   r   r     so         ' ' ' '% % % %N
 
 
 
7  7  7  7  7  7 rw   r   filenotuple[int, int]c                F    t          j        |           }|j        |j        fS )zw
    Get the size of this pseudo terminal.

    :param fileno: stdout.fileno()
    :returns: A (rows, cols) tuple.
    )osget_terminal_sizelinescolumns)r   sizes     ru   	_get_sizer   z  s#     ''D:t|##rw   c                     e Zd ZU dZ e            Zded<   	 	 	 	 dGdHdZe	 	 	 dIdJd            Z	dKdZ
dLdZdMdZdNdZdNdZdOdZdPd ZdPd!ZdPd"ZdPd#ZdPd$ZdPd%ZdPd&ZdPd'ZdPd(ZdQd-ZdPd.ZdPd/ZdPd0ZdPd1ZdPd2ZdRdSd6ZdTd8Z dTd9Z!dTd:Z"dTd;Z#dPd<Z$dPd=Z%dUd@Z&dPdAZ'dPdBZ(dPdCZ)e*dVdD            Z+dPdEZ,dWdFZ-dS )Xr   a  
    :param get_size: A callable which returns the `Size` of the output terminal.
    :param stdout: Any object with has a `write` and `flush` method + an 'encoding' property.
    :param term: The terminal environment variable. (xterm, xterm-256color, linux, ...)
    :param enable_cpr: When `True` (the default), send "cursor position
        request" escape sequences to the output in order to detect the cursor
        position. That way, we can properly determine how much space there is
        available for the UI (especially for drop down menus) to render. The
        `Renderer` will still try to figure out whether the current terminal
        does respond to CPR escapes. When `False`, never attempt to send CPR
        requests.
    zset[int]_fds_not_a_terminalNTstdoutr	   get_sizeCallable[[], Size]term
str | Nonedefault_color_depthColorDepth | Noneenable_bellr{   
enable_cprra   r|   c           
        t          fddD                       sJ g | _        | _        || _        || _        || _        || _        || _        t          j	        t          t          j	                  t          j        t          t          j                  t          j        t          t          j                  t          j        t          t          j                  i| _        d| _        d | _        d S )Nc              3  8   K   | ]}t          |          V  d S r~   )hasattr).0ar   s     ru   	<genexpr>z(Vt100_Output.__init__.<locals>.<genexpr>  s-      BB!761%%BBBBBBrw   )writeflushF)all_bufferr   r   r   r   r   r   r   r   r   r   DEPTH_8_BITr   _escape_code_caches_cursor_shape_changed_cursor_visible)r   r   r   r   r   r   r   s    `     ru   r   zVt100_Output.__init__  s     BBBB/ABBBBBBBB"$$#6 !	&$ "$4Z5K$L$L"$4Z5K$L$L"$4Z5K$L$L#%5j6M%N%N	H
  &+"
 -1rw   c                   	                                  }n# t          j        $ r d}Y nw xY w                                si|	|| j        vr^d}t
          j                            ||z             t
          j                                         || j        	                    |           dfd} | ||||          S )z
        Create an Output class from a pseudo terminal.
        (This will take the dimensions by reading the pseudo
        terminal attributes.)
        Nz+Warning: Output is not a terminal (fd=%r).
ra   r   c                     d\  } }	 t                                                    \  } }n# t          $ r Y nw xY wt          | pd|pd          S )N)NN   P   )rowsr   )r   r   OSErrorr   )r  r   r   s     ru   r   z'Vt100_Output.from_pty.<locals>.get_size  si     )MD' )&--// : :gg   TZRB????s   $- 
::)r   r   r   ra   r   )
r   ioUnsupportedOperationisattyr   sysstderrr   r   add)clsr   r   r   r   fdmsgr   s    `      ru   from_ptyzVt100_Output.from_pty  s    "	BB& 	 	 	BBB	 }} 	0BJ"C<S2S2S@CJS2X&&&J~'++B///	@ 	@ 	@ 	@ 	@ 	@  s 3#
 
 
 	
s    ,,r   c                *    |                                  S r~   )r   r   s    ru   r   zVt100_Output.get_size  s    ~~rw   r\   c                4    | j                                         S )zReturn file descriptor.)r   r   r  s    ru   r   zVt100_Output.fileno  s    {!!###rw   rb   c                    | j         j        S )z Return encoding used for stdout.)r   encodingr  s    ru   r  zVt100_Output.encoding  s    {##rw   datac                :    | j                             |           dS )z+
        Write raw data to output.
        N)r   r   r   r  s     ru   	write_rawzVt100_Output.write_raw  s      	D!!!!!rw   c                b    | j                             |                    dd                     dS )zn
        Write text to output.
        (Removes vt100 escape codes. -- used for safely writing text.)
        ?N)r   r   replacer  s     ru   r   zVt100_Output.write  s.    
 	DLL5566666rw   titlec                    | j         dvrR|                     d                    |                    dd                              dd                               dS dS )z%
        Set terminal title.
        linuxzeterm-colorz]2;{}r  r   N)r   r  formatr!  )r   r"  s     ru   	set_titlezVt100_Output.set_title	  st     9 
 
 
 NN&&u}}VR'@'@'H'HQS'T'TUU    	
 
rw   c                0    |                      d           d S )Nr   )r(  r  s    ru   clear_titlezVt100_Output.clear_title  s    rrw   c                0    |                      d           dS )zc
        Erases the screen with the background color and moves the cursor to
        home.
        z[2JNr  r  s    ru   erase_screenzVt100_Output.erase_screen  s    
 	y!!!!!rw   c                0    |                      d           d S )Nz[?1049h[Hr,  r  s    ru   enter_alternate_screenz#Vt100_Output.enter_alternate_screen  s    *+++++rw   c                0    |                      d           d S )Nz[?1049lr,  r  s    ru   quit_alternate_screenz"Vt100_Output.quit_alternate_screen"      }%%%%%rw   c                    |                      d           |                      d           |                      d           |                      d           d S )Nz[?1000hz[?1003hz[?1015hz[?1006hr,  r  s    ru   enable_mouse_supportz!Vt100_Output.enable_mouse_support%  sV    }%%% 	}%%% 	}%%% 	}%%%%%rw   c                    |                      d           |                      d           |                      d           |                      d           d S )Nz[?1000lz[?1015lz[?1006lz[?1003lr,  r  s    ru   disable_mouse_supportz"Vt100_Output.disable_mouse_support4  sP    }%%%}%%%}%%%}%%%%%rw   c                0    |                      d           dS )zY
        Erases from the current cursor position to the end of the current line.
        z[KNr,  r  s    ru   erase_end_of_linezVt100_Output.erase_end_of_line:  s     	x     rw   c                0    |                      d           dS )zc
        Erases the screen from the current line down to the bottom of the
        screen.
        z[JNr,  r  s    ru   
erase_downzVt100_Output.erase_down@  s    
 	x     rw   c                0    |                      d           d S )Nr   r,  r  s    ru   reset_attributeszVt100_Output.reset_attributesG  s    y!!!!!rw   r   r   r   r   c                V    | j         |         }|                     ||                    dS )zW
        Create new style and output.

        :param attrs: `Attrs` instance.
        N)r  r  )r   r   r   escape_code_caches       ru   set_attributeszVt100_Output.set_attributesJ  s2     !4[A 	(/00000rw   c                0    |                      d           d S )Nz[?7lr,  r  s    ru   disable_autowrapzVt100_Output.disable_autowrapV      z"""""rw   c                0    |                      d           d S )Nz[?7hr,  r  s    ru   enable_autowrapzVt100_Output.enable_autowrapY  rB  rw   c                0    |                      d           d S )Nz[?2004hr,  r  s    ru   enable_bracketed_pastez#Vt100_Output.enable_bracketed_paste\  r2  rw   c                0    |                      d           d S )Nz[?2004lr,  r  s    ru   disable_bracketed_pastez$Vt100_Output.disable_bracketed_paste_  r2  rw   c                0    |                      d           dS )zh
        For vt100 only.
        Put the terminal in cursor mode (instead of application mode).
        z[?1lNr,  r  s    ru   reset_cursor_key_modez"Vt100_Output.reset_cursor_key_modeb  s     	z"""""rw   r   rowcolumnc                :    |                      d||fz             dS )z'
        Move cursor position.
        z[%i;%iHNr,  )r   rK  rL  s      ru   cursor_gotozVt100_Output.cursor_gotoj  s%     	}V}455555rw   amountc                    |dk    rd S |dk    r|                      d           d S |                      d|z             d S )Nr   r   z[Az[%iAr,  r   rO  s     ru   	cursor_upzVt100_Output.cursor_upp  N    Q;;Dq[[NN8$$$$$NN:./////rw   c                    |dk    rd S |dk    r|                      d           d S |                      d|z             d S )Nr   r   z[Bz[%iBr,  rQ  s     ru   cursor_downzVt100_Output.cursor_downx  sP    Q;;Dq[[ NN8$$$$$NN:./////rw   c                    |dk    rd S |dk    r|                      d           d S |                      d|z             d S )Nr   r   z[Cz[%iCr,  rQ  s     ru   cursor_forwardzVt100_Output.cursor_forward  rS  rw   c                    |dk    rd S |dk    r|                      d           d S |                      d|z             d S )Nr   r   z[%iDr,  rQ  s     ru   cursor_backwardzVt100_Output.cursor_backward  sN    Q;;Dq[[NN4     NN:./////rw   c                T    | j         dv rd| _         |                     d           d S d S )N)TNFz[?25lr  r  r  s    ru   hide_cursorzVt100_Output.hide_cursor  s8    <//#(D NN;''''' 0/rw   c                T    | j         dv rd| _         |                     d           d S d S )N)FNTz[?12l[?25hr\  r  s    ru   show_cursorzVt100_Output.show_cursor  s9    =00#'D NN/00000 10rw   cursor_shaper   c                   |t           j        k    rd S d| _        |                     t           j        dt           j        dt           j        dt           j        dt           j        dt           j	        di
                    |d                     d S )	NTz[2 qz[6 qz[4 qz[1 qz[5 qz[3 qr   )r   _NEVER_CHANGEr  r  BLOCKBEAM	UNDERLINEBLINKING_BLOCKBLINKING_BEAMBLINKING_UNDERLINEr   )r   r`  s     ru   set_cursor_shapezVt100_Output.set_cursor_shape  s    ;444F%)"!: *%z*J):.
 c,##		
 		
 		
 		
 		
rw   c                P    | j         rd| _         |                     d           dS dS )zReset cursor shape.Fz[0 qN)r  r  r  s    ru   reset_cursor_shapezVt100_Output.reset_cursor_shape  s;     % 	').D& NN:&&&&&		' 	'rw   c                    | j         sdS d                    | j                   }g | _         t          | j        |           dS )z3
        Write to output stream and flush.
        Nr   )r   r   r   r   r  s     ru   r   zVt100_Output.flush  sE     | 	Fwwt|$$T[$'''''rw   c                X    |                      d           |                                  dS )z:
        Asks for a cursor position report (CPR).
        z[6nN)r  r   r  s    ru   ask_for_cprzVt100_Output.ask_for_cpr  s'     	y!!!

rw   c                    | j         sdS t          j                            dd          dk    rdS t	          | j                  rdS 	 | j                                        S # t          $ r Y dS w xY w)NFPROMPT_TOOLKIT_NO_CPRr   r   )	r   r   environr   r   r   r   r  r   r  s    ru   responds_to_cprzVt100_Output.responds_to_cpr  s     	5 :>>1266#==5DI&& 	5	;%%''' 	 	 	55	s   A   
A.-A.c                j    | j         r+|                     d           |                                  dS dS )zSound bell.r&  N)r   r  r   r  s    ru   bellzVt100_Output.bell  s:     	NN4   JJLLLLL	 	rw   c                    | j         | j         S | j        }|t          j        S t	          |          rt          j        S |dv rt          j        S t          j        S )z
        Return the default color depth for a vt100 terminal, according to the
        our term value.

        We prefer 256 colors almost always, because this is what most terminals
        support these days, and is a good default.
        Nr$  )r   r   r   DEFAULTr   r   r   )r   r   s     ru   get_default_color_depthz$Vt100_Output.get_default_color_depth  sb     #/++y<%%D!! 	*))+++))!!rw   )NNTT)r   r	   r   r   r   r   r   r   r   r{   r   r{   ra   r|   )NNT)
r   r	   r   r   r   r   r   r{   ra   r   r
  )ra   r\   )ra   rb   )r  rb   ra   r|   )r"  rb   ra   r|   r   )r   r   r   r   ra   r|   )r   r   )rK  r\   rL  r\   ra   r|   )rO  r\   ra   r|   )r`  r   ra   r|   )ra   r{   )ra   r   ).r   r   r   r   setr   __annotations__r   classmethodr  r   r   r  r  r   r(  r*  r-  r/  r1  r4  r6  r8  r:  r<  r?  rA  rD  rF  rH  rJ  rN  rR  rU  rW  rZ  r]  r_  ri  rk  r   rn  propertyrr  rt  rw  rZ   rw   ru   r   r     s`          %(CEE))))  15 #1 #1 #1 #1 #1J   15 2
 2
 2
 2
 [2
h       $ $ $ $$ $ $ $" " " "7 7 7 7
 
 
 
   " " " ", , , ,& & & &
& 
& 
& 
&& & & &! ! ! !! ! ! !" " " "
1 
1 
1 
1# # # ## # # #& & & && & & &# # # #6 6 6 6 60 0 0 00 0 0 00 0 0 00 0 0 0( ( ( (
1 1 1 1

 
 
 
 ' ' ' '
( 
( 
( 
(       X    " " " " " "rw   r   )
r[   r\   r]   r\   r^   r\   r_   r`   ra   rb   )r   r\   ra   r   )-r   
__future__r   r  r   r  typingr   r   r   r   r   r	   r
   prompt_toolkit.cursor_shapesr   prompt_toolkit.data_structuresr   prompt_toolkit.outputr   prompt_toolkit.stylesr   r   prompt_toolkit.utilsr   r   r   r   __all__r   r   rk   rx  rv   r\   rb   r   ry   r   r   r   r   r   r   r   rZ   rw   ru   <module>r     sT    # " " " " " 				 				 



 N N N N N N N N N N N N N N N N N N 4 4 4 4 4 4 / / / / / / ( ( ( ( ( ( 9 9 9 9 9 9 9 9 1 1 1 1 1 1 # # # # # # & & & & & & 
2  r	
  "  2   r R r   b!" #$ b%& ',2  r	
  "  2   s S s   c!" #$ c%& '.  # " ) # ! # $ " %  "!$ '%& )'( *)* (+, +-. (/ 6 s>cc"2333333
s>cc"2333333
s##&6"7"77777    D #s(O & & & & & & & &R? ? ? ? ?T%S#.34 ? ? ?D '''&&&nv  v  v  v  v tE3J' v  v  v r$ $ $ $p" p" p" p" p"6 p" p" p" p" p"rw   