
l2RTc           @   sX   d  Z  d d l Z d d l m Z d g Z d d 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-2014 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iN(   t	   Formattert   Terminal256Formattert   EscapeSequencec           B   s8   e  Z d d e e 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   (    (    sC   /usr/lib/python2.7/dist-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(    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyt   escape*   s    c         C   s   g  } |  j  d  k	 r5 | j d d d |  j  f  n  |  j d  k	 rd | j d d d |  j f  n  |  j r} | j d  n  |  j r | j d  n  |  j |  S(   Nt   38t   5s   %it   48t   01t   04(   R   t   Nonet   extendR   R   t   appendR   R   (   R   R   (    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyt   color_string/   s      		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   (    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyt   reset_string;   s    N(   t   __name__t
   __module__R   t   FalseR   R   R   R   (    (    (    sC   /usr/lib/python2.7/dist-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(   sX  
    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

    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(    (    sC   /usr/lib/python2.7/dist-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   (   R&   R   t   range(   R   t
   valueranget   it   rt   gt   bt   v(    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyR+   i   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 (   R.   R&   (   R   R1   R2   R3   t   distancet   matchR0   t   valuest   rdt   gdt   bdt   d(    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyt   _closest_color   s    c         C   s   |  j  j | d   } | d  k r y t t |  d  } Wn t k
 rS d } n X| d ?d @} | d ?d @} | d @} |  j | | |  } | |  j  | <n  | S(   Ni   i    i   i   (   R'   t   getR   t   intt   strt
   ValueErrorR<   (   R   t   colort   indext   rgbR1   R2   R3   (    (    sC   /usr/lib/python2.7/dist-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  |  j r | d r t | _ n  |  j r | d r t | _	 n  | j
   | j   f |  j t |  <q
 Wd  S(   NRA   t   bgcolorR   R   (   t   styleR   RD   R   R   R)   t   TrueR   R*   R   R   R   R(   R?   (   R   t   ttypet   ndefR   (    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyR,      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   (   RJ   t   hasattrRK   t   syst   version_infoR    t   format(   R   t   tokensourcet   outfile(    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyRO      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(   RG   R(   R?   t   splitt   writeR   t   KeyError(
   R   RP   RQ   RH   t   valuet	   not_foundt   ont   offt   splt   line(    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyt   format_unencoded   s"    

(   R   R   t   __doc__t   namet   aliasest	   filenamesR   R+   R<   RD   R,   RO   R[   (    (    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyR   F   s   		$					(    (   R\   RM   t   pygments.formatterR    t   __all__R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/pygments/formatters/terminal256.pyt   <module>   s
   	#