B
    `aù[*  ã               @   sj   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G dd	„ d	ƒZ	G d
d„ deƒZ
G dd„ de
ƒZdS )a¿  
    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-2017 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
é    N)Ú	Formatter)Úcodes)Ú
ansicolorsÚTerminal256FormatterÚTerminalTrueColorFormatterc               @   s6   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚEscapeSequenceNFc             C   s   || _ || _|| _|| _d S )N)ÚfgÚbgÚboldÚ	underline)Úselfr   r	   r
   r   © r   ú>lib/python3.7/site-packages/pygments/formatters/terminal256.pyÚ__init__&   s    zEscapeSequence.__init__c             C   s   t |ƒrdd |¡ d S dS )Nz[ú;ÚmÚ )ÚlenÚjoin)r   Úattrsr   r   r   Úescape,   s    zEscapeSequence.escapec             C   sè   g }| j d k	rb| j tkrLt| j dd …  }d|kr8d| _| |dd… ¡ n| ddd| j  f¡ | jd k	r¾| jtkr¨t| jdd …  }| tt|dd… ƒd	 ƒ¡ n| d
dd| j f¡ | jrÎ| d¡ | j	rÞ| d¡ |  
|¡S )Né   z;01mTé   é   Ú38Ú5z%ié
   Ú48Ú01Ú04)r   r   r   r
   ÚappendÚextendr	   ÚstrÚintr   r   )r   r   Zescr   r   r   Úcolor_string1   s$    



 

zEscapeSequence.color_stringc          	   C   sž   g }| j r<| ddt| j d ƒt| j d ƒt| j d ƒf¡ | jrt| ddt| jd ƒt| jd ƒt| jd ƒf¡ | jr„| d¡ | jr”| d¡ |  |¡S )	Nr   Ú2r   é   r   r   r   r   )r   r!   r"   r	   r
   r    r   r   )r   r   r   r   r   Útrue_color_stringI   s    22

z EscapeSequence.true_color_stringc             C   sL   g }| j d k	r| d¡ | jd k	r,| d¡ | js8| jrB| d¡ |  |¡S )NZ39Z49Z00)r   r    r	   r
   r   r   )r   r   r   r   r   Úreset_stringU   s    




zEscapeSequence.reset_string)NNFF)Ú__name__Ú
__module__Ú__qualname__r   r   r$   r'   r(   r   r   r   r   r   %   s
   
r   c               @   sZ   e Zd ZdZdZdddgZg Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )r   aO  
    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.

    Options accepted:

    `style`
        The style to use, can be a string or a Style subclass (default:
        ``'default'``).
    ZTerminal256Zterminal256Z
console256Z256c             K   sH   t j| f|Ž g | _i | _i | _d|k| _d|k| _|  ¡  |  ¡  d S )NZnoboldZnounderline)	r   r   Úxterm_colorsÚ
best_matchÚstyle_stringÚuseboldÚuseunderlineÚ_build_color_tableÚ_setup_styles)r   Zoptionsr   r   r   r   {   s    

zTerminal256Formatter.__init__c             C   sL  | 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}xNtdƒD ]B}||d d  }||d d  }||d  }| j  |||f¡ qÎW x2tddƒD ]$}d|d  }| j  |||f¡ q W d S )N)r   r   r   )éÍ   r   r   )r   r3   r   )r3   r3   r   )r   r   éî   )r3   r   r3   )r   r3   r3   )éå   r5   r5   )é   r6   r6   )éÿ   r   r   )r   r7   r   )r7   r7   r   )é\   r8   r7   )r7   r   r7   )r   r7   r7   )r7   r7   r7   )r   é_   é‡   é¯   é×   r7   éÙ   é$   é   r&   é   é   r   )r,   r    Úrange)r   Z
valuerangeÚiÚrÚgÚbÚvr   r   r   r1   ˆ   s2    z'Terminal256Formatter._build_color_tablec             C   sv   d}d}xht ddƒD ]Z}| j| }||d  }||d  }	||d  }
|| |	|	  |
|
  }||k r|}|}qW |S )Ni r   éþ   r&   r   )rB   r,   )r   rD   rE   rF   ZdistanceÚmatchrC   ÚvaluesZrdZgdZbdÚdr   r   r   Ú_closest_color¬   s    
z#Terminal256Formatter._closest_colorc             C   s”   | j  |d ¡}|tkr$|}|| j |< |d krytt|ƒdƒ}W n tk
rV   d}Y nX |d? d@ }|d? d@ }|d@ }|  |||¡}|| j |< |S )Né   r   r7   rA   )r-   Úgetr   r#   r"   Ú
ValueErrorrL   )r   ÚcolorÚindexÚrgbrD   rE   rF   r   r   r   Ú_color_index½   s    


z!Terminal256Formatter._color_indexc             C   sÄ   x¾| j D ]´\}}tƒ }|d r0|  |d ¡|_n|d rH|  |d ¡|_|d rb|  |d ¡|_n|d rz|  |d ¡|_| jrŽ|d rŽd|_| jr¢|d r¢d|_| 	¡ | 
¡ f| jt|ƒ< qW d S )NZ	ansicolorrP   ZbgansicolorÚbgcolorr
   Tr   )Ústyler   rS   r   r	   r/   r
   r0   r   r$   r(   r.   r"   )r   ÚttypeÚndefr   r   r   r   r2   Ð   s     z"Terminal256Formatter._setup_stylesc             C   sB   | j s4t|dƒr4t|dƒr4| ¡ r4tjdk r4|j | _ t | ||¡S )NÚencodingÚisatty)é   )rX   ÚhasattrrY   ÚsysÚversion_infor   Úformat)r   ÚtokensourceÚoutfiler   r   r   r^   ã   s
    
zTerminal256Formatter.formatc       
   	   C   sÐ   xÊ|D ]Â\}}d}x¦|r¸|r¸yx| j t|ƒ \}}| d¡}x4|d d… D ]$}	|	rb| ||	 | ¡ | d¡ qHW |d rŽ| ||d  | ¡ d}W q tk
r´   |d d… }Y qX qW |r| |¡ qW d S )NTÚ
éÿÿÿÿF)r.   r"   ÚsplitÚwriteÚKeyError)
r   r_   r`   rV   ÚvalueZ	not_foundZonZoffZsplÚliner   r   r   Úformat_unencodedì   s"    

z%Terminal256Formatter.format_unencodedN)r)   r*   r+   Ú__doc__ÚnameÚaliasesÚ	filenamesr   r1   rL   rS   r2   r^   rh   r   r   r   r   r   `   s   
$	c               @   s:   e Zd ZdZdZdddgZg Zdd„ Zdd	„ Zd
d„ Z	dS )r   a}  
    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'``).
    ZTerminalTrueColorZterminal16mZ
console16mZ16mc             C   s   d S )Nr   )r   r   r   r   r1     s    z-TerminalTrueColorFormatter._build_color_tablec             C   sR   yt t|ƒdƒ}W n tk
r&   d S X |d? d@ }|d? d@ }|d@ }|||fS )NrM   r7   rA   )r#   r"   rO   )r   rP   rR   rD   rE   rF   r   r   r   Ú_color_tuple  s    z'TerminalTrueColorFormatter._color_tuplec             C   s   xŠ| j D ]€\}}tƒ }|d r.|  |d ¡|_|d rF|  |d ¡|_| jrZ|d rZd|_| jrn|d rnd|_| 	¡ | 
¡ f| jt|ƒ< qW d S )NrP   rT   r
   Tr   )rU   r   rm   r   r	   r/   r
   r0   r   r'   r(   r.   r"   )r   rV   rW   r   r   r   r   r2   )  s    z(TerminalTrueColorFormatter._setup_stylesN)
r)   r*   r+   ri   rj   rk   rl   r1   rm   r2   r   r   r   r   r   
  s   

)ri   r\   Zpygments.formatterr   Zpygments.consoler   Zpygments.styler   Ú__all__r   r   r   r   r   r   r   Ú<module>   s   ; +