
    L                     z   S SK Jr  S SKJrJrJrJrJrJrJ	r	J
r
  S SKJr  S SKJr  S SKJr  S SKJrJrJrJrJr  S SKrS SKrSrS	 rS
rSr " S S\5      r " S S\5      r " S S5      r  " S S5      r!S r"\"" \ 5      r#\"" \!5      r$\%" \#5      \%" \5      :X  d   e\%" \$5      \%" \5      :X  d   e " S S\&5      r'g)    )unicode_literals)windllbyrefArgumentErrorc_charc_longc_ulongc_uintpointer)DWORD)Output)ANSI_COLOR_NAMES)CONSOLE_SCREEN_BUFFER_INFOSTD_OUTPUT_HANDLESTD_INPUT_HANDLECOORD
SMALL_RECTN)Win32Outputc                 R    [        U R                  S-  U R                  S-  -  5      $ )a$  
Turns a COORD object into a c_long.
This will cause it to be passed by value instead of by reference. (That is what I think at least.)

When runing ``ptipython`` is run (only with IPython), we often got the following error::

     Error in 'SetConsoleCursorPosition'.
     ArgumentError("argument 2: <class 'TypeError'>: wrong type",)
 argument 2: <class 'TypeError'>: wrong type

It was solved by turning ``COORD`` parameters into a ``c_long`` like this.

More info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx
i   i  )r   YX)coords    7lib/third_party/prompt_toolkit/terminal/win32_output.py_coord_byvalr      s&     %''G#egg&6677    Fz!prompt-toolkit-windows-output.logc                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )NoConsoleScreenBufferError+   zu
Raised when the application is not running inside a Windows Console, but
the user tries to instantiate Win32Output.
c                    > S[         R                  R                  SS5      ;   nU(       a  S[         R                  S   -  nOSn[        [        U ]  U5        g )NxtermTERM zFound %s, while expecting a Windows console. Maybe try to run this program using "winpty" or run it in cmd.exe instead. Or otherwise, in case of Cygwin, use the Python executable that is compiled for Cygwin.z2No Windows console found. Are you running cmd.exe?)osenvirongetsuperr   __init__)selfr    message	__class__s      r   r'   #NoConsoleScreenBufferError.__init__0   sR    2::>>&"556 9;

68J	KG KG($8Ar    )__name__
__module____qualname____firstlineno____doc__r'   __static_attributes____classcell__)r*   s   @r   r   r   +   s    B Br   r   c                       \ rS rSrSrS&S jrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rS'S jrS rS rS rS rS rS rS rS rS r S r!S  r"S! r#S" r$\%S# 5       r&S$r'g%)(r   ?   zK
I/O abstraction for rendering to Windows consoles.
(cmd.exe and similar.)
c                 <   X l         / U l        Xl        [        R                  R                  [        5      U l        SU l        [        5       U l
        U R                  5       nU(       a  UR                  OSU l        [        (       a  [        [         S5      U l        g g )NF   ab)use_complete_width_bufferstdoutr   kernel32GetStdHandler   hconsole_in_alternate_screenColorLookupTablecolor_lookup_tableget_win32_screen_buffer_infowAttributesdefault_attrs_DEBUG_RENDER_OUTPUTopen_DEBUG_RENDER_OUTPUT_FILENAMELOG)r(   r;   r9   infos       r   r'   Win32Output.__init__D   sz    "4445FG$)!"2"4 00215T--294@DH  r   c                 6    U R                   R                  5       $ )zReturn file descriptor. )r;   filenor(   s    r   rL   Win32Output.filenoV   s    {{!!##r   c                 .    U R                   R                  $ )z!Return encoding used for stdout. )r;   encodingrM   s    r   rP   Win32Output.encodingZ   s    {{###r   c                 :    U R                   R                  U5        g N)r:   appendr(   datas     r   writeWin32Output.write^   s    D!r   c                 &    U R                  U5        g)z?For win32, there is no difference between write and write_raw. N)rW   rU   s     r   	write_rawWin32Output.write_rawa   s    

4r   c                    SSK Jn  U R                  5       nU R                  (       a  UR                  R
                  nO-UR                  R                  UR                  R                  -
  nUR                  R                  UR                  R                  -
  S-   nUR                  R
                  S-
  n[        XS5      nU" XCS9$ )Nr   )Size   )rowscolumns)prompt_toolkit.layout.screenr]   rB   r9   dwSizer   srWindowRightLeftBottomTopmin)r(   r]   rI   widthheightmaxwidths         r   get_sizeWin32Output.get_sizee   s    5002
 ""KKMMEMM''$--*<*<<E%%(9(99A= ;;==1$H$ //r   c                 6   U R                  5         [        (       Ga  U R                  R                  SUR                  -  R                  S5      S-   5        U R                  R                  SSR                  U Vs/ s H  nSU-  PM
     sn5      R                  S5      -   S-   5        U R                  R                  SSR                  U Vs/ s H  nS[        U5      -  PM     sn5      R                  S5      -   S-   5        U R                  R                  5          U" U0 UD6$ s  snf s  snf ! [         a[  n[        (       aF  U R                  R                  SUR                  < SU< SU< S3R                  S5      5         S	nAg	 S	nAg	S	nAff = f)
z"
Flush and call win API function.
%rutf-8   
s        z, z    Error in  
N)	flushrE   rH   rW   r-   encodejointyper   )r(   funcakwies         r   _winapiWin32Output._winapiz   s9    	

HHNND4==088AEIJHHNN8dii10E1a10E&F&M&Mg&VVY^^_HHNN8dii0KAQ0K&L&S&ST[&\\_ddeHHNN	d>b>! 1F0K
  	d##T]]Aq QYYZabcc $	ds%   <D)D.!D3 3
F=AFFc                     U R                  5         [        5       n[        R                  R	                  U R
                  [        U5      5      nU(       a  U$ [        e)z
Return Screen buffer info.
)rt   r   r   r<   GetConsoleScreenBufferInfor>   r   r   )r(   sbinfosuccesss      r   rB   (Win32Output.get_win32_screen_buffer_info   sD    , 	

+-//<<T]]ERXMZ
 M,,r   c                     [        U[        R                  5      (       d   eU R                  [        R
                  R                  U5        g)z
Set terminal title.
N)
isinstancesix	text_typer}   r   r<   SetConsoleTitleW)r(   titles     r   	set_titleWin32Output.set_title   s3     %////V__55u=r   c                 X    U R                  [        R                  R                  S5        g )Nr"   )r}   r   r<   r   rM   s    r   clear_titleWin32Output.clear_title   s    V__55r:r   c                     [        SS5      nU R                  5       nUR                  R                  UR                  R                  -  nU R                  SSS9  U R                  X5        g )Nr   )rowcolumn)r   rB   rb   r   r   cursor_goto_erase)r(   startr   lengths       r   erase_screenWin32Output.erase_screen   sT    a2246==??2Qq)E"r   c                 
   U R                  5       nUR                  nUR                  nUR                  UR                  -
  UR                  UR                  UR                  R                  -
  -  -   nU R                  X45        g rS   )rB   rb   dwCursorPositionr   r   r   )r(   r   sizer   r   s        r   
erase_downWin32Output.erase_down   sd    224}}''66DFF?dff9P9P9R9R0R&SSE"r   c                     U R                  5       nUR                  nUR                  R                  UR                  R                  -
  nU R	                  X#5        g)z	
        N)rB   r   rb   r   r   )r(   r   r   r   s       r   erase_end_of_lineWin32Output.erase_end_of_line   sG     224''6#:#:#<#<<E"r   c           
         [        5       nU R                  [        R                  R                  U R
                  [        S5      [        U5      [        U5      [        U5      5        U R                  5       nU R                  [        R                  R                  U R
                  UR                  U[        U5      [        U5      5        g )N    )r	   r}   r   r<   FillConsoleOutputCharacterAr>   r   r   r   r   rB   FillConsoleOutputAttributerC   )r(   r   r   chars_writtenr   s        r   r   Win32Output._erase   s    	V__@@]]F4L%-eAT=)	+
 224V__??]]F$6$6U@S=)	+r   c                     U R                  [        R                  R                  U R                  U R
                  5        g)z/Reset the console foreground/background color. N)r}   r   r<   SetConsoleTextAttributer>   rD   rM   s    r   reset_attributesWin32Output.reset_attributes   s)    V__<<dmm''	)r   c                 d   Uu  p#pEpgnU R                   nUb"  US-  nXR                  R                  U5      -  nUb"  US-  nXR                  R                  U5      -  nU(       a  US-  US-  S-  -  US-  S-	  -  nU R	                  [
        R                  R                  U R                  U5        g )Niii r7         )	rD   rA   lookup_fg_colorlookup_bg_colorr}   r   r<   r   r>   )	r(   attrsfgcolorbgcolorbold	underlineitalicblinkreverses	            r   set_attributesWin32Output.set_attributes   s    DIA$6' "" DLE,,<<WEEE EME,,<<WEEE U]'9:ut|PQ>QREV__<<dmmUSr   c                     g rS   r,   rM   s    r   disable_autowrapWin32Output.disable_autowrap       r   c                     g rS   r,   rM   s    r   enable_autowrapWin32Output.enable_autowrap   r   r   c                     [        X!S9nU R                  [        R                  R                  U R
                  [        U5      5        g )N)xy)r   r}   r   r<   SetConsoleCursorPositionr>   r   )r(   r   r   poss       r   r   Win32Output.cursor_goto  s0    f$V__==t}}l[^N_`r   c                     U R                  5       R                  n[        UR                  UR                  U-
  5      nU R                  [        R                  R                  U R                  [        U5      5        g rS   )rB   r   r   r   r   r}   r   r<   r   r>   r   r(   amountsrr   s       r   	cursor_upWin32Output.cursor_up  sR    ..0AABDD"$$-(V__==t}}l[^N_`r   c                 (    U R                  U* 5        g rS   )r   r(   r   s     r   cursor_downWin32Output.cursor_down  s    wr   c                    U R                  5       R                  n[        [        SUR                  U-   5      UR
                  5      nU R                  [        R                  R                  U R                  [        U5      5        g )Nr   )rB   r   r   maxr   r   r}   r   r<   r   r>   r   r   s       r   cursor_forwardWin32Output.cursor_forward  s[    ..0AA C244&=)2440V__==t}}l[^N_`r   c                 (    U R                  U* 5        g rS   )r   r   s     r   cursor_backwardWin32Output.cursor_backward  s    VG$r   c           	         U R                   (       d  U R                  R                  5         gSR                  U R                   5      n[        (       aJ  U R
                  R                  SU-  R                  S5      S-   5        U R
                  R                  5         U HK  n[        5       n[        R                  R                  U R                  US[        U5      S5      nUS:w  a  MK   e   / U l         g)z#
Write to output stream and flush.
Nr"   ro   rp   rq   r^   r   )r:   r;   rt   rv   rE   rH   rW   ru   r   r   r<   WriteConsoleWr>   r   )r(   rV   bwrittenretvals        r   rt   Win32Output.flush  s     || KKwwt||$HHNND4K//85@AHHNN
 AgG__224==!QgX\]FQ;;	  r   c                     U R                  5       nUR                  R                  UR                  R                  -
  S-   $ )Nr^   )rB   rc   rf   r   r   )r(   rI   s     r   get_rows_below_cursor_position*Win32Output.get_rows_below_cursor_position4  s6    002}}##d&;&;&=&==AAr   c                 6   U R                  5       nUR                  nUR                  n[        5       nSUl        UR
                  UR                  -
  Ul        UR                  UR                  -
  nSUR                  UR                  -
  s=:  a	  US-
  :  a  O  OUR                  Ul        O[        XSR                  5      Ul        UR                  U-
  Ul        U R                  [        R                  R                  U R                  S[        U5      5        g)z~
To be called before drawing the prompt. This should scroll the console
to left, with the cursor at the bottom (if possible).
r   r^   TN)rB   rc   r   r   re   rd   rf   rg   r   r   r}   r   r<   SetConsoleWindowInfor>   r   )r(   rI   r   
cursor_posresult
win_heights         r   scroll_buffer_to_prompt#Win32Output.scroll_buffer_to_prompt8  s     002]]**
 xx"'') YY'
ryy:<<'8*q.8IIFM
LL9FM]]Z/
 	V__994==$PUV\P]^r   c           	      "   U R                   (       d~  SnSnU R                  [        R                  R                  X-  [        S5      S[        S5      S5      nU R                  [        R                  R                  U5        X0l        SU l         gg)z 
Go to alternate screen buffer.
l        i   @r   Nr^   T)r?   r}   r   r<   CreateConsoleScreenBufferr   SetConsoleActiveScreenBufferr>   )r(   GENERIC_READGENERIC_WRITEhandles       r   enter_alternate_screen"Win32Output.enter_alternate_screenT  sv     ((%L&M \\&//"K"K\Mg"'(D%(DBF LLEEvN"M(,D% )r   c                 Z   U R                   (       a  U R                  [        R                  R                  [
        5      nU R                  [        R                  R                  U5        U R                  [        R                  R                  U R                  5        Xl        SU l         gg)z&
Make stdout again the active buffer.
FN)	r?   r}   r   r<   r=   r   r   CloseHandler>   )r(   r;   s     r   quit_alternate_screen!Win32Output.quit_alternate_screend  sn     $$\\&//">">@QRFLLEEvNLL44dmmD"M(-D% %r   c                 8   Sn[         R                  R                  [        5      n[	        5       nU R                  [         R                  R                  U[        U5      5        U R                  [         R                  R                  X#R                  U-  5        g N   
r   r<   r=   r   r   r}   GetConsoleModer   SetConsoleModevaluer(   ENABLE_MOUSE_INPUTr   original_modes       r   enable_mouse_support Win32Output.enable_mouse_supporto  sg    !--.>?V__33VW]=STV__33V=P=PSe=efr   c                 :   Sn[         R                  R                  [        5      n[	        5       nU R                  [         R                  R                  U[        U5      5        U R                  [         R                  R                  X#R                  U) -  5        g r   r   r   s       r   disable_mouse_support!Win32Output.disable_mouse_supportw  sj    !--.>?V__33VW]=STV__33V=P=PUgSg=ghr   c                     g rS   r,   rM   s    r   hide_cursorWin32Output.hide_cursor      r   c                     g rS   r,   rM   s    r   show_cursorWin32Output.show_cursor  r  r   c                     [         R                  R                  5       nSn[         R                  R	                  USS[        U5      5        g)z
Call win32 API to refresh the whole Window.

This is sometimes necessary when the application paints background
for completion menus. When the menu disappears, it leaves traces due
to a bug in the Windows Console. Sending a repaint request solves it.
r^   N)r   r<   GetConsoleWindowuser32RedrawWindowr
   )clsr   RDW_INVALIDATEs      r   win32_refresh_window Win32Output.win32_refresh_window  s8     113""64vn7MNr   )rH   r:   r?   rA   rD   r>   r;   r9   N)F)r   r   )(r-   r.   r/   r0   r1   r'   rL   rP   rW   rZ   rl   r}   rB   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   r   r   r   r   r   r   r  r  classmethodr  r2   r,   r   r   r   r   ?   s    A$$$"0*d$ -D>;###+)
T,aa
 a%8B_8- 	.gi O Or   r   c                   8    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrg)FOREGROUND_COLORi  r   r^         r               r,   Nr-   r.   r/   r0   BLACKBLUEGREENCYANREDMAGENTAYELLOWGRAY	INTENSITYr2   r,   r   r   r  r    /    EDEDCGFDIr   r  c                   8    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrg)BACKROUND_COLORi  r   r       0   @   P   `   p      r,   Nr  r,   r   r   r&  r&    r$  r   r&  c                    0 SU R                   _SU R                   _SU R                   U R                  -  _SU R                  _SU R                  U R                  -  _SU R                  _SU R                  _SU R
                  _S	U R                  _S
U R                  _SU R                  _SU R                  U R                  -  _SU R                  U R                  -  _SU R
                  U R                  -  _SU R                  U R                  -  _SU R                  U R                  -  _SU R                  U R                  -  _$ )zICreate a table that maps the 16 named ansi colors to their Windows code. ansidefault	ansiblackansidarkgrayansilightgray	ansiwhiteansidarkredansidarkgreen	ansibrownansidarkblue
ansipurpleansitealansired	ansigreen
ansiyellowansiblueansifuchsiaansiturquoise)	r  r#  r"  r  r  r!  r  r   r  )	color_clss    r   _create_ansi_color_dictrA    sx    	9+>+>> 		
 	)*=*== 	9== 	9?? 	9++ 	9>> 	9,, 	9>>  	)--)*=*==!" 	)//I,?,??#$ 	)**Y-@-@@%& 	)..9+>+>>'( 	)++i.A.AA)* 	)..9+>+>>+ r   c                   F    \ rS rSrSrS r\S 5       rS rS r	S r
S rS	rg
)r@   i  z0
Inspired by pygments/formatters/terminal256.py
c                 <    U R                  5       U l        0 U l        g rS   )_build_color_table_win32_colors
best_matchrM   s    r   r'   ColorLookupTable.__init__  s    !446r   c                     [         n [        nSSSU R                  UR                  4SSSU R                  UR                  4SSSU R                  UR                  4SSSU R
                  UR
                  4SSSU R                  UR                  4SSSU R                  UR                  4SSSU R                  UR                  4SSSU R                  UR                  4SSSU R                  U R                  -  UR                  UR                  -  4SSSU R                  U R                  -  UR                  UR                  -  4SSSU R
                  U R                  -  UR
                  UR                  -  4SSSU R                  U R                  -  UR                  UR                  -  4SSSU R                  U R                  -  UR                  UR                  -  4SSSU R                  U R                  -  UR                  UR                  -  4SSSU R                  U R                  -  UR                  UR                  -  4SSSU R                  U R                  -  UR                  UR                  -  4/$ )z,
Build an RGB-to-256 color conversion table
r         D      )r  r&  r  r  r  r  r  r   r!  r"  r#  )FGBGs     r   rD  #ColorLookupTable._build_color_table  s
   
  4rxx24rww04rxx24rww04rvvrvv.4rzz2::64ryy"))44rww04rww5rww7MN4rxx",,62<<8OP4rww5rww7MN4rvv4bffr||6KL4rzzBLL8"**r||:ST4ryy2<<7R\\9QR4rxx",,62<<8OP4rww5rww7MN%
 	
r   c                     SnSnSnU R                    H.  u  pxpnX-
  nX(-
  nX9-
  nX-  X-  -   X-  -   nX:  d  M(  U
nUnUnM0     XV4$ )Ni r   )rE  )r(   rgr   distancefg_matchbg_matchr_g_b_fg_bg_rdgdbdds                   r   _closest_colorColorLookupTable._closest_color  sq     $($6$6 BBSBBB"'!BG+A| %7 !!r   c                     U R                   R                  US 5      nUcK   [        [        U5      S5      nUS-	  S-  nUS-	  S-  nUS-  nU R                  XEU5      nX R                   U'   U$ ! [         a    Sn NEf = f)Nr   r   rL  r  )rF  r%   intstr
ValueErrorr_  )r(   colorindexesrgbrQ  rR  r   s          r   _color_indexesColorLookupTable._color_indexes  s    //%%eT2?#e*b) d"AT!Ad
A))!2G%,OOE"  s   A- -A<;A<c                 P    U[         ;   a	  [         U   $ U R                  U5      S   $ )z
Return the color for use in the
`windll.kernel32.SetConsoleTextAttribute` API call.

:param fg_color: Foreground as text. E.g. 'ffffff' or 'red'
r   )FG_ANSI_COLORSrh  )r(   fg_colors     r   r    ColorLookupTable.lookup_fg_color  -     ~%!(++&&x033r   c                 P    U[         ;   a	  [         U   $ U R                  U5      S   $ )z
Return the color for use in the
`windll.kernel32.SetConsoleTextAttribute` API call.

:param bg_color: Background as text. E.g. 'ffffff' or 'red'
r^   )BG_ANSI_COLORSrh  )r(   bg_colors     r   r    ColorLookupTable.lookup_bg_color!  rn  r   )rE  rF  N)r-   r.   r/   r0   r1   r'   staticmethodrD  r_  rh  r   r   r2   r,   r   r   r@   r@     s4     
 
8"$44r   r@   )(
__future__r   ctypesr   r   r   r   r   r	   r
   r   ctypes.wintypesr   prompt_toolkit.rendererr   prompt_toolkit.stylesr   prompt_toolkit.win32_typesr   r   r   r   r   r#   r   __all__r   rE   rG   	Exceptionr   r   r  r&  rA  rk  rp  setobjectr@   r,   r   r   <module>r~     s    ' Y Y Y ! * 2 y y 	 

8*   D B B(SO& SOl
	 		 	4 ))9:(9
>c"233 33
>c"233 33^4v ^4r   