
    f^X                       d dl mZ d dlZej                  dk(  sJ d dlZd dlmZmZmZm	Z	m
Z
mZmZ d dlmZmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( e$sd dlm)Z) dgZ*d!dZ+dZ,dZ- G d de.      Z/ ed      Z0 G d de&      Z1 G d d      Z2 G d d      Z3	 	 	 	 d"dZ4 e4e2      Z5 e4e3      Z6 e7e5       e7e      k(  sJ  e7e6       e7e      k(  sJ  G d d       Z8y)#    )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    t        | j                  dz  | j                  dz  z        S )aI  
    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    V/var/www/cvtools/html/venv/lib/python3.12/site-packages/prompt_toolkit/output/win32.py_coord_byvalr'   '   s&     %''G#egg&6677    Fz!prompt-toolkit-windows-output.logc                  $     e Zd ZdZd fdZ xZS )NoConsoleScreenBufferErrorz
    Raised when the application is not running inside a Windows Console, but
    the user tries to instantiate Win32Output.
    c                    dt         j                  j                  dd      v }|r#dj                  t         j                  d         }nd}t        |   |       y )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&   r4   z#NoConsoleScreenBufferError.__init__F   sS    2::>>&"55/ 06vbjj6H/I  KG!r(   returnNone)__name__
__module____qualname____doc__r4   __classcell__)r7   s   @r&   r*   r*   @   s    
" "r(   r*   _Tc                  P   e Zd ZdZ	 	 d'	 	 	 	 	 	 	 d(dZd)dZd*dZd+dZd+dZd,dZ	d-d	Z
d.d
Zd/dZd0dZd0dZd0dZd0dZd1dZd0dZd2dZd0dZd0dZd3d4dZd5dZd5dZd5dZd5dZd0dZd)dZd0dZd0dZd0dZd0dZ d0d Z!d0d!Z"d0d"Z#d6d#Z$d0d$Z%e&d0d%       Z'd7d&Z(y)8r!   zW
    I/O abstraction for rendering to Windows consoles.
    (cmd.exe and similar.)
    Nc                Z   || _         || _        g | _        || _        t	        t
        j                  j                  t                    | _	        d| _
        d| _        t               | _        | j                         }|r|j                  nd| _        t"        rt%        t&        d      | _        y y )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)r5   rH   rE   rF   infos        r&   r4   zWin32Output.__init__`   s     #5#6 "$$v;;<MNO$)!"2"4 00215T--294@DH  r(   c                6    | j                   j                         S )zReturn file descriptor.)rH   filenor5   s    r&   rY   zWin32Output.filenoy   s    {{!!##r(   c                .    | j                   j                  S )z Return encoding used for stdout.)rH   encodingrZ   s    r&   r\   zWin32Output.encoding}   s    {{###r(   c                n    | j                   rdt        |      z  }| j                  j                  |       y )N )rM   r   rG   appendr5   datas     r&   writezWin32Output.write   s*    <<D))DD!r(   c                &    | j                  |       y)z>For win32, there is no difference between write and write_raw.N)rb   r`   s     r&   	write_rawzWin32Output.write_raw   s    

4r(   c                   | j                         }| j                  r|j                  j                  }n-|j                  j
                  |j                  j                  z
  }|j                  j                  |j                  j                  z
  dz   }|j                  j                  dz
  }t        ||      }t        ||      S )Nr   )rowscolumns)rP   rE   dwSizer$   srWindowRightLeftBottomTopminr   )r5   rW   widthheightmaxwidths        r&   get_sizezWin32Output.get_size   s    002
 ""KKMMEMM''$--*<*<<E%%(9(99A= ;;==1$He$ //r(   c                   | j                          t        r| j                  j                  |j                  j                         dz          | j                  j                  ddj                  |D cg c]  }| c}      j                  d      z   dz          | j                  j                  ddj                  |D cg c]  }t        |       c}      j                  d      z   dz          | j                  j                          	  ||i |S c c}w c c}w # t        $ rI}t        r=| j                  j                  d|j                  d|d| dj                                 d}~ww xY w)	z2
        Flush and call win API function.
           
s        z, zutf-8z    Error in r^   
N)	flushrS   rV   rb   r;   encodejointyper   )r5   funcakwies         r&   _winapizWin32Output._winapi   s:    	

HHNNt}}/88:UBCHHNN499%:1h%:;BB7KKeS HHNN))Q7Q{^78??HI
 HHNN	>b>! &; 8  	#-/@!as" MUUWX		s%   3
DDD   	E2)AE--E2c                    | j                          t               }t        j                  j	                  | j
                  t        |            }|r|S t        )z,
        Return Screen buffer info.
        )rv   r   r    rI   GetConsoleScreenBufferInforK   r   r*   )r5   sbinfosuccesss      r&   rP   z(Win32Output.get_win32_screen_buffer_info   sF    , 	

+-//<<MM5=
 M,,r(   c                X    | j                  t        j                  j                  |       y)z%
        Set terminal title.
        Nr   r    rI   SetConsoleTitleW)r5   titles     r&   	set_titlezWin32Output.set_title   s     	V__55u=r(   c                X    | j                  t        j                  j                  d       y )Nr.   r   rZ   s    r&   clear_titlezWin32Output.clear_title   s    V__55r:r(   c                    t        dd      }| j                         }|j                  j                  |j                  j                  z  }| j                  dd       | j                  ||       y )Nr   )rowcolumn)r   rP   rh   r$   r#   cursor_goto_erase)r5   startr   lengths       r&   erase_screenzWin32Output.erase_screen   sV    a2246==??2Qq)E6"r(   c                   | j                         }|j                  }|j                  }|j                  |j                  z
  |j                  |j                  |j                  j                  z
  z  z   }| j                  ||       y N)rP   rh   dwCursorPositionr$   r#   r   )r5   r   sizer   r   s        r&   
erase_downzWin32Output.erase_down   sf    224}}''&&466/TVVtvv8O8O8Q8Q/Q%RRE6"r(   c                    | j                         }|j                  }|j                  j                  |j                  j                  z
  }| j	                  ||       y)r.   N)rP   r   rh   r$   r   )r5   r   r   r   s       r&   erase_end_of_linezWin32Output.erase_end_of_line   sG    224''6#:#:#<#<<E6"r(   c           
        t               }| j                  t        j                  j                  | j
                  t        d      t        |      t        |      t        |             | j                         }| j                  t        j                  j                  | j
                  |j                  |t        |      t        |             y )N    )r
   r   r    rI   FillConsoleOutputCharacterArK   r   r   r'   r   rP   FillConsoleOutputAttributerQ   )r5   r   r   chars_writtenr   s        r&   r   zWin32Output._erase   s    	OO77MM4L&M- 	
 224OO66MM- 	
r(   c                    | j                  t        j                  j                  | j                  | j
                         d| _        y)z.Reset the console foreground/background color.FN)r   r    rI   SetConsoleTextAttributerK   rR   rM   rZ   s    r&   reset_attributeszWin32Output.reset_attributes  s2    OO33T]]DDVDV	
 r(   c           	        |\	  }}}}}}}	}
}t        |      | _        | j                  }|t        j                  k7  rJ|r#|dz  }|| j
                  j                  |      z  }|r#|dz  }|| j
                  j                  |      z  }|
r|dz  |dz  dz  z  |dz  dz	  z  }| j                  t        j                  j                  | j                  |       y )Niii rC         )boolrM   rR   r   DEPTH_1_BITrO   lookup_fg_colorlookup_bg_colorr   r    rI   r   rK   )r5   attrscolor_depthfgcolorbgcolorbold	underlinestrikeitalicblinkreversehidden	win_attrss                r&   set_attributeszWin32Output.set_attributes  s     
	
F| ++	*000%,	T44DDWMM	 %-	T44DDWMM	 U"O)+$*,  	V__<<dmmYWr(   c                     y r    rZ   s    r&   disable_autowrapzWin32Output.disable_autowrapC      r(   c                     y r   r   rZ   s    r&   enable_autowrapzWin32Output.enable_autowrapG  r   r(   c                    t        ||      }| j                  t        j                  j                  | j
                  t        |             y Nr$   r#   )r   r   r    rI   SetConsoleCursorPositionrK   r'   )r5   r   r   poss       r&   r   zWin32Output.cursor_gotoK  s5    f$OO44dmm\RUEV	
r(   c                    | j                         j                  }t        |j                  |j                  |z
        }| j                  t        j                  j                  | j                  t        |             y r   )rP   r   r   r$   r#   r   r    rI   r   rK   r'   r5   amountsrr   s       r&   	cursor_upzWin32Output.cursor_upQ  sU    ..0AAbddbddVm,OO44dmm\RUEV	
r(   c                (    | j                  |        y r   )r   r5   r   s     r&   cursor_downzWin32Output.cursor_downX  s    wr(   c                   | j                         j                  }t        t        d|j                  |z         |j
                        }| j                  t        j                  j                  | j                  t        |             y )Nr   r   )rP   r   r   maxr$   r#   r   r    rI   r   rK   r'   r   s       r&   cursor_forwardzWin32Output.cursor_forward[  s^    ..0AA c!RTTF]+rtt4OO44dmm\RUEV	
r(   c                (    | j                  |        y r   )r   r   s     r&   cursor_backwardzWin32Output.cursor_backwardd  s    VG$r(   c           	        | j                   s| j                  j                          ydj                  | j                         }t        rG| j
                  j                  |j                         dz          | j
                  j                          |D ]I  }t               }t        j                  j                  | j                  |dt        |      d      }|dk7  rIJ  g | _         y)z3
        Write to output stream and flush.
        Nr.   rt   r   r   )rG   rH   rv   rx   rS   rV   rb   rw   r   r    rI   WriteConsoleWrK   r   )r5   ra   bwrittenretvals        r&   rv   zWin32Output.flushg  s     || KKwwt||$HHNNth//1E9:HHNN
  	AgG__22q!U7^TF Q;;	 r(   c                    | j                         }|j                  j                  |j                  j                  z
  dz   S )Nr   )rP   ri   rl   r   r#   )r5   rW   s     r&   get_rows_below_cursor_positionz*Win32Output.get_rows_below_cursor_position  s6    002}}##d&;&;&=&==AAr(   c                4   | j                         }|j                  }|j                  }t               }d|_        |j
                  |j                  z
  |_        |j                  |j                  z
  }d|j                  |j                  z
  cxk  r|dz
  k  rn n|j                  |_        nt        ||j                        |_        |j                  |z
  |_        | j                  t        j                  j                  | j                  dt        |             y)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)rP   ri   r   r   rk   rj   rl   rm   r#   r   r   r    rI   SetConsoleWindowInforK   r   )r5   rW   r   
cursor_posresult
win_heights         r&   scroll_buffer_to_promptz#Win32Output.scroll_buffer_to_prompt  s     002]]**
 xx"'') YY'
ryy:<<'8*q.8IIFM
JLL9FM]]Z/
 	OO00$--uV}	
r(   c                .   | j                   sd}d}t        | j                  t        j                  j
                  ||z  t        d      dt        d      d            }| j                  t        j                  j                  |       || _        d| _         yy)z0
        Go to alternate screen buffer.
        l        i   @r   Nr   T)	rL   r   r   r    rI   CreateConsoleScreenBufferr   SetConsoleActiveScreenBufferrK   )r5   GENERIC_READGENERIC_WRITEhandles       r&   enter_alternate_screenz"Win32Output.enter_alternate_screen  s     ((%L&M OO== =0!H!H	F LLEEvN"DM(,D%% )r(   c                d   | j                   rt        | j                  t        j                  j
                  t                    }| j                  t        j                  j                  |       | j                  t        j                  j                  | j                         || _	        d| _         yy)z6
        Make stdout again the active buffer.
        FN)
rL   r   r   r    rI   rJ   r   r   CloseHandlerK   )r5   rH   s     r&   quit_alternate_screenz!Win32Output.quit_alternate_screen  sz     $$V__99;LMF LLEEvNLL44dmmD"DM(-D% %r(   c                X   d}d}t        t        j                  j                  t                    }t               }| j                  t        j                  j                  |t        |             | j                  t        j                  j                  ||j                  |z  | z         y )N   @   r   r    rI   rJ   r   r   r   GetConsoleModer   SetConsoleModevalue)r5   ENABLE_MOUSE_INPUTENABLE_QUICK_EDIT_MODEr   original_modes        r&   enable_mouse_supportz Win32Output.enable_mouse_support  s    !
 "(445EFGV__33VW]=STOO**  #55:P9PP	
r(   c                N   d}t        t        j                  j                  t                    }t               }| j                  t        j                  j                  |t        |             | j                  t        j                  j                  ||j                  | z         y )Nr   r   )r5   r   r   r   s       r&   disable_mouse_supportz!Win32Output.disable_mouse_support  sv    !445EFGV__33VW]=STOO**#5"55	
r(   c                     y r   r   rZ   s    r&   hide_cursorzWin32Output.hide_cursor      r(   c                     y r   r   rZ   s    r&   show_cursorzWin32Output.show_cursor  r   r(   c                     y r   r   )r5   cursor_shapes     r&   set_cursor_shapezWin32Output.set_cursor_shape  r   r(   c                     y r   r   rZ   s    r&   reset_cursor_shapezWin32Output.reset_cursor_shape  r   r(   c                    t        t        j                  j                               }d}t        j                  j                  |ddt        |             y)a$  
        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    rI   GetConsoleWindowuser32RedrawWindowr	   )clsr   RDW_INVALIDATEs      r&   win32_refresh_windowz Win32Output.win32_refresh_window  s=     88:;""64vn7MNr(   c                R    | j                   | j                   S t        j                  S )z
        Return the default color depth for a windows terminal.

        Contrary to the Vt100 implementation, this doesn't depend on a $TERM
        variable.
        )rF   r   DEPTH_4_BITrZ   s    r&   get_default_color_depthz#Win32Output.get_default_color_depth  s(     ##/+++%%%r(   )FN)rH   r   rE   r   rF   zColorDepth | Noner9   r:   )r9   int)r9   str)ra   r  r9   r:   )r9   r   )rz   zCallable[..., _T]r{   objectr|   r  r9   r@   )r9   r   )r   r  r9   r:   r8   )r   r   r   r  r9   r:   )r   r   r   r   r9   r:   )r   r   )r   r  r   r  r9   r:   )r   r  r9   r:   )r   r   r9   r:   )r9   r   ))r;   r<   r=   r>   r4   rY   r\   rb   rd   rr   r   rP   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   r   r   r   r   r   r   r   r   r   classmethodr  r  r   r(   r&   r!   r!   Z   s    $)15	AA !A /	A
 
A2$$"0(4"-H>;###
.$XL

 
%<B
<-0.
$

 O O
&r(   c                  0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)FOREGROUND_COLORr   r   r      r               Nr;   r<   r=   BLACKBLUEGREENCYANREDMAGENTAYELLOWGRAY	INTENSITYr   r(   r&   r  r    /    EDED
CGFDIr(   r  c                  0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)BACKGROUND_COLORr   r       0   r   P   `   p      Nr  r   r(   r&   r  r    r  r(   r  c                   i d| j                   d| j                   d| j                  d| j                   | j                  z  d| j                  | j                  z  d| j                  d| j                  d| j
                  d	| j                  d
| j                  d| j                  d| j                  | j                  z  d| j                  | j                  z  d| j
                  | j                  z  d| j                  | j                  z  d| j                  | j                  z  d| j                  | j                  z  S )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  r  r  r  r  r  r  r  r  )	color_clss    r&   _create_ansi_color_dictr7  *  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                  J    e Zd ZdZd	dZed
d       ZddZddZddZ	ddZ
y)rN   z8
    Inspired by pygments/formatters/terminal256.py
    c                <    | j                         | _        i | _        y r   )_build_color_table_win32_colors
best_matchrZ   s    r&   r4   zColorLookupTable.__init__Q  s    !446 79r(   c                    t         } t        }ddd| j                  |j                  fddd| j                  |j                  fddd| j                  |j                  fddd| j
                  |j
                  fddd| j                  |j                  fddd| j                  |j                  fddd| j                  |j                  fddd| j                  |j                  fddd| j                  | j                  z  |j                  |j                  z  fddd| j                  | j                  z  |j                  |j                  z  fddd| j
                  | j                  z  |j
                  |j                  z  fddd| j                  | j                  z  |j                  |j                  z  fddd| j                  | j                  z  |j                  |j                  z  fddd| j                  | j                  z  |j                  |j                  z  fddd| j                  | j                  z  |j                  |j                  z  fddd| j                  | j                  z  |j                  |j                  z  fgS )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&   r:  z#ColorLookupTable._build_color_tableW  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                    d}d}d}| j                   D ]4  \  }}}	}
}||z
  }||z
  }||	z
  }||z  ||z  z   ||z  z   }||k  s/|
}|}|}6 ||fS )Ni r   )r;  )r5   rgr   distancefg_matchbg_matchr_g_b_fg_bg_rdgdbdds                   r&   _closest_colorzColorLookupTable._closest_colorr  s     $($6$6 
	 BBSRBRBRBR"r'!BG+A8|
	 !!r(   c                    | j                   j                  |d       }|M	 t        t        |      d      }|dz	  dz  }|dz	  dz  }|dz  }| j                  |||      }|| j                   |<   |S # t        $ r d}Y Fw xY w)Nr   r   rA  r  )r<  r1   r  r  
ValueErrorrS  )r5   colorindexesrgbrE  rF  r   s          r&   _color_indexeszColorLookupTable._color_indexes  s    //%%eT2?#e*b) d"AT!Ad
A))!Q2G%,DOOE"  s   A. .A<;A<c                L    |t         v r	t         |   S | j                  |      d   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_COLORSrY  )r5   fg_colors     r&   r   z ColorLookupTable.lookup_fg_color  -     ~%!(++&&x033r(   c                L    |t         v r	t         |   S | j                  |      d   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_COLORSrY  )r5   bg_colors     r&   r   z ColorLookupTable.lookup_bg_color  r]  r(   Nr8   )r9   z$list[tuple[int, int, int, int, int]])rE  r  rF  r  r   r  r9   tuple[int, int])rV  r  r9   ra  )r\  r  r9   r  )r`  r  r9   r  )r;   r<   r=   r>   r4   staticmethodr:  rS  rY  r   r   r   r(   r&   rN   rN   L  s4    9 
 
4"$44r(   rN   )r%   r   r9   r   )r6  z/type[FOREGROUND_COLOR] | type[BACKGROUND_COLOR]r9   zdict[str, int])9
__future__r   sysplatformr/   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'   rS   rU   	Exceptionr*   r@   r!   r  r  r7  r[  r_  setrN   r   r(   r&   <module>rs     s   " 

||w  	 Q Q Q ) , , 4 / 9 +  +  #  
8*   D " ". T]u&& u&p	 		 	>6 ))9:()9:
>c"233 33
>c"233 33_4 _4r(   