B
    't\lM                 @   s  d 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Zdd	lZdd	lZdd	lZd
gZddddddddddddddddddZdddd d!d"d#d$d%d&d'd(d)d*d+d,d-dZd.d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>Zeeeeksteeeeksteeeeks*tdNd@dAZG dBdC dCeZG dDdE dEeZedFdGZedHdGZe ZG dIdJ dJeZdKdL ZG dMd
 d
eZ d	S )Oz
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/
    )unicode_literals)Size)Output)ANSI_COLOR_NAMES   )
ColorDepth)rangeNVt100_Output'             !   "   #   $   %   Z   [   \   ]   ^   _   `   a   )ansidefault	ansiblackansired	ansigreen
ansiyellowansiblueansimagentaansicyanansigrayansibrightblackansibrightredansibrightgreenansibrightyellowansibrightblueansibrightmagentaansibrightcyan	ansiwhite1   (   )   *   +   ,   -   .   /   d   e   f   g   h   i   j   k   )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   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)   r*    c             C   s   t |tstt| | t||  t||   }|dkrB|d7 }d}d}x^t D ]R\}\}}	}
|dkrT||krT| | d ||	 d  ||
 d  }||k rT|}|}qTW |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   )ZansilightgrayZansidarkgrayr+   r   i r      )
isinstancetupleAssertionErrorabsANSI_COLORS_TO_RGBitems)rgbexcludeZ
saturationdistancematchnamer2g2b2drA   rA   :lib/python3.7/site-packages/prompt_toolkit/output/vt100.py_get_closest_ansi_colork   s    	$$rU   c               @   s.   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.
    Fc             C   s   t |tst|| _d S )N)rC   boolrE   bg)selfrX   rA   rA   rT   __init__   s    z_16ColorCache.__init__rA   c             C   s(   ||f}|| kr |  ||| |< | | S )zx
        Return a (ansi_code, ansi_name) tuple. (E.g. ``(44, 'ansiblue')``.) for
        a given (r,g,b) value.
        )_get)rY   valuerL   keyrA   rA   rT   get_code   s    z_16ColorCache.get_codec             C   sB   |\}}}t ||||d}| jr*t| }nt| }|| |< ||fS )N)rL   )rU   rX   BG_ANSI_COLORSFG_ANSI_COLORS)rY   r\   rL   rI   rJ   rK   rN   coderA   rA   rT   r[      s    

z_16ColorCache._getN)F)rA   )rA   )__name__
__module____qualname____doc__rZ   r^   r[   rA   rA   rA   rT   rV      s   


rV   c               @   s    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.
    c             C   s2  g }| d | d | d | d | d | d | d | d | d	 | d
 | d | d | d | d | d | d d}xLtdD ]@}||d d  }||d d  }||d  }| |||f qW x0tddD ]"}d|d  }| |||f qW || _d S )N)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   r?   r   )r?   r?   r   )r   r   r?   )r?   r   r?   )r   r?   r?   )r?   r?   r?   )r   r            r?      r      r         
   )appendr   colors)rY   rq   Z
valuerangeirI   rJ   rK   vrA   rA   rT   rZ      s6    















z_256ColorCache.__init__c             C   sx   |\}}}d}d}xXt | jD ]J\}\}}	}
|dkr|| d ||	 d  ||
 d  }||k r|}|}qW || |< |S )Ni r      rB   )	enumeraterq   )rY   r\   rI   rJ   rK   rM   rN   rr   rP   rQ   rR   rS   rA   rA   rT   __missing__   s    
$z_256ColorCache.__missing__N)rb   rc   rd   re   rZ   rv   rA   rA   rA   rT   rf      s   &rf   F)rX   Tc               @   s0   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, reverse) tuples to VT100 escape sequences.

    :param true_color: When True, use 24bit colors instead of 256 colors.
    c             C   s   |t jkst|| _d S )N)r   Z_ALLrE   color_depth)rY   rx   rA   rA   rT   rZ      s    z_EscapeCodeCache.__init__c             C   s   |\}}}}}}}}	g }
|
 | || |r8|
d |rF|
d |rT|
d |rb|
d |rp|
d |	r~|
d |
rdd|
 d	 }nd
}|| |< |S )N135478z[0;;mz[0m)extend_colors_to_coderp   join)rY   attrsZfgcolorZbgcolorZboldZ	underlineZitalicZblinkreverseZhiddenpartsresultrA   rA   rT   rv      s(    





z_EscapeCodeCache.__missing__c             C   sT   yt |d}W n tk
r$    Y n,X |d? d@ }|d? d@ }|d@ }|||fS dS )z) Turn 'ffffff', into (0xff, 0xff, 0xff). rt   r?   rn   N)int
ValueError)rY   colorrgbrI   rJ   rK   rA   rA   rT   _color_name_to_rgb  s    z#_EscapeCodeCache._color_name_to_rgbc                sH   dg fdd}g }| |d | | d ttj|S )zB Return a tuple with the vt100 values  that represent this color. rA   c       
         s   |rt nt}| rjtjkr dS | |kr2||  fS y| }W n tk
rT   dS X jtjkr|r krzd f}nd}tj	||d\}}|fS t
	|\}}|d< |fS nBjtjkr|\}}}	|rdndd|||	fS |rdnddt| fS d S )NrA   r   )rL   0   &   rB      )r_   r`   rx   r   DEPTH_1_BITr   r   DEPTH_4_BIT_16_bg_colorsr^   _16_fg_colorsDEPTH_24_BIT_256_colors)
r   rX   tabler   rL   ra   rO   rI   rJ   rK   )bg_colorfg_ansifg_colorrY   rA   rT   get.  s.    

z-_EscapeCodeCache._colors_to_code.<locals>.getFT)r   mapsix	text_type)rY   r   r   r   r   rA   )r   r   r   rY   rT   r   '  s    (z _EscapeCodeCache._colors_to_codeN)rb   rc   rd   re   rZ   rv   r   r   rA   rA   rA   rT   rw      s
   rw   c             C   sN   ddl }ddl}ttjrdndddddg}|| |j| |d |d fS )zw
    Get the size of this pseudo terminal.

    :param fileno: stdout.fileno()
    :returns: A (rows, cols) tuple.
    r   N   hhr   )fcntltermiosarrayr   ZPY2ZioctlZ
TIOCGWINSZ)filenor   r   ZbufrA   rA   rT   	_get_size]  s
    r   c               @   s  e Zd ZdZe ZdCddZedDd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"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- ZdEd/d0Zd1d2 Zd3d4 Zd5d6 Zd7d8 Z d9d: Z!d;d< Z"d=d> Z#d?d@ Z$dAdB Z%dS )Fr	   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 write_binary: Encode the output before writing it. If `True` (the
        default), the `stdout` object is supposed to expose an `encoding` attribute.
    NTc          	      s   t |st|d ks$t|tjs$tt fdddD s>t|rPt dsPtg | _ | _|| _	|| _
|pnd| _tjttjtjttjtjttjtjttji| _d S )Nc             3   s   | ]}t  |V  qd S )N)hasattr).0a)stdoutrA   rT   	<genexpr>  s    z(Vt100_Output.__init__.<locals>.<genexpr>)writeflushencodingZxterm)callablerE   rC   r   r   allr   _bufferr   write_binaryget_sizetermr   r   rw   r   ZDEPTH_8_BITr   _escape_code_caches)rY   r   r   r   r   rA   )r   rT   rZ     s    
zVt100_Output.__init__c                sZ       } s>|| jkr>d}tj||  | j|  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.)
        z.Warning: Output is not to a terminal (fd=%r).
c                 s0    r t  \} }t| |dS tdddS d S )N)rowscolumns   P   )r   r   r   )r   r   )isattyr   rA   rT   r     s    z'Vt100_Output.from_pty.<locals>.get_size)r   )r   r   _fds_not_a_terminalsysstderrr   add)clsr   r   fdmsgr   rA   )r   r   rT   from_pty  s    
zVt100_Output.from_ptyc             C   s
   | j  S )z Return file descriptor. )r   r   )rY   rA   rA   rT   r     s    zVt100_Output.filenoc             C   s   | j jS )z" Return encoding used for stdout. )r   r   )rY   rA   rA   rT   r     s    zVt100_Output.encodingc             C   s   | j | dS )z+
        Write raw data to output.
        N)r   rp   )rY   datarA   rA   rT   	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   rp   replace)rY   r   rA   rA   rT   r     s    zVt100_Output.writec             C   s,   | j dkr(| d|dddd  dS )z%
        Set terminal title.
        )Zlinuxzeterm-colorz]2;%sr    N)r   r   r   )rY   titlerA   rA   rT   	set_title  s    
zVt100_Output.set_titlec             C   s   |  d d S )Nr   )r   )rY   rA   rA   rT   clear_title  s    zVt100_Output.clear_titlec             C   s   |  d dS )zd
        Erases the screen with the background colour and moves the cursor to
        home.
        z[2JN)r   )rY   rA   rA   rT   erase_screen  s    zVt100_Output.erase_screenc             C   s   |  d d S )Nz[?1049h[H)r   )rY   rA   rA   rT   enter_alternate_screen  s    z#Vt100_Output.enter_alternate_screenc             C   s   |  d d S )Nz[?1049l)r   )rY   rA   rA   rT   quit_alternate_screen  s    z"Vt100_Output.quit_alternate_screenc             C   s"   |  d |  d |  d d S )Nz[?1000hz[?1015hz[?1006h)r   )rY   rA   rA   rT   enable_mouse_support  s    

z!Vt100_Output.enable_mouse_supportc             C   s"   |  d |  d |  d d S )Nz[?1000lz[?1015lz[?1006l)r   )rY   rA   rA   rT   disable_mouse_support  s    

z"Vt100_Output.disable_mouse_supportc             C   s   |  d dS )zY
        Erases from the current cursor position to the end of the current line.
        z[KN)r   )rY   rA   rA   rT   erase_end_of_line  s    zVt100_Output.erase_end_of_linec             C   s   |  d dS )zc
        Erases the screen from the current line down to the bottom of the
        screen.
        z[JN)r   )rY   rA   rA   rT   
erase_down  s    zVt100_Output.erase_downc             C   s   |  d d S )Nz[0m)r   )rY   rA   rA   rT   reset_attributes  s    zVt100_Output.reset_attributesc             C   s   | j | }| ||  dS )zW
        Create new style and output.

        :param attrs: `Attrs` instance.
        N)r   r   )rY   r   rx   Zescape_code_cacherA   rA   rT   set_attributes  s    
zVt100_Output.set_attributesc             C   s   |  d d S )Nz[?7l)r   )rY   rA   rA   rT   disable_autowrap  s    zVt100_Output.disable_autowrapc             C   s   |  d d S )Nz[?7h)r   )rY   rA   rA   rT   enable_autowrap  s    zVt100_Output.enable_autowrapc             C   s   |  d d S )Nz[?2004h)r   )rY   rA   rA   rT   enable_bracketed_paste  s    z#Vt100_Output.enable_bracketed_pastec             C   s   |  d d S )Nz[?2004l)r   )rY   rA   rA   rT   disable_bracketed_paste  s    z$Vt100_Output.disable_bracketed_paster   c             C   s   |  d||f  dS )z Move cursor position. z[%i;%iHN)r   )rY   rowcolumnrA   rA   rT   cursor_goto  s    zVt100_Output.cursor_gotoc             C   s0   |dkr
n"|dkr|  d n|  d|  d S )Nr   r   z[Az[%iA)r   )rY   amountrA   rA   rT   	cursor_up  s
    zVt100_Output.cursor_upc             C   s0   |dkr
n"|dkr|  d n|  d|  d S )Nr   r   z[Bz[%iB)r   )rY   r   rA   rA   rT   cursor_down%  s
    zVt100_Output.cursor_downc             C   s0   |dkr
n"|dkr|  d n|  d|  d S )Nr   r   z[Cz[%iC)r   )rY   r   rA   rA   rT   cursor_forward/  s
    zVt100_Output.cursor_forwardc             C   s0   |dkr
n"|dkr|  d n|  d|  d S )Nr   r   z[%iD)r   )rY   r   rA   rA   rT   cursor_backward7  s
    zVt100_Output.cursor_backwardc             C   s   |  d d S )Nz[?25l)r   )rY   rA   rA   rT   hide_cursor?  s    zVt100_Output.hide_cursorc             C   s   |  d d S )Nz[?12l[?25h)r   )rY   rA   rA   rT   show_cursorB  s    zVt100_Output.show_cursorc          
   C   s   | j s
dS d| j }yX| jrVt| jdr4| jj}n| j}||| jjpLdd n| j| | j	  W nR t
k
r } z4|jr|jd tjkrn|jr|jd dkrn W dd}~X Y nX g | _ dS )z3
        Write to output stream and flush.
        Nr   bufferzutf-8r   r   )r   r   r   r   r   r   r   encoder   r   IOErrorargserrnoZEINTR)rY   r   outerA   rA   rT   r   E  s$    
zVt100_Output.flushc             C   s   |  d |   dS )z:
        Asks for a cursor position report (CPR).
        z[6nN)r   r   )rY   rA   rA   rT   ask_for_cprq  s    
zVt100_Output.ask_for_cprc             C   s   |  d |   dS )z Sound bell. r   N)r   r   )rY   rA   rA   rT   bellx  s    
zVt100_Output.bell)NT)N)r   r   )&rb   rc   rd   re   setr   rZ   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   rA   rA   rA   rT   r	   x  sD   


,)rA   )!re   Z
__future__r   Zprompt_toolkit.layout.screenr   Zprompt_toolkit.outputr   Zprompt_toolkit.styles.baser   rx   r   Z	six.movesr   r   r   r   r   __all__r`   r_   rG   r   rE   rU   dictrV   rf   r   r   r   rw   r   r	   rA   rA   rA   rT   <module>   s   
""A

h