
    L                        d 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dlmZ ddlZddlZddlZddlZd	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d-dd.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<Zi d
d=dd=dd>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Z ee       ee      k(  sJ  ee       ee      k(  sJ  ee       ee      k(  sJ dZdMZ G dN dOe      Z G dP dQe      Z edRS      Z edTS      Z e       Z G dU dVe      ZdW Z G dX dYe	      Z y)[z
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)to_simple_filter	Condition)Size)Output)ANSI_COLOR_NAMES)rangeN)Vt100_Outputansidefault'   	ansiblack   ansidarkred   ansidarkgreen    	ansibrown!   ansidarkblue"   
ansipurple#   ansiteal$   ansilightgray%   ansidarkgrayZ   ansired[   	ansigreen\   
ansiyellow]   ansiblue^   ansifuchsia_   ansiturquoise`   	ansiwhitea   1   (   )   *   +   ,   -   .   /   d   e   f   g   h   i   j   k   r   r   r      r@   r@      rB   rB      rD   rD      r   r   r   rF   r   rF   rF   r   )r   r   rF   rF   r   rF   r   rF   rF   rB   r   r   r   rB   r   rB   rB   r   )r   r   rB   rB   r   rB   r   rB   rB   c                 <   t        |t              sJ t        | |z
        t        ||z
        z   t        || z
        z   }|dkD  r|dz  }d}d}t        j	                         D ]8  \  }\  }}	}
|dk7  s||vs| |z
  dz  ||	z
  dz  z   ||
z
  dz  z   }||k  s5|}|}: |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   )r   r   r+   r    r      )
isinstancetupleabsANSI_COLORS_TO_RGBitems)rgbexclude
saturationdistancematchnamer2g2b2ds               7lib/third_party/prompt_toolkit/terminal/vt100_output.py_get_closest_ansi_colorre   j   s     gu%%% QUc!a%j(3q1u:5JBNN HE0668lr2r= T%8RARA-RA=A8| 9 L    c                   (    e Zd ZdZddZddZddZy)_16ColorCachez
    Cache which maps (r, g, b) tuples to 16 ansi colors.

    :param bg: Cache for background colors, instead of foreground.
    c                 6    t        |t              sJ || _        y N)rS   boolbg)selfrl   s     rd   __init__z_16ColorCache.__init__   s    "d###rf   c                 F    ||f}|| vr| j                  ||      | |<   | |   S )zx
        Return a (ansi_code, ansi_name) tuple. (E.g. ``(44, 'ansiblue')``.) for
        a given (r,g,b) value.
        )_get)rm   valuer[   keys       rd   get_codez_16ColorCache.get_code   s3    
 gd?		%1DICyrf   c                 |    |\  }}}t        ||||      }| j                  r
t        |   }n	t        |   }|| |<   ||fS )Nr[   )re   rl   BG_ANSI_COLORSFG_ANSI_COLORS)rm   rq   r[   rX   rY   rZ   r^   codes           rd   rp   z_16ColorCache._get   sM    1a'1aA 77!%(D!%(DUU{rf   N)F )__name__
__module____qualname____doc__rn   rs   rp   rz   rf   rd   rh   rh      s    
rf   rh   c                       e Zd ZdZd Zd Zy)_256ColorCachez9
    Cach which maps (r, g, b) tuples to 256 colors.
    c                    g }|j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d	       |j                  d
       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       d}t        d      D ]4  }||dz  dz     }||dz  dz     }||dz     }|j                  |||f       6 t        dd      D ]  }d|dz  z   }|j                  |||f         || _        y )Nr>   rE   rG   rH   )r   r      rI   rJ   rC   r?   rK   rL   rM   )r"   r"   rB   rN   rO   rA   )r   r(            rB      r               
   )appendr	   colors)rm   r   
valuerangeirX   rY   rZ   vs           rd   rn   z_256ColorCache.__init__   sl    	()()()()()()()()()()()()()()()() :
sAAGq=)AAFa<(A1q5!AMM1a)$	  q"AAF
AMM1a)$  rf   c                     |\  }}}d}d}t        | j                        D ]-  \  }\  }}	}
||z
  dz  ||	z
  dz  z   ||
z
  dz  z   }||k  s*|}|}/ || |<   |S )NrQ   r   rR   )	enumerater   )rm   rq   rX   rY   rZ   r]   r^   r   r`   ra   rb   rc   s               rd   __missing__z_256ColorCache.__missing__   s~    1a (5OA|BRARA-RA=A8|  6 Urf   N)r{   r|   r}   r~   rn   r   rz   rf   rd   r   r      s    $Lrf   r   F)rl   Tc                   *    e Zd ZdZddZd Zd Zd Zy)_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                 V    t        |t              sJ || _        t        |      | _        y rj   )rS   rk   
true_colorr   ansi_colors_only)rm   r   r   s      rd   rn   z_EscapeCodeCache.__init__   s'    *d+++$ 01A Brf   c                 `   |\  }}}}}}}g }	|	j                  | j                  ||             |r|	j                  d       |r|	j                  d       |r|	j                  d       |r|	j                  d       |r|	j                  d       |	rddj                  |	      z   dz   }
nd	}
|
| |<   |
S )
N13547z[0;;m[0m)extend_colors_to_coder   join)rm   attrsfgcolorbgcolorbold	underlineitalicblinkreversepartsresults              rd   r   z_EscapeCodeCache.__missing__   s    DIA$	65'T))'7;<LLLLLLLLLL%036FFUrf   c                 l    	 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).    rB   r   )int
ValueError)rm   colorrgbrX   rY   rZ   s         rd   _color_name_to_rgbz#_EscapeCodeCache._color_name_to_rgb  sX    	eR.C d"AT!Ad
Aa7N  		s   ( 3c                      dg fd}g }|j                   |d             |j                   |d             t        t        j                  |      S )zB Return a tuple with the vt100 values  that represent this color. rz   c                    |rt         nt        }| y| |v r||    fS 	 j                  |       }j	                         rM|r+
k7  rd   f}nd}t
        j                  ||      \  }}|fS t        j                  |      \  }}|d<   |fS j                  r|\  }}}	|rdndd|||	fS |rdnddt        |   fS # t        $ r Y yw xY w)Nrz   r   ru   0   &   rR      )
rv   rw   r   r   r   _16_bg_colorsrs   _16_fg_colorsr   _256_colors)r   rl   tabler   r[   rx   r_   rX   rY   rZ   bg_colorfg_ansifg_colorrm   s             rd   getz-_EscapeCodeCache._colors_to_code.<locals>.get)  s   &(NnE} %e''11%8C
 ((*#x/'.qznG&(G%2%;%;C%;%Q
d $x%2%;%;C%@
d%)
 $x __!GAq!"$B"aAq99 #%B"aS1ABB1 " s   B9 9	CCFT)r   mapsix	text_type)rm   r   r   r   r   r   s   ```  @rd   r   z _EscapeCodeCache._colors_to_code"  sQ    
 $&	CP c(E*+c(D)*3==&))rf   N)FF)r{   r|   r}   r~   rn   r   r   r   rz   rf   rd   r   r      s    C
2
3*rf   r   c                     ddl }ddl}t        j                  t        j                  rdndg d      }|j                  | |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h)r   r   r   r   r   )fcntltermiosarrayr   PY2ioctl
TIOCGWINSZ)filenor   r   bufs       rd   	_get_sizer   X  sP      ++cggd4
>C 
KK**C0 q63q6>rf   c                       e Zd ZdZ	 	 d"dZed#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 Zd Zd Zd Zd Zd Zd Zd$dZd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#y)%r
   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 true_color: Use 24bit color instead of 256 colors. (Can be a :class:`SimpleFilter`.)
        When `ansi_colors_only` is set, only 16 colors are used.
    :param ansi_colors_only: Restrict to 16 ANSI colors only.
    :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.
    Nc                    t        |      sJ t        t        j                        sJ t	        fddD              sJ |rt        d      sJ g | _        | _        || _        || _	        t        |      | _        xs d| _        |9t        t        j                  j!                  dd            t"        fd       }nt        |      }|| _        t'        |      | _        t'        d	|
      | _        y )Nc              3   6   K   | ]  }t        |        y wrj   )hasattr).0astdouts     rd   	<genexpr>z(Vt100_Output.__init__.<locals>.<genexpr>  s     B/A!761%/As   )writeflushencodingxtermPROMPT_TOOLKIT_ANSI_COLORS_ONLYFc                       xs dv S )Nlinuxzeterm-colorrz   )ANSI_COLORS_ONLYterms   rd   r   z/Vt100_Output.__init__.<locals>.ansi_colors_only  s    'K43K+KKrf   )r   T)r   r   )callablerS   r   r   allr   _bufferr   write_binaryget_sizer   r   r   rk   osenvironr   r   r   r   _escape_code_cache_escape_code_cache_true_color)rm   r   r   r   r   r   r   r   s    `   ` @rd   rn   zVt100_Output.__init__~  s    !!!|z$>>>B/ABBBB6:...( *:6OG	 ##BJJNN15%:  ; L L  00@A 0 #3DT"U-=.>.@*rf   c                 L    j                         sJ fd} | ||||      S )z
        Create an Output class from a pseudo terminal.
        (This will take the dimensions by reading the pseudo
        terminal attributes.)
        c                  V    t        j                               \  } }t        | |      S )N)rowscolumns)r   r   r   )r   r   r   s     rd   r   z'Vt100_Output.from_pty.<locals>.get_size  s$    %fmmo6MD'T733rf   )r   r   r   )isatty)clsr   r   r   r   r   s    `    rd   from_ptyzVt100_Output.from_pty  s4     }}	4 68
$44A 	Arf   c                 6    | j                   j                         S )z Return file descriptor. )r   r   rm   s    rd   r   zVt100_Output.fileno  s    {{!!##rf   c                 .    | j                   j                  S )z" Return encoding used for stdout. )r   r   r   s    rd   r   zVt100_Output.encoding  s    {{###rf   c                 :    | j                   j                  |       y)z+
        Write raw data to output.
        N)r   r   rm   datas     rd   	write_rawzVt100_Output.write_raw  s     	D!rf   c                 Z    | j                   j                  |j                  dd             y)zn
        Write text to output.
        (Removes vt100 escape codes. -- used for safely writing text.)
        ?N)r   r   replacer   s     rd   r   zVt100_Output.write  s!    
 	DLL56rf   c                     | j                   dvr5| j                  d|j                  dd      j                  dd      z         yy)z%
        Set terminal title.
        r   z]2;%sr    N)r   r   r   )rm   titles     rd   	set_titlezVt100_Output.set_title  s@     9944NN?U]]62-F-N-NvWY-ZZ[ 5rf   c                 &    | j                  d       y )Nr   )r  r   s    rd   clear_titlezVt100_Output.clear_title  s    rrf   c                 &    | j                  d       y)zd
        Erases the screen with the background colour and moves the cursor to
        home.
        z[2JNr   r   s    rd   erase_screenzVt100_Output.erase_screen  s    
 	y!rf   c                 &    | j                  d       y )Nz[?1049h[Hr  r   s    rd   enter_alternate_screenz#Vt100_Output.enter_alternate_screen  s    *+rf   c                 &    | j                  d       y )Nz[?1049lr  r   s    rd   quit_alternate_screenz"Vt100_Output.quit_alternate_screen      }%rf   c                 j    | j                  d       | j                  d       | j                  d       y )Nz[?1000hz[?1015hz[?1006hr  r   s    rd   enable_mouse_supportz!Vt100_Output.enable_mouse_support  s*    }% 	}% 	}%rf   c                 j    | j                  d       | j                  d       | j                  d       y )Nz[?1000lz[?1015lz[?1006lr  r   s    rd   disable_mouse_supportz"Vt100_Output.disable_mouse_support  s&    }%}%}%rf   c                 &    | j                  d       y)zY
        Erases from the current cursor position to the end of the current line.
        z[KNr  r   s    rd   erase_end_of_linezVt100_Output.erase_end_of_line  s     	x rf   c                 &    | j                  d       y)zc
        Erases the screen from the current line down to the bottom of the
        screen.
        z[JNr  r   s    rd   
erase_downzVt100_Output.erase_down  s    
 	x rf   c                 &    | j                  d       y )Nr   r  r   s    rd   reset_attributeszVt100_Output.reset_attributes  s    y!rf   c                     | j                         r/| j                         s| j                  | j                  |          y| j                  | j                  |          y)zW
        Create new style and output.

        :param attrs: `Attrs` instance.
        N)r   r   r   r   r   )rm   r   s     rd   set_attributeszVt100_Output.set_attributes  sG     ??T%:%:%<NN4==eDENN42259:rf   c                 &    | j                  d       y )Nz[?7lr  r   s    rd   disable_autowrapzVt100_Output.disable_autowrap      z"rf   c                 &    | j                  d       y )Nz[?7hr  r   s    rd   enable_autowrapzVt100_Output.enable_autowrap  r  rf   c                 &    | j                  d       y )Nz[?2004hr  r   s    rd   enable_bracketed_pastez#Vt100_Output.enable_bracketed_paste  r  rf   c                 &    | j                  d       y )Nz[?2004lr  r   s    rd   disable_bracketed_pastez$Vt100_Output.disable_bracketed_paste  r  rf   c                 0    | j                  d||fz         y)z Move cursor position. z[%i;%iHNr  )rm   rowcolumns      rd   cursor_gotozVt100_Output.cursor_goto  s    }V}45rf   c                 f    |dk(  ry |dk(  r| j                  d       y | j                  d|z         y )Nr   r   z[Az[%iAr  rm   amounts     rd   	cursor_upzVt100_Output.cursor_up  0    Q;q[NN8$NN:./rf   c                 f    |dk(  ry |dk(  r| j                  d       y | j                  d|z         y )Nr   r   z[Bz[%iBr  r(  s     rd   cursor_downzVt100_Output.cursor_down   s2    Q;q[ NN8$NN:./rf   c                 f    |dk(  ry |dk(  r| j                  d       y | j                  d|z         y )Nr   r   z[Cz[%iCr  r(  s     rd   cursor_forwardzVt100_Output.cursor_forward*  r+  rf   c                 f    |dk(  ry |dk(  r| j                  d       y | j                  d|z         y )Nr   r   z[%iDr  r(  s     rd   cursor_backwardzVt100_Output.cursor_backward2  s0    Q;q[NN4 NN:./rf   c                 &    | j                  d       y )Nz[?25lr  r   s    rd   hide_cursorzVt100_Output.hide_cursor:  s    {#rf   c                 &    | j                  d       y )Nz[?12l[?25hr  r   s    rd   show_cursorzVt100_Output.show_cursor=  s    +,rf   c                    | j                   sydj                  | j                         }	 | j                  rst        | j                  d      r| j                  j
                  }n| j                  }|j                  |j                  | j                  j                  xs dd             n| j                  j                  |       | j                  j                          g | _         y# t        $ r^}|j                  r!|j                  d   t        j                  k(  rn |j                  r|j                  d   dk(  rn Y d}~g | _         yd}~ww xY w)z3
        Write to output stream and flush.
        Nr   bufferzutf-8r   r   )r   r   r   r   r   r8  r   encoder   r   IOErrorargserrnoEINTR)rm   r   outes       rd   r   zVt100_Output.flush@  s     ||wwt||$	   4;;1++,,C++C		$++dkk&:&:&EgyQR!!$'KK$ #  	vv!&&)u{{2 AFF1IN #	s   B4C& &	E/AEEc                 F    | j                  d       | j                          y)z:
        Asks for a cursor position report (CPR).
        z[6nNr   r   r   s    rd   ask_for_cprzVt100_Output.ask_for_cprl  s     	y!

rf   c                 F    | j                  d       | j                          y)z Sound bell. r  NrA  r   s    rd   bellzVt100_Output.bells  s    t

rf   )FNNT)FNN)r   r   )$r{   r|   r}   r~   rn   classmethodr   r   r   r   r   r  r  r  r
  r  r  r  r  r  r  r  r  r  r   r"  r&  r*  r-  r/  r2  r4  r6  r   rB  rD  rz   rf   rd   r
   r
   s  s    	 5:@D!@F A A$$"7\",&&&
!!"	;##&&60000$-*Xrf   r
   ry   )!r~   
__future__r   prompt_toolkit.filtersr   r   prompt_toolkit.layout.screenr   prompt_toolkit.rendererr   prompt_toolkit.stylesr   	six.movesr	   r   r<  r   r   __all__rw   rv   rV   setre   dictrh   r   r   r   r   r   r   r
   rz   rf   rd   <module>rO     sb   ( > - * 2    	 

2 r	
 r r r r r r r r r  r!" r#$ r%& r'( r)* r+0r r	
 r r r r r r r s s  s!" s#$ s%& s'( s)* s+2'' ' '	
 ' ) ) ) ) ) )  )!" )#$ )%& )'( ))* )+ 2 >c"233 33
>c"233 33
#&6"77 77DD D;T ;| '&d*t d*N6C6 Crf   