
    ^hoX                    ,   S SK Jr  S SKr\R                  S:X  d   eS SKrS SKJrJrJrJ	r	J
r
JrJr  S SKJrJr  S SKJrJrJr  S SKJr  S SKJr  S S	KJrJr  S S
KJr  S SK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(  \$(       d  S SKJ)r)  S/r*S!S jr+Sr,Sr- " S S\.5      r/\" S5      r0 " S S\&5      r1 " S S5      r2 " S S5      r3    S"S jr4\4" \25      r5\4" \35      r6\7" \55      \7" \5      :X  d   e\7" \65      \7" \5      :X  d   e " S S 5      r8g)#    )annotationsNwin32)ArgumentErrorbyrefc_charc_longc_uintc_ulongpointer)DWORDHANDLE)CallableTextIOTypeVar)CursorShape)Size)ANSI_COLOR_NAMESAttrs)
get_cwidth)CONSOLE_SCREEN_BUFFER_INFOCOORD
SMALL_RECTSTD_INPUT_HANDLESTD_OUTPUT_HANDLE   )SPHINX_AUTODOC_RUNNING   )Output)
ColorDepth)windll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 running ``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    U/home/james-whalen/.local/lib/python3.13/site-packages/prompt_toolkit/output/win32.py_coord_byvalr'   '   s&     %''G#egg&6677    Fz!prompt-toolkit-windows-output.logc                  0   ^  \ rS rSrSrSU 4S j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                  [         R                  S   5      nOSn[        TU ]  U5        g )NxtermTERM zFound {}, 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formatsuper__init__)selfr-   message	__class__s      r&   r5   #NoConsoleScreenBufferError.__init__F   sS    2::>>&"55/ 06vbjj6H/I  KG!r(    returnNone)__name__
__module____qualname____firstlineno____doc__r5   __static_attributes____classcell__)r8   s   @r&   r*   r*   @   s    
" "r(   r*   _Tc                     \ rS rSrSr  S)       S*S jjrS+S jrS,S jrS-S jrS-S jr	S.S	 jr
S/S
 jrS0S jrS1S jrS2S jrS2S jrS2S jrS2S jrS3S jrS2S jrS4S jrS2S jrS2S jrS5S6S jjrS7S jrS7S jrS7S jrS7S jrS2S jrS+S jrS2S jrS2S jrS2S jr S2S  jr!S2S! jr"S2S" jr#S2S# jr$S8S$ jr%S2S% jr&\'S2S& j5       r(S9S' jr)S(r*g):r!   Z   zK
I/O abstraction for rendering to Windows consoles.
(cmd.exe and similar.)
Nc                h   X l         X0l        / U l        Xl        [	        [
        R                  R                  [        5      5      U l	        S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default_color_depth_bufferstdoutr   r    kernel32GetStdHandler   hconsole_in_alternate_screen_hiddenColorLookupTablecolor_lookup_tableget_win32_screen_buffer_infowAttributesdefault_attrs_DEBUG_RENDER_OUTPUTopen_DEBUG_RENDER_OUTPUT_FILENAMELOG)r6   rN   rK   rL   infos        r&   r5   Win32Output.__init__`   s     #5#6 "$$v;;<MNO$)!"2"4 00215T--294@DH  r(   c                6    U R                   R                  5       $ )zReturn file descriptor.)rN   filenor6   s    r&   r`   Win32Output.filenoy   s    {{!!##r(   c                .    U R                   R                  $ )z Return encoding used for stdout.)rN   encodingra   s    r&   rd   Win32Output.encoding}   s    {{###r(   c                x    U R                   (       a  S[        U5      -  nU R                  R                  U5        g )N )rS   r   rM   appendr6   datas     r&   writeWin32Output.write   s*    <<D))DD!r(   c                &    U R                  U5        g)z>For win32, there is no difference between write and write_raw.N)rk   ri   s     r&   	write_rawWin32Output.write_raw   s    

4r(   c                   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[        XB5      n[        X2S9$ )Nr   )rowscolumns)rV   rK   dwSizer$   srWindowRightLeftBottomTopminr   )r6   r]   widthheightmaxwidths        r&   get_sizeWin32Output.get_size   s    002
 ""KKMMEMM''$--*<*<<E%%(9(99A= ;;==1$H$ //r(   c                   U R                  5         [        (       a  U R                  R                  UR                  < R                  5       S-   5        U R                  R                  SSR                  U Vs/ s H  oD< PM     sn5      R                  S5      -   S-   5        U R                  R                  SSR                  U Vs/ s H  n[        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 ! [         aP  n[        (       a?  U R                  R                  SUR                  < SU< SU S3R                  5       5        e SnAff = f)	z"
Flush and call win API function.
   
s        z, zutf-8z    Error in rg   
N)	flushrY   r\   rk   r>   encodejointyper   )r6   funcakwies         r&   _winapiWin32Output._winapi   sF    	

HHNNt}}/88:UBCHHNN499%:1h%:;BB7KKeS HHNN))Q7QQ{^Q78??HI
 HHNN	>b>! &; 8  	##-/@!as" MUUWX		s%   9D#
D(D- -
F7A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.
)r   r   r    rO   GetConsoleScreenBufferInforQ   r   r*   )r6   sbinfosuccesss      r&   rV   (Win32Output.get_win32_screen_buffer_info   sF    , 	

+-//<<MM5=
 M,,r(   c                X    U R                  [        R                  R                  U5        g)z
Set terminal title.
Nr   r    rO   SetConsoleTitleW)r6   titles     r&   	set_titleWin32Output.set_title   s     	V__55u=r(   c                X    U R                  [        R                  R                  S5        g )Nr/   r   ra   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   rV   rs   r$   r#   cursor_goto_erase)r6   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 N)rV   rs   dwCursorPositionr$   r#   r   )r6   r   sizer   r   s        r&   
erase_downWin32Output.erase_down   sd    224}}''&&466/TVVtvv8O8O8Q8Q/Q%RRE"r(   c                    U R                  5       nUR                  nUR                  R                  UR                  R                  -
  nU R	                  X#5        g)r/   N)rV   r   rs   r$   r   )r6   r   r   r   s       r&   erase_end_of_lineWin32Output.erase_end_of_line   sE    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    rO   FillConsoleOutputCharacterArQ   r   r   r'   r   rV   FillConsoleOutputAttributerW   )r6   r   r   chars_writtenr   s        r&   r   Win32Output._erase   s    	OO77MM4L&M- 	
 224OO66MM- 	
r(   c                    U R                  [        R                  R                  U R                  U R
                  5        SU l        g)z.Reset the console foreground/background color.FN)r   r    rO   SetConsoleTextAttributerQ   rX   rS   ra   s    r&   reset_attributesWin32Output.reset_attributes  s2    OO33T]]DDVDV	
 r(   c           
        Uu
  nnnnnnn	n
nn[        U5      U l        U R                  nU[        R                  :w  aR  U(       a"  US-  nXR
                  R                  U5      -  nU(       a"  US-  nXR
                  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 rI         )boolrS   rX   r   DEPTH_1_BITrU   lookup_fg_colorlookup_bg_colorr   r    rO   r   rQ   )r6   attrscolor_depthfgcolorbgcolorbold	underlinestrikeitalicblinkreversehiddendim	win_attrss                 r&   set_attributesWin32Output.set_attributes  s     	
F| ++	*000%,	44DDWMM	 %-	44DDWMM	 U"O)+$*,  	V__<<dmmYWr(   c                    g r   r:   ra   s    r&   disable_autowrapWin32Output.disable_autowrapD      r(   c                    g r   r:   ra   s    r&   enable_autowrapWin32Output.enable_autowrapH  r   r(   c                    [        X!S9nU R                  [        R                  R                  U R
                  [        U5      5        g Nr$   r#   )r   r   r    rO   SetConsoleCursorPositionrQ   r'   )r6   r   r   poss       r&   r   Win32Output.cursor_gotoL  s3    f$OO44dmm\RUEV	
r(   c                    U R                  5       R                  n[        UR                  UR                  U-
  S9nU R                  [        R                  R                  U R                  [        U5      5        g r   )rV   r   r   r$   r#   r   r    rO   r   rQ   r'   r6   amountsrr   s       r&   	cursor_upWin32Output.cursor_upR  sU    ..0AAbddbddVm,OO44dmm\RUEV	
r(   c                (    U R                  U* 5        g r   )r   r6   r   s     r&   cursor_downWin32Output.cursor_downY  s    wr(   c                
   U R                  5       R                  n[        [        SUR                  U-   5      UR
                  S9nU R                  [        R                  R                  U R                  [        U5      5        g )Nr   r   )rV   r   r   maxr$   r#   r   r    rO   r   rQ   r'   r   s       r&   cursor_forwardWin32Output.cursor_forward\  s^    ..0AA c!RTTF]+rtt4OO44dmm\RUEV	
r(   c                (    U R                  U* 5        g r   )r   r   s     r&   cursor_backwardWin32Output.cursor_backwarde  s    VG$r(   c           	        U R                   (       d  U R                  R                  5         gSR                  U R                   5      n[        (       aH  U R
                  R                  U< R                  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/   r   r   r   )rM   rN   r   r   rY   r\   rk   r   r   r    rO   WriteConsoleWrQ   r   )r6   rj   bwrittenretvals        r&   r   Win32Output.flushh  s     || KKwwt||$HHNNth//1E9:HHNN
 AgG__22q!U7^TF Q;;  r(   c                    U R                  5       nUR                  R                  UR                  R                  -
  S-   $ )Nr   )rV   rt   rw   r   r#   )r6   r]   s     r&   get_rows_below_cursor_position*Win32Output.get_rows_below_cursor_position  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)rV   rt   r   r   rv   ru   rw   rx   r#   r   r   r    rO   SetConsoleWindowInforQ   r   )r6   r]   r   
cursor_posresult
win_heights         r&   scroll_buffer_to_prompt#Win32Output.scroll_buffer_to_prompt  s     002]]**
 xx"'') YY'
ryy:<<'8*q.8IIFM
LL9FM]]Z/
 	OO00$--uV}	
r(   c                4   U R                   (       d  SnSn[        U R                  [        R                  R
                  X-  [        S5      S[        S5      S5      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)	rR   r   r   r    rO   CreateConsoleScreenBufferr   SetConsoleActiveScreenBufferrQ   )r6   GENERIC_READGENERIC_WRITEhandles       r&   enter_alternate_screen"Win32Output.enter_alternate_screen  s     ((%L&M OO== 0!H!H	F LLEEvN"M(,D%% )r(   c                l   U R                   (       a  [        U R                  [        R                  R
                  [        5      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)
rR   r   r   r    rO   rP   r   r   CloseHandlerQ   )r6   rN   s     r&   quit_alternate_screen!Win32Output.quit_alternate_screen  sx     $$V__99;LMF LLEEvNLL44dmmD"M(-D% %r(   c                X   SnSn[        [        R                  R                  [        5      5      n[        5       nU R                  [        R                  R                  U[        U5      5        U R                  [        R                  R                  UUR                  U-  U) -  5        g )N   r+   r   r    rO   rP   r   r   r   GetConsoleModer   SetConsoleModevalue)r6   ENABLE_MOUSE_INPUTENABLE_QUICK_EDIT_MODEr   original_modes        r&   enable_mouse_support Win32Output.enable_mouse_support  s    !
 "(445EFGV__33VW]=STOO**  #55:P9PP	
r(   c                N   Sn[        [        R                  R                  [        5      5      n[        5       nU R                  [        R                  R                  U[        U5      5        U R                  [        R                  R                  UUR                  U) -  5        g )Nr  r	  )r6   r  r   r  s       r&   disable_mouse_support!Win32Output.disable_mouse_support  sv    !445EFGV__33VW]=STOO**#5"55	
r(   c                    g r   r:   ra   s    r&   hide_cursorWin32Output.hide_cursor      r(   c                    g r   r:   ra   s    r&   show_cursorWin32Output.show_cursor  r  r(   c                    g r   r:   )r6   cursor_shapes     r&   set_cursor_shapeWin32Output.set_cursor_shape  r  r(   c                    g r   r:   ra   s    r&   reset_cursor_shapeWin32Output.reset_cursor_shape  r  r(   c                    [        [        R                  R                  5       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    rO   GetConsoleWindowuser32RedrawWindowr	   )clsr   RDW_INVALIDATEs      r&   win32_refresh_window Win32Output.win32_refresh_window  s=     88:;""64vn7MNr(   c                T    U R                   b  U R                   $ [        R                  $ )z
Return the default color depth for a windows terminal.

Contrary to the Vt100 implementation, this doesn't depend on a $TERM
variable.
)rL   r   DEPTH_4_BITra   s    r&   get_default_color_depth#Win32Output.get_default_color_depth  s(     ##/+++%%%r(   )
r\   rM   rS   rR   rU   rX   rL   rQ   rN   rK   )FN)rN   r   rK   r   rL   zColorDepth | Noner<   r=   )r<   int)r<   str)rj   r0  r<   r=   )r<   r   )r   zCallable[..., _T]r   objectr   r1  r<   rE   )r<   r   )r   r0  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   r<   r=   )r<   r   )+r>   r?   r@   rA   rB   r5   r`   rd   rk   rn   r}   r   rV   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!  classmethodr)  r-  rC   r:   r(   r&   r!   r!   Z   s    $)15	AA !A /	A
 
A2$$"0(4"-H>;###
.%XN

 
%<B
<-0.
$

 O O
&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               r:   Nr>   r?   r@   rA   BLACKBLUEGREENCYANREDMAGENTAYELLOWGRAY	INTENSITYrC   r:   r(   r&   r4  r4    /    EDED
CGFDIr(   r4  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)BACKGROUND_COLORi  r   r      0   r+   P   `   p      r:   Nr:  r:   r(   r&   rF  rF    rD  r(   rF  c                   0 SU R                   _SU R                   _SU R                  _SU R                   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                  -  _$ )zHCreate a table that maps the 16 named ansi colors to their Windows code.ansidefault	ansiblackansigrayansibrightblack	ansiwhiteansired	ansigreen
ansiyellowansiblueansimagentaansicyanansibrightredansibrightgreenansibrightyellowansibrightblueansibrightmagentaansibrightcyan)	r;  rB  rC  r?  r=  rA  r<  r@  r>  )	color_clss    r&   _create_ansi_color_dictr`  +  sx   yY__ 	INN 	9??Y-@-@@	
 	Y^^i&9&99 	9== 	Y__ 	i&& 	INN 	y(( 	INN 	)<)<< 	9??Y-@-@@  	I,,y/B/BB!" 	)..9+>+>>#$ 	Y..1D1DD%& 	)..9+>+>>' r(   c                  ^    \ rS rSrSrSS jr\SS j5       rSS jrSS jr	SS jr
SS jrS	rg
)rT   iM  z0
Inspired by pygments/formatters/terminal256.py
c                <    U R                  5       U l        0 U l        g r   )_build_color_table_win32_colors
best_matchra   s    r&   r5   ColorLookupTable.__init__R  s    !446 79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      )r4  rF  r;  r<  r=  r>  r?  r@  rA  rB  rC  )FGBGs     r&   rc  #ColorLookupTable._build_color_tableX  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   )rd  )r6   rgr   distancefg_matchbg_matchr_g_b_fg_bg_rdgdbdds                   r&   _closest_colorColorLookupTable._closest_colors  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   rk  r9  )re  r2   r/  r0  
ValueErrorr~  )r6   colorindexesrgbrp  rq  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_COLORSr  )r6   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_COLORSr  )r6   bg_colors     r&   r    ColorLookupTable.lookup_bg_color  r  r(   )rd  re  Nr;   )r<   z$list[tuple[int, int, int, int, int]])rp  r/  rq  r/  r   r/  r<   tuple[int, int])r  r0  r<   r  )r  r0  r<   r/  )r  r0  r<   r/  )r>   r?   r@   rA   rB   r5   staticmethodrc  r~  r  r   r   rC   r:   r(   r&   rT   rT   M  s4    9 
 
4"$44r(   rT   )r%   r   r<   r   )r_  z/type[FOREGROUND_COLOR] | type[BACKGROUND_COLOR]r<   zdict[str, int])9
__future__r   sysplatformr0   ctypesr   r   r   r   r	   r
   r   ctypes.wintypesr   r   typingr   r   r   prompt_toolkit.cursor_shapesr   prompt_toolkit.data_structuresr   prompt_toolkit.stylesr   r   prompt_toolkit.utilsr   prompt_toolkit.win32_typesr   r   r   r   r   utilsr   baser   r   r   r    __all__r'   rY   r[   	Exceptionr*   rE   r!   r4  rF  r`  r  r  setrT   r:   r(   r&   <module>r     s   " 

||w  	 Q Q Q ) , , 4 / 9 +  +  #  
8*   D " ". T]v&& v&r	 		 	>6 ))9:()9:
>c"233 33
>c"233 33_4 _4r(   