3 �Pf�%�@s�ddlZddlZddlZddlmZmZmZmZddlm Z m Z m Z ddl m Z mZdZe dk rfe �Zdd�Zdd �ZGd d �d e�ZGd d �d e�ZdS)�N�)�AnsiFore�AnsiBack� AnsiStyle�Style)�WinTerm�WinColor�WinStyle)�windll� winapi_testcCst|d� p|jS)N�closed)�hasattrr )�stream�r�!/usr/lib/python3.6/ansitowin32.py�is_stream_closedsrcCst|d�o|j�S)N�isatty)r r)rrrr�is_a_ttysrc@s(eZdZdZdd�Zdd�Zdd�ZdS) � StreamWrapperz� Wraps a stream (such as stdout), acting as a transparent proxy for all attribute access apart from method 'write()', which is delegated to our Converter instance. cCs||_||_dS)N)�_StreamWrapper__wrapped�_StreamWrapper__convertor)�self�wrappedZ converterrrr�__init__szStreamWrapper.__init__cCs t|j|�S)N)�getattrr)r�namerrr� __getattr__$szStreamWrapper.__getattr__cCs|jj|�dS)N)r�write)r�textrrrr'szStreamWrapper.writeN)�__name__� __module__� __qualname__�__doc__rrrrrrrrsrc@s~eZdZdZejd�Zejd�Zddd�Zdd �Z d d �Z d d �Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zdd�ZdS)� AnsiToWin32z� Implements a 'write()' method which, on Windows, will strip ANSI character sequences from the text, and if outputting to a tty, will convert them into win32 function calls. z?\[((?:\d|;)*)([a-zA-Z])?z?\]((?:.|;)*?)()?NFcCs�||_||_t||�|_tjdk}|o*t�}|dkrL|pJt|� oJt|� }||_ |dkrp|ont|� ont|�}||_ |j �|_ |jt jk|_dS)N�nt)r� autoresetrr�osrr rr�strip�convert�get_win32_calls� win32_calls�sys�stderr� on_stderr)rrr(r'r%Z on_windowsZconversion_supportedrrrr4s    zAnsiToWin32.__init__cCs|jp|jp|jS)aj True if this class is actually needed. If false, then the output stream will not be affected, nor will win32 calls be issued, so wrapping stdout is not actually required. This will generally be False on non-Windows platforms, unless optional functionality like autoreset has been requested using kwargs to init() )r(r'r%)rrrr� should_wrapUszAnsiToWin32.should_wrapcNCs>|jot�r8tjtjftjtjtjftjtjtj ftj tjtj ft j tj t j ft jtj t jft jtj t jft jtj t jft jtj t jft jtj t jft jtj t jft jtj t jft jtj ft jtj t j dft jtj t jdft jtj t jdft jtj t jdft jtj t jdft jtj t jdft jtj t jdft jtj t jdftj tj t j ftjtj t jftjtj t jftjtj t jftjtj t jftjtj t jftjtj t jftjtj t jftjtj ftjtj t j dftjtj t jdftjtj t jdftjtj t jdftjtj t jdftjtj t jdftjtj t jdftjtj t jdfi&St!�S)NT)"r(�wintermr� RESET_ALL� reset_allZBRIGHTZstyler ZDIMZNORMALrZBLACKZforerZREDZGREENZYELLOWZBLUEZMAGENTAZCYANZWHITEZGREYZRESETZ LIGHTBLACK_EXZ LIGHTRED_EXZ LIGHTGREEN_EXZLIGHTYELLOW_EXZ LIGHTBLUE_EXZLIGHTMAGENTA_EXZ LIGHTCYAN_EXZ LIGHTWHITE_EXrZback�dict)rrrrr)_sP    zAnsiToWin32.get_win32_callscCs@|js |jr|j|�n|jj|�|jj�|jr<|j�dS)N)r'r(�write_and_convertrr�flushr%r1)rrrrrr�s     zAnsiToWin32.writecCs:|jr|jdd�n"|j r6t|j� r6|jjtj�dS)N�mr)r)r(� call_win32r'rrrrr0)rrrrr1�szAnsiToWin32.reset_allcCsfd}|j|�}x@|jj|�D]0}|j�\}}|j|||�|j|j��|}qW|j||t|��dS)z� Write the given text to our wrapped stream, stripping any ANSI sequences from the text, and optionally converting them into win32 calls. rN)� convert_osc� ANSI_CSI_RE�finditer�span�write_plain_text� convert_ansi�groups�len)rrZcursor�match�start�endrrrr3�s  zAnsiToWin32.write_and_convertcCs*||kr&|jj|||��|jj�dS)N)rrr4)rrr@rArrrr;�szAnsiToWin32.write_plain_textcCs"|jr|j||�}|j||�dS)N)r(�extract_paramsr6)r� paramstring�command�paramsrrrr<�s zAnsiToWin32.convert_ansicCs~|dkrtdd�|jd�D��}t|�dkrz|d krnd }n |d krzd }|S)N�Hfcss&|]}t|�dkrt|�ndVqdS)rrN)r>�int)�.0�prrr� �sz-AnsiToWin32.extract_params..�;�rcss"|]}t|�dkrt|�VqdS)rN)r>rG)rHrIrrrrJ�srZJKm�ABCD)r)r)r)�tuple�splitr>)rrDrCrErrrrB�s zAnsiToWin32.extract_paramsc Cs�|dkrVx�|D]B}||jkr|j|}|d}|dd�}t|jd�}|||�qWn�|dkrttj|d|jd�n�|dkr�tj|d|jd�nf|dkr�tj||jd�nL|dkr�|d}d| fd|f|df| dfd �|\} } tj| | |jd�dS) Nr5rr)r-�J�KrFrM)�A�B�C�D)r*r2r-r/Z erase_screenZ erase_lineZset_cursor_positionZ cursor_adjust) rrDrEZparamZ func_args�func�args�kwargs�n�x�yrrrr6�s$     *zAnsiToWin32.call_win32cCsvxp|jj|�D]`}|j�\}}|d|�||d�}|j�\}}|dkr|jd�}|ddkrtj|d�qW|S)N�rKrZ02r)� ANSI_OSC_REr9r:r=rOr/Z set_title)rrr?r@rArCrDrErrrr7�s    zAnsiToWin32.convert_osc)NNF)rr r!r"�re�compiler8r]rr.r)rr1r3r;r<rBr6r7rrrrr#+s   ! , r#)r^r+r&�ansirrrrr/rrr Zwin32r r rr�objectrr#rrrr�s