
xi\c           @   s   d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d g Z d d d	     YZ	 d e f d
     YZ
 d e
 f d     YZ d S(   s  
    pygments.formatters.terminal256
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Formatter for 256-color terminal output with ANSI sequences.

    RGB-to-XTERM color conversion routines adapted from xterm256-conv
    tool (http://frexx.de/xterm-256-notes/data/xterm256-conv2.tar.bz2)
    by Wolfgang Frisch.

    Formatter version 1.

    :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iN(   t	   Formatter(   t   codes(   t
   ansicolorst   Terminal256Formattert   TerminalTrueColorFormattert   EscapeSequencec           B   sA   e  Z d d e e d   Z d   Z d   Z d   Z d   Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   fgt   bgt   boldt	   underline(   t   selfR   R   R   R	   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   __init__&   s    			c         C   s%   t  |  r! d d j |  d Sd S(   Ns   [t   ;t   mt    (   t   lent   join(   R
   t   attrs(    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   escape,   s    c         C   sI  g  } |  j  d  k	 r |  j  t k rl t |  j  j d d  } d | k rU t |  _ n  | j | d d ! q | j d d d |  j  f  n  |  j	 d  k	 r
|  j	 t k r t |  j	 j d d  } | j t
 t | d d ! d	   q
| j d
 d d |  j	 f  n  |  j r#| j d  n  |  j r<| j d  n  |  j |  S(   Nt   ansiR   s   ;01mi   i   t   38t   5s   %ii
   t   48t   01t   04(   R   t   NoneR   R   t   replacet   TrueR   t   appendt   extendR   t   strt   intR	   R   (   R
   R   t   esc(    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   color_string1   s$     ' 		c         C   s   g  } |  j  rU | j d d t |  j  d  t |  j  d  t |  j  d  f  n  |  j r | j d d t |  j d  t |  j d  t |  j d  f  n  |  j r | j d  n  |  j r | j d  n  |  j |  S(	   NR   t   2i    i   i   R   R   R   (   R   R   R   R   R   R   R	   R   (   R
   R   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   true_color_stringI   s    	F	F		c         C   ss   g  } |  j  d  k	 r% | j d  n  |  j d  k	 rD | j d  n  |  j sV |  j rf | j d  n  |  j |  S(   Nt   39t   49t   00(   R   R   R   R   R   R	   R   (   R
   R   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   reset_stringU   s    N(	   t   __name__t
   __module__R   t   FalseR   R   R!   R#   R'   (    (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR   %   s
   			c           B   sh   e  Z d  Z d Z d d d g Z g  Z d   Z d   Z d   Z d   Z	 d	   Z
 d
   Z d   Z RS(   sP  
    Format tokens with ANSI color sequences, for output in a 256-color
    terminal or console.  Like in `TerminalFormatter` color sequences
    are terminated at newlines, so that paging the output works correctly.

    The formatter takes colors from a style defined by the `style` option
    and converts them to nearest ANSI 256-color escape sequences. Bold and
    underline attributes from the style are preserved (and displayed).

    .. versionadded:: 0.9

    .. versionchanged:: 2.2
       If the used style defines foreground colors in the form ``#ansi*``, then
       `Terminal256Formatter` will map these to non extended foreground color.
       See :ref:`AnsiTerminalStyle` for more information.

    .. versionchanged:: 2.4
       The ANSI color names have been updated with names that are easier to
       understand and align with colornames of other projects and terminals.
       See :ref:`this table <new-ansi-color-names>` for more information.


    Options accepted:

    `style`
        The style to use, can be a string or a Style subclass (default:
        ``'default'``).
    t   Terminal256t   terminal256t
   console256t   256c         K   sa   t  j |  |  g  |  _ i  |  _ i  |  _ d | k |  _ d | k |  _ |  j   |  j   d  S(   Nt   noboldt   nounderline(	   R    R   t   xterm_colorst
   best_matcht   style_stringt   useboldt   useunderlinet   _build_color_tablet   _setup_styles(   R
   t   options(    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR      s    			
c         C   s  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d  |  j  j d   |  j  j d!  |  j  j d"  d# } x_ t d  D]Q } | | d d } | | d d } | | d } |  j  j | | | f  qWx> t d d  D]- } d | d } |  j  j | | | f  qxWd  S($   Ni    i   i   i   i   i   i\   i_   i   i   i   i   i$   i   i   i   i   i
   (   i    i    i    (   i   i    i    (   i    i   i    (   i   i   i    (   i    i    i   (   i   i    i   (   i    i   i   (   i   i   i   (   i   i   i   (   i   i    i    (   i    i   i    (   i   i   i    (   i\   i\   i   (   i   i    i   (   i    i   i   (   i   i   i   (   i    i_   i   i   i   i   (   R1   R   t   range(   R
   t
   valueranget   it   rt   gt   bt   v(    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR6      s2    c         C   s   d } d } x t  d d  D]r } |  j | } | | d } | | d }	 | | d }
 | | |	 |	 |
 |
 } | | k  r | } | } q q W| S(	   Ni  i   i    i   i   i   i i (   R9   R1   (   R
   R<   R=   R>   t   distancet   matchR;   t   valuest   rdt   gdt   bdt   d(    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   _closest_color   s    c         C   s   |  j  j | d   } | t k r7 | } | |  j  | <n  | d  k r y t t |  d  } Wn t k
 ru d } n X| d ?d @} | d ?d @} | d @} |  j | | |  } | |  j  | <n  | S(   Ni   i    i   i   (   R2   t   getR   R   R   R   t
   ValueErrorRG   (   R
   t   colort   indext   rgbR<   R=   R>   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   _color_index   s    

c         C   s  x|  j  D]\ } } t   } | d rB |  j | d  | _ n# | d re |  j | d  | _ n  | d r |  j | d  | _ n# | d r |  j | d  | _ n  |  j r | d r t | _ n  |  j r | d r t | _	 n  | j
   | j   f |  j t |  <q
 Wd  S(   Nt	   ansicolorRJ   t   bgansicolort   bgcolorR   R	   (   t   styleR   RM   R   R   R4   R   R   R5   R	   R!   R'   R3   R   (   R
   t   ttypet   ndefR   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR7      s     	



	c         C   se   |  j  rR t | d  rR t | d  rR | j   rR t j d k  rR | j  |  _  n  t j |  | |  S(   Nt   encodingt   isattyi   (   i   (   RT   t   hasattrRU   t   syst   version_infoR    t   format(   R
   t   tokensourcet   outfile(    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyRY      s
    c   
      C   s   x | D] \ } } t  } x | r | r y |  j t |  \ } } | j d  } x= | d  D]1 }	 |	 r | j | |	 |  n  | j d  q^ W| d r | j | | d |  n  t } Wq t k
 r | d  } q Xq W| r | j |  q q Wd  S(   Ns   
i(   R   R3   R   t   splitt   writeR*   t   KeyError(
   R
   RZ   R[   RR   t   valuet	   not_foundt   ont   offt   splt   line(    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   format_unencoded   s"    

(   R(   R)   t   __doc__t   namet   aliasest	   filenamesR   R6   RG   RM   R7   RY   Re   (    (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR   `   s   		$					c           B   sD   e  Z d  Z d Z d d d g Z g  Z d   Z d   Z d   Z RS(   s}  
    Format tokens with ANSI color sequences, for output in a true-color
    terminal or console.  Like in `TerminalFormatter` color sequences
    are terminated at newlines, so that paging the output works correctly.

    .. versionadded:: 2.1

    Options accepted:

    `style`
        The style to use, can be a string or a Style subclass (default:
        ``'default'``).
    t   TerminalTrueColort   terminal16mt
   console16mt   16mc         C   s   d  S(   N(    (   R
   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR6   "  s    c         C   sa   y t  t |  d  } Wn t k
 r- d  SX| d ?d @} | d ?d @} | d @} | | | f S(   Ni   i   i   (   R   R   RI   R   (   R
   RJ   RL   R<   R=   R>   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   _color_tuple%  s    
c         C   s   x |  j  D] \ } } t   } | d rB |  j | d  | _ n  | d re |  j | d  | _ n  |  j r | d r t | _ n  |  j r | d r t | _	 n  | j
   | j   f |  j t |  <q
 Wd  S(   NRJ   RP   R   R	   (   RQ   R   Rn   R   R   R4   R   R   R5   R	   R#   R'   R3   R   (   R
   RR   RS   R   (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR7   /  s    	

	(	   R(   R)   Rf   Rg   Rh   Ri   R6   Rn   R7   (    (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyR     s   		
(    (   Rf   RW   t   pygments.formatterR    t   pygments.consoleR   t   pygments.styleR   t   __all__R   R   R   (    (    (    s>   lib/python2.7/site-packages/pygments/formatters/terminal256.pyt   <module>   s   ;