3 υ_%@sddlZddlZddlZddlmZmZmZmZddlm Z m Z m Z ddl m Z mZdZe dk rfe ZddZdd ZGd d d eZGd d d eZdS)N)AnsiForeAnsiBack AnsiStyleStyle)WinTermWinColorWinStyle)windll winapi_testcCst|d p|jS)Nclosed)hasattrr )streamr!/usr/lib/python3.6/ansitowin32.pyis_stream_closedsrcCst|do|jS)Nisatty)r r)rrrris_a_ttysrc@s(eZdZdZddZddZddZdS) 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)selfwrappedZ converterrrr__init__szStreamWrapper.__init__cCs t|j|S)N)getattrr)rnamerrr __getattr__$szStreamWrapper.__getattr__cCs|jj|dS)N)rwrite)rtextrrrr'szStreamWrapper.writeN)__name__ __module__ __qualname____doc__rrrrrrrrsrc@s~eZdZdZejdZejdZdddZdd Z d d Z d d Z ddZ ddZ ddZddZddZddZddZdS) 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)Nnt)r autoresetrrosrr rrstripconvertget_win32_calls win32_callssysstderr 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>|jotr8tjtjftjtjtjftjtjtj 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_EXrZbackdict)rrrrr)_sP    zAnsiToWin32.get_win32_callscCs@|js |jr|j|n|jj||jj|jr<|jdS)N)r'r(write_and_convertrrflushr%r1)rrrrrrs     zAnsiToWin32.writecCs:|jr|jddn"|j r6t|j r6|jjtjdS)Nmr)r)r( call_win32r'rrrrr0)rrrrr1szAnsiToWin32.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_REfinditerspanwrite_plain_text convert_ansigroupslen)rrZcursormatchstartendrrrr3s  zAnsiToWin32.write_and_convertcCs*||kr&|jj||||jjdS)N)rrr4)rrr@rArrrr;szAnsiToWin32.write_plain_textcCs"|jr|j||}|j||dS)N)r(extract_paramsr6)r paramstringcommandparamsrrrr<s zAnsiToWin32.convert_ansicCs~|dkrtdd|jdD}t|dkrz|d krnd }n |d krzd }|S)NHfcss&|]}t|dkrt|ndVqdS)rrN)r>int).0prrr sz-AnsiToWin32.extract_params..;rcss"|]}t|dkrt|VqdS)rN)r>rG)rHrIrrrrJsrZJKmABCD)r)r)r)tuplesplitr>)rrDrCrErrrrBs zAnsiToWin32.extract_paramsc Cs|dkrVx|D]B}||jkr|j|}|d}|dd}t|jd}|||qWn|dkrttj|d|jdn|dkrtj|d|jdnf|dkrtj||jdnL|dkr|d}d| fd|f|df| dfd |\} } tj| | |jddS) Nr5rr)r-JKrFrM)ABCD)r*r2r-r/Z erase_screenZ erase_lineZset_cursor_positionZ cursor_adjust) rrDrEZparamZ func_argsfuncargskwargsnxyrrrr6s$     *zAnsiToWin32.call_win32cCsvxp|jj|D]`}|j\}}|d|||d}|j\}}|dkr|jd}|ddkrtj|dqW|S)NrKrZ02r) ANSI_OSC_REr9r:r=rOr/Z set_title)rrr?r@rArCrDrErrrr7s    zAnsiToWin32.convert_osc)NNF)rr r!r"recompiler8r]rr.r)rr1r3r;r<rBr6r7rrrrr#+s   ! , r#)r^r+r&ansirrrrr/rrr Zwin32r r rrobjectrr#rrrrs