3
.ßL]<+  ã               @   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-2019 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   úE/tmp/pip-install-5sebvq2j/pygments/pygments/formatters/terminal256.pyÚ__init__&   s    zEscapeSequence.__init__c             C   s   t |ƒrddj|ƒ 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 jddƒ }d|kr8d| _|j|dd… ƒ n|jddd	| j  fƒ | jd k	r¾| jtkr¨t| jjddƒ }|jtt	|dd… ƒd
 ƒƒ n|jddd	| j fƒ | jrÎ|jdƒ | j
rÞ|jdƒ | j|ƒS )NÚansir   z;01mTé   é   Ú38Ú5z%ié
   Ú48Ú01Ú04)r   r   r   Úreplacer
   ÚappendÚextendr	   ÚstrÚintr   r   )r   r   Úescr   r   r   Úcolor_string1   s$    



 

zEscapeSequence.color_stringc             C   sž   g }| j r<|jddt| j d ƒt| j d ƒt| j d ƒfƒ | jrt|jddt| jd ƒt| jd ƒt| jd ƒfƒ | jr„|jdƒ | jr”|jdƒ | j|ƒ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|jdƒ | jd k	r,|jdƒ | js8| jrB|jdƒ | j|ƒ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   aP  
    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'``).
    ZTerminal256Zterminal256Z
console256Z256c             K   sH   t j| f|Ž g | _i | _i | _d|k| _d|k| _| jƒ  | jƒ  d S )NZnoboldZnounderline)	r   r   Úxterm_colorsÚ
best_matchÚstyle_stringÚuseboldÚuseunderlineÚ_build_color_tableÚ_setup_styles)r   Úoptionsr   r   r   r   �   s    

zTerminal256Formatter.__init__c             C   sN  | 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#}xPtdƒD ]D}||d d  }||d d  }||d  }| j j|||fƒ � qÎW x2tddƒD ]$}d|d  }| j j|||fƒ �q"W d S )$Nr   éÍ   éî   éå   é   éÿ   é\   é_   é‡   é¯   é×   éÙ   é$   é   r(   é   é   r   )r   r   r   )r6   r   r   )r   r6   r   )r6   r6   r   )r   r   r7   )r6   r   r6   )r   r6   r6   )r8   r8   r8   )r9   r9   r9   )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!   Úrange)r   Z
valuerangeÚiÚrÚgÚbÚvr   r   r   r3   Ž   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   i i )rE   r.   )r   rG   rH   rI   ZdistanceÚmatchrF   ÚvaluesÚrdÚgdZbdÚdr   r   r   Ú_closest_color²   s    
z#Terminal256Formatter._closest_colorc             C   s”   | j j|d ƒ}|tkr$|}|| j |< |d kr�ytt|ƒdƒ}W n tk
rV   d}Y nX |d? d@ }|d? d@ }|d@ }| j|||ƒ}|| j |< |S )Né   r   r:   rD   )r/   Úgetr   r$   r#   Ú
ValueErrorrR   )r   ÚcolorÚindexÚrgbrG   rH   rI   r   r   r   Ú_color_indexÃ   s    


z!Terminal256Formatter._color_indexc             C   sÄ   x¾| j D ]´\}}tƒ }|d r0| j|d ƒ|_n|d rH| j|d ƒ|_|d rb| j|d ƒ|_n|d rz| j|d ƒ|_| jrŽ|d rŽd|_| jr¢|d r¢d|_|j	ƒ |j
ƒ f| jt|ƒ< qW d S )NZ	ansicolorrV   ZbgansicolorÚbgcolorr
   Tr   )Ústyler   rY   r   r	   r1   r
   r2   r   r&   r*   r0   r#   )r   ÚttypeÚndefr   r   r   r   r4   Ö   s     z"Terminal256Formatter._setup_stylesc             C   sD   | j  r6t|dƒr6t|dƒr6|jƒ r6tjdk r6|j | _ tj| ||ƒS )NÚencodingÚisattyrK   )rK   )r^   Úhasattrr_   ÚsysÚversion_infor   Úformat)r   ÚtokensourceÚoutfiler   r   r   rc   é   s
    
zTerminal256Formatter.formatc       
      C   sÐ   xÊ|D ]Â\}}d}x¦|r¸|r¸yx| j t|ƒ \}}|jdƒ}x4|d d… D ]$}	|	rb|j||	 | ƒ |jdƒ qHW |d rŽ|j||d  | ƒ d}W q tk
r´   |d d… }Y qX qW |r|j|ƒ qW d S )	NTÚ
r(   Féÿÿÿÿrg   rg   rg   )r0   r#   ÚsplitÚwriteÚKeyError)
r   rd   re   r\   ÚvalueÚ	not_foundÚonÚoffZsplÚliner   r   r   Úformat_unencodedò   s"    

z%Terminal256Formatter.format_unencodedN)r+   r,   r-   Ú__doc__ÚnameÚaliasesÚ	filenamesr   r3   rR   rY   r4   rc   rp   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   r3   "  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 )NrS   r:   rD   )r$   r#   rU   )r   rV   rX   rG   rH   rI   r   r   r   Ú_color_tuple%  s    z'TerminalTrueColorFormatter._color_tuplec             C   s�   xŠ| j D ]€\}}tƒ }|d r.| j|d ƒ|_|d rF| j|d ƒ|_| jrZ|d rZd|_| jrn|d rnd|_|j	ƒ |j
ƒ f| jt|ƒ< qW d S )NrV   rZ   r
   Tr   )r[   r   ru   r   r	   r1   r
   r2   r   r)   r*   r0   r#   )r   r\   r]   r   r   r   r   r4   /  s    z(TerminalTrueColorFormatter._setup_stylesN)
r+   r,   r-   rq   rr   rs   rt   r3   ru   r4   r   r   r   r   r     s   

)rq   ra   Zpygments.formatterr   Zpygments.consoler   Zpygments.styler   Ú__all__r   r   r   r   r   r   r   Ú<module>   s   ; 1