o
    ˷eW                     @   s  d 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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d/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?Zi dd@dd@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Z e!ee!eksJ e!ee!eks	J e!e e!eksJ dedQe"dRe"dSe"dTee# dUe#f
dVdWZ$ee"e#f Z%G dXdY dYZ&G dZd[ d[eee"e"e"f e"f Z'e&d\d]Z(e&d^d]Z)e' Z*G d_d` d`eee#f Z+dae"dUee"e"f fdbdcZ,G ddd deZ-dS )fz
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/
    N)
CallableDictHashableIterableListOptionalSequenceSet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      rK   rK      rM   rM      rO   rO      r   r   r   rQ   r   rQ   rQ   r   )r   r   rQ   rQ   r   rQ   r   rQ   rQ   rO   r   r   r   rO   r   rO   rO   r   )r   r   rO   rO   r   rO   r   rO   rO    rgbexcludereturnc                 C   s   t |}t| | t||  t||   }|dkr!|g d d}d}t D ])\}\}}	}
|dkrR||vrR| | d ||	 d  ||
 d  }||k rR|}|}q)|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ansidarkgrayr6   r    r      )listabsextendANSI_COLORS_TO_RGBitems)r\   r]   r^   r_   
saturationdistancematchnamer2g2b2dr[   r[   R/var/www/ideatree/venv/lib/python3.10/site-packages/prompt_toolkit/output/vt100.py_get_closest_ansi_colort   s   	$$rs   c                   @   st   e Zd ZdZddeddfddZ	dd	eeeef d
ee	 de
fddZ	dd	eeeef d
ee	 de
f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bgr`   Nc                 C   s   || _ i | _d S N)ru   _cache)selfru   r[   r[   rr   __init__   s   
z_16ColorCache.__init__r[   valuer_   c                 C   s2   |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.
        )tuplerw   _get)rx   rz   r_   keycacher[   r[   rr   get_code   s
   z_16ColorCache.get_codec                 C   s@   |\}}}t ||||d}| jrt| }||fS t| }||fS )Nr_   )rs   ru   BG_ANSI_COLORSFG_ANSI_COLORS)rx   rz   r_   r\   r]   r^   rl   coder[   r[   rr   r|      s   
z_16ColorCache._get)Fr[   )__name__
__module____qualname____doc__boolry   r   intr   str_ColorCodeAndNamer   r|   r[   r[   r[   rr   rt      s&    
rt   c                   @   s6   e Zd ZdZd	ddZdeeeef defddZdS )
_256ColorCachez:
    Cache which maps (r, g, b) tuples to 256 colors.
    r`   Nc                 C   s(  g }| d | d | d | d | d | d | d | d | d	 | d
 | d | d | d | d | d | d d}tdD ] }||d d  }||d d  }||d  }| |||f qXtddD ]}d|d  }| |||f q~|| _d S )NrI   rP   rR   rS   )r   r      rT   rU   rJ   rL   rV   rW   rX   )r-   r-   rO   rY   rZ   rN   )r   r3            rO      r%      r         
   )appendrangecolors)rx   r   
valuerangeir\   r]   r^   vr[   r[   rr   ry      s6   
















z_256ColorCache.__init__rz   c                 C   st   |\}}}d}d}t | jD ]%\}\}}	}
|dkr3|| d ||	 d  ||
 d  }||k r3|}|}q|| |< |S )Nrc   r      rd   )	enumerater   )rx   rz   r\   r]   r^   rk   rl   r   rn   ro   rp   rq   r[   r[   rr   __missing__   s   
$z_256ColorCache.__missing__r`   N)r   r   r   r   ry   r   r   r   r[   r[   r[   rr   r      s    
 &r   F)ru   Tc                   @   sj   e Zd ZdZdeddfddZdedefdd	Zd
ede	e
e
e
f fddZdededee f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`   Nc                 C   s
   || _ d S rv   )r   )rx   r   r[   r[   rr   ry     s   
z_EscapeCodeCache.__init__attrsc              	   C   s   |\	}}}}}}}}	}
g }| | |pd|pd |r!|d |r(|d |r/|d |r6|d |	r=|d |
rD|d |rK|d |rWd	d
| d }nd}|| |< |S )N 1354789z[0;;m[0m)rg   _colors_to_coder   join)rx   r   fgcolorbgcolorbold	underlinestrikeitalicblinkreversehiddenpartsresultr[   r[   rr   r     s@   






z_EscapeCodeCache.__missing__colorc                 C   sJ   zt |d}W n ty    w |d? d@ }|d? d@ }|d@ }|||fS )z'Turn 'ffffff', into (0xff, 0xff, 0xff).r   rO   r   )r   
ValueError)rx   r   rgbr\   r]   r^   r[   r[   rr   _color_name_to_rgb=  s   
z#_EscapeCodeCache._color_name_to_rgbfg_colorbg_colorc                    sV   ddt dtdtt f fdd}g }||d || d tt |S )	zR
        Return a tuple with the vt100 values  that represent this color.
        r   r   ru   r`   c           
         s   |rt nt}| rjtjkrg S | |v r||  gS z| }W n ty+   g  Y S w jtjkrV|rJ kr<g}ng }tj	||d\}}|gS t
	|\}}||gS jtjkrl|\}}}	|redndd|||	gS |rpdnddt| gS )Nr   0   &   rd      )r   r   r   r   DEPTH_1_BITr   r   DEPTH_4_BIT_16_bg_colorsr   _16_fg_colorsDEPTH_24_BIT_256_colors)
r   ru   tabler   r_   r   rm   r\   r]   r^   r   fg_ansir   rx   r[   rr   getR  s0   

z-_EscapeCodeCache._colors_to_code.<locals>.getFT)r   r   r   r   rg   map)rx   r   r   r   r   r[   r   rr   r   I  s   $*
z _EscapeCodeCache._colors_to_code)r   r   r   r   r   ry   r   r   r   r   r   r   r   r   r[   r[   r[   rr   r   
  s    'r   filenoc                 C   s   t | }|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   sizer[   r[   rr   	_get_size  s   
r   c                   @   s`  e Zd ZU dZe Zee ed< 			d^de	de
g ef dee dee d	ed
dfddZe			d^de	dee dee d	ed
d f
ddZd
efddZd
efddZd
efddZded
dfddZded
dfddZded
dfddZd_ddZd_dd Zd_d!d"Zd_d#d$Zd_d%d&Zd_d'd(Zd_d)d*Zd_d+d,Z d_d-d.Z!d/e"d0ed
dfd1d2Z#d_d3d4Z$d_d5d6Z%d_d7d8Z&d_d9d:Z'd_d;d<Z(d`d>ed?ed
dfd@dAZ)dBed
dfdCdDZ*dBed
dfdEdFZ+dBed
dfdGdHZ,dBed
dfdIdJZ-d_dKdLZ.d_dMdNZ/dOe0d
dfdPdQZ1d_dRdSZ2d_dTdUZ3d_dVdWZ4e5d
efdXdYZ6d_dZd[Z7d
efd\d]Z8dS )ar   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, ...)
    _fds_not_a_terminalNTstdoutget_sizetermdefault_color_depthenable_bellr`   c              	      s~   t  fd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 S )Nc                 3   s    | ]}t  |V  qd S rv   )hasattr).0ar   r[   rr   	<genexpr>  s    z(Vt100_Output.__init__.<locals>.<genexpr>)writeflushF)all_bufferr   r   r   r   r   r   r   r   r   DEPTH_8_BITr   _escape_code_caches_cursor_shape_changed)rx   r   r   r   r   r   r[   r   rr   ry     s   	

zVt100_Output.__init__c                    s   z   }W n tjy   d}Y nw   s9|du s || jvr9d}tj||  tj  |dur9| j	| dt
f fdd}|  ||||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).
r`   c                     sD   d\} }z
t   \} }W n	 ty   Y nw t| pd|pddS )N)NN   P   rowsr   )r   r   OSErrorr   r   r   r[   rr   r     s   z'Vt100_Output.from_pty.<locals>.get_size)r   r   r   )r   ioUnsupportedOperationisattyr   sysstderrr   r   addr   )clsr   r   r   r   fdmsgr   r[   r   rr   from_pty  s&   
zVt100_Output.from_ptyc                 C   s   |   S rv   )r   rx   r[   r[   rr   r     s   zVt100_Output.get_sizec                 C   s
   | j  S )zReturn file descriptor.)r   r   r   r[   r[   rr   r     s   
zVt100_Output.filenoc                 C   s   | j jS )z Return encoding used for stdout.)r   encodingr   r[   r[   rr   r     s   zVt100_Output.encodingdatac                 C   s   | j | dS )z+
        Write raw data to output.
        N)r   r   rx   r   r[   r[   rr   	write_raw  s   zVt100_Output.write_rawc                 C   s   | j |dd dS )zn
        Write text to output.
        (Removes vt100 escape codes. -- used for safely writing text.)
        ?N)r   r   replacer   r[   r[   rr   r     s   zVt100_Output.writetitlec                 C   s0   | j dvr| d|dddd  dS dS )z%
        Set terminal title.
        linuxzeterm-colorz]2;%sr   r   N)r   r   r   )rx   r   r[   r[   rr   	set_title  s
   
zVt100_Output.set_titlec                 C      |  d d S )Nr   )r  r   r[   r[   rr   clear_title     zVt100_Output.clear_titlec                 C      |  d dS )zd
        Erases the screen with the background colour and moves the cursor to
        home.
        z[2JNr   r   r[   r[   rr   erase_screen     zVt100_Output.erase_screenc                 C   r  )Nz[?1049h[Hr	  r   r[   r[   rr   enter_alternate_screen  r  z#Vt100_Output.enter_alternate_screenc                 C   r  )Nz[?1049lr	  r   r[   r[   rr   quit_alternate_screen  r  z"Vt100_Output.quit_alternate_screenc                 C   ,   |  d |  d |  d |  d d S )Nz[?1000hz[?1003hz[?1015hz[?1006hr	  r   r[   r[   rr   enable_mouse_support!  s   


z!Vt100_Output.enable_mouse_supportc                 C   r  )Nz[?1000lz[?1015lz[?1006lz[?1003lr	  r   r[   r[   rr   disable_mouse_support0  s   


z"Vt100_Output.disable_mouse_supportc                 C   r  )zY
        Erases from the current cursor position to the end of the current line.
        z[KNr	  r   r[   r[   rr   erase_end_of_line6  s   zVt100_Output.erase_end_of_linec                 C   r  )zc
        Erases the screen from the current line down to the bottom of the
        screen.
        z[JNr	  r   r[   r[   rr   
erase_down<  r  zVt100_Output.erase_downc                 C   r  )Nr   r	  r   r[   r[   rr   reset_attributesC  r  zVt100_Output.reset_attributesr   r   c                 C   s   | j | }| ||  dS )zW
        Create new style and output.

        :param attrs: `Attrs` instance.
        N)r   r   )rx   r   r   escape_code_cacher[   r[   rr   set_attributesF  s   
zVt100_Output.set_attributesc                 C   r  )Nz[?7lr	  r   r[   r[   rr   disable_autowrapR  r  zVt100_Output.disable_autowrapc                 C   r  )Nz[?7hr	  r   r[   r[   rr   enable_autowrapU  r  zVt100_Output.enable_autowrapc                 C   r  )Nz[?2004hr	  r   r[   r[   rr   enable_bracketed_pasteX  r  z#Vt100_Output.enable_bracketed_pastec                 C   r  )Nz[?2004lr	  r   r[   r[   rr   disable_bracketed_paste[  r  z$Vt100_Output.disable_bracketed_pastec                 C   r  )zh
        For vt100 only.
        Put the terminal in cursor mode (instead of application mode).
        z[?1lNr	  r   r[   r[   rr   reset_cursor_key_mode^  s   z"Vt100_Output.reset_cursor_key_moder   rowcolumnc                 C   s   |  d||f  dS )z'
        Move cursor position.
        z[%i;%iHNr	  )rx   r  r  r[   r[   rr   cursor_gotof  s   zVt100_Output.cursor_gotoamountc                 C   4   |dkrd S |dkr|  d d S |  d|  d S )Nr   r   z[Az[%iAr	  rx   r  r[   r[   rr   	cursor_upl  
   zVt100_Output.cursor_upc                 C   r  )Nr   r   z[Bz[%iBr	  r   r[   r[   rr   cursor_downt  s
   zVt100_Output.cursor_downc                 C   r  )Nr   r   z[Cz[%iCr	  r   r[   r[   rr   cursor_forward~  r"  zVt100_Output.cursor_forwardc                 C   r  )Nr   r   z[%iDr	  r   r[   r[   rr   cursor_backward  r"  zVt100_Output.cursor_backwardc                 C   r  )Nz[?25lr	  r   r[   r[   rr   hide_cursor  r  zVt100_Output.hide_cursorc                 C   r  )Nz[?12l[?25hr	  r   r[   r[   rr   show_cursor  r  zVt100_Output.show_cursorcursor_shapec                 C   sN   |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   )rx   r)  r[   r[   rr   set_cursor_shape  s   
zVt100_Output.set_cursor_shapec                 C   s   | j rd| _ | d dS dS )zReset cursor shape.Fz[0 qN)r   r   r   r[   r[   rr   reset_cursor_shape  s   zVt100_Output.reset_cursor_shapec                 C   s,   | j sdS d| j }g | _ t| j| dS )z3
        Write to output stream and flush.
        Nr   )r   r   r   r   r   r[   r[   rr   r     s
   zVt100_Output.flushc                 C   s   |  d |   dS )z:
        Asks for a cursor position report (CPR).
        z[6nN)r   r   r   r[   r[   rr   ask_for_cpr  s   
zVt100_Output.ask_for_cprc                 C   sF   t jdddkrdS t| jrdS z| j W S  ty"   Y dS w )NPROMPT_TOOLKIT_NO_CPRr   r   F)r   environr   r   r   r   r   r   r   r[   r[   rr   responds_to_cpr  s   
zVt100_Output.responds_to_cprc                 C   s    | j r| d |   dS dS )zSound bell.r  N)r   r   r   r   r[   r[   rr   bell  s   
zVt100_Output.bellc                 C   sF   | j dur| j S | j}|du r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   )rx   r   r[   r[   rr   get_default_color_depth  s   
z$Vt100_Output.get_default_color_depth)NNTr   )r   r   )9r   r   r   r   setr   r	   r   __annotations__r
   r   r   r   r   r   r   ry   classmethodr   r   r   r   r   r   r  r  r
  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r!  r#  r$  r&  r'  r(  r   r1  r2  r   r3  propertyr6  r7  r9  r[   r[   r[   rr   r     s   
 

4


















	

r   ).r   r   r   r   typingr   r   r   r   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   rh   r:  r   r   rs   r   rt   r   r   r   r   r   r   r   r[   r[   r[   rr   <module>   s   0	
	
	$""*
B
y