o
    �õ±jŠX  ã                   @  s®  d dl mZ d dlZejdksJ ‚d dlZd dlm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 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d„Z,dZ-dZ.G dd„ de/ƒZ0edƒZ1G dd„ de'ƒZ2G d d!„ d!ƒZ3G d"d#„ d#ƒZ4d,d'd(„Z5e5e3ƒZ6e5e4ƒZ7e8e6ƒe8eƒksÄJ ‚e8e7ƒe8eƒksÎJ ‚G d)d*„ d*ƒZ9dS )-é    )ÚannotationsNÚwin32)ÚCallable)ÚArgumentErrorÚbyrefÚc_charÚc_longÚc_uintÚc_ulongÚpointer)ÚDWORDÚHANDLE)Ú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ÚWin32OutputÚcoordr   Úreturnr   c                 C  s   t | jd | jd@ B ƒ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)r!   © r%   úŽ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/output/win32.pyÚ_coord_byval(   s   r'   Fz!prompt-toolkit-windows-output.logc                      s"   e Zd ZdZd‡ fdd„Z‡  ZS )ÚNoConsoleScreenBufferErrorz�
    Raised when the application is not running inside a Windows Console, but
    the user tries to instantiate Win32Output.
    r"   ÚNonec                   s<   dt j dd¡v }|rd t jd ¡}nd}tƒ  |¡ d S )NÚxtermÚTERMÚ zÏFound {}, 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__r%   r&   r2   G   s   ûz#NoConsoleScreenBufferError.__init__©r"   r)   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   Ú__classcell__r%   r%   r5   r&   r(   A   s    r(   Ú_Tc                   @  s„  e Zd ZdZ		dmdndd„Zdodd„Zdpdd„Zdqdd„Zdqdd„Zdrdd„Z	dsd"d#„Z
dtd%d&„Zdud(d)„Zdvd*d+„Zdvd,d-„Zdvd.d/„Zdvd0d1„Zdwd5d6„Zdvd7d8„Zdxd=d>„Zdvd?d@„ZdvdAdB„ZdydzdFdG„Zd{dIdJ„Zd{dKdL„Zd{dMdN„Zd{dOdP„ZdvdQdR„ZdodSdT„ZdvdUdV„ZdvdWdX„ZdvdYdZ„Zdvd[d\„Z dvd]d^„Z!dvd_d`„Z"dvdadb„Z#d|dedf„Z$dvdgdh„Z%e&dvdidj„ƒZ'd}dkdl„Z(dS )~r    zW
    I/O abstraction for rendering to Windows consoles.
    (cmd.exe and similar.)
    FNÚstdoutr   Úuse_complete_widthÚboolÚdefault_color_depthúColorDepth | Noner"   r)   c                 C  sn   || _ || _g | _|| _ttj t¡ƒ| _	d| _
d| _tƒ | _|  ¡ }|r(|jnd| _tr5ttdƒ| _d S d S )NFé   Úab)r?   rA   Ú_bufferr>   r   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)r3   r>   r?   rA   Úinfor%   r%   r&   r2   a   s   ÿzWin32Output.__init__Úintc                 C  s
   | j  ¡ S )zReturn file descriptor.)r>   Úfileno©r3   r%   r%   r&   rV   z   s   
zWin32Output.filenoÚstrc                 C  s   | j jS )z Return encoding used for stdout.)r>   ÚencodingrW   r%   r%   r&   rY   ~   s   zWin32Output.encodingÚdatac                 C  s"   | j r	dt|ƒ }| j |¡ d S )Nú )rJ   r   rE   Úappend©r3   rZ   r%   r%   r&   Úwrite‚   s   zWin32Output.writec                 C  s   |   |¡ dS )z>For win32, there is no difference between write and write_raw.N)r^   r]   r%   r%   r&   Ú	write_rawˆ   s   zWin32Output.write_rawr   c                 C  s^   |   ¡ }| jr|jj}n|jj|jj }|jj|jj d }|jjd }t	||ƒ}t
||d�S )Nr   )ÚrowsÚcolumns)rM   r?   ÚdwSizer$   ÚsrWindowÚRightÚLeftÚBottomÚTopÚminr   )r3   rT   ÚwidthÚheightÚmaxwidthr%   r%   r&   Úget_sizeŒ   s   

zWin32Output.get_sizeÚfuncúCallable[..., _T]ÚaÚobjectÚkwr=   c                 O  sØ   |   ¡  trA| j |j› ¡ d ¡ | j dd dd„ |D ƒ¡ d¡ d ¡ | j dd dd„ |D ƒ¡ d¡ d ¡ | j  ¡  z||i |¤ŽW S  tyk } ztrf| j d|j›d	|›d	|› d
� ¡ ¡ ‚ d}~ww )z2
        Flush and call win API function.
        ó   
s        z, c                 S  s   g | ]}|›‘qS r%   r%   ©Ú.0Úir%   r%   r&   Ú
<listcomp>©   s    z'Win32Output._winapi.<locals>.<listcomp>zutf-8c                 S  s   g | ]}t |ƒ›‘qS r%   )Útypers   r%   r%   r&   rv   ­   s    z    Error in r[   Ú
N)ÚflushrP   rS   r^   r8   ÚencodeÚjoinr   )r3   rm   ro   rq   Úer%   r%   r&   Ú_winapi    s,    ÿÿþÿ
&€üzWin32Output._winapir   c                 C  s.   |   ¡  tƒ }tj | jt|ƒ¡}|r|S t‚)z,
        Return Screen buffer info.
        )ry   r   r   rF   ÚGetConsoleScreenBufferInforH   r   r(   )r3   ÚsbinfoÚsuccessr%   r%   r&   rM   º   s   
ÿz(Win32Output.get_win32_screen_buffer_infoÚtitlec                 C  s   |   tjj|¡ dS )z%
        Set terminal title.
        N©r}   r   rF   ÚSetConsoleTitleW)r3   r�   r%   r%   r&   Ú	set_titleÞ   s   zWin32Output.set_titlec                 C  s   |   tjjd¡ d S )Nr,   r‚   rW   r%   r%   r&   Úclear_titleä   s   zWin32Output.clear_titlec                 C  s@   t ddƒ}|  ¡ }|jj|jj }| jddd� |  ||¡ d S )Nr   )ÚrowÚcolumn)r   rM   rb   r$   r#   Úcursor_gotoÚ_erase)r3   Ústartr   Úlengthr%   r%   r&   Úerase_screenç   s
   
zWin32Output.erase_screenc                 C  sD   |   ¡ }|j}|j}|j|j |j|j|jj   }|  ||¡ d S ©N)rM   rb   ÚdwCursorPositionr$   r#   r‰   )r3   r   ÚsizerŠ   r‹   r%   r%   r&   Ú
erase_downï   s
    zWin32Output.erase_downc                 C  s.   |   ¡ }|j}|jj|jj }|  ||¡ dS )r,   N)rM   rŽ   rb   r$   r‰   )r3   r   rŠ   r‹   r%   r%   r&   Úerase_end_of_lineø   s   zWin32Output.erase_end_of_linerŠ   r   r‹   c              	   C  s`   t ƒ }|  tjj| jtdƒt|ƒt|ƒt	|ƒ¡ |  
¡ }|  tjj| j|j|t|ƒt	|ƒ¡ d S )Nó    )r
   r}   r   rF   ÚFillConsoleOutputCharacterArH   r   r   r'   r   rM   ÚFillConsoleOutputAttributerN   )r3   rŠ   r‹   Úchars_writtenr   r%   r%   r&   r‰      s$   ú
úzWin32Output._erasec                 C  s    |   tjj| j| j¡ d| _dS )z.Reset the console foreground/background color.FN)r}   r   rF   ÚSetConsoleTextAttributerH   rO   rJ   rW   r%   r%   r&   Úreset_attributes  s   ÿ
zWin32Output.reset_attributesÚattrsr   Úcolor_depthr   c              
   C  s¦   |\
}}}}}}}	}
}}t |ƒ| _| j}|tjkr5|r'|d@ }|| j |¡O }|r5|d@ }|| j |¡O }|
rG|d@ |d@ d> B |d@ d? B }|  t	j
j| j|¡ d S )Niðÿÿÿiÿÿÿi ÿÿÿrC   é   éð   )r@   rJ   rO   r   ÚDEPTH_1_BITrL   Úlookup_fg_colorÚlookup_bg_colorr}   r   rF   r–   rH   )r3   r˜   r™   ÚfgcolorÚbgcolorÚboldÚ	underlineÚstrikeÚitalicÚblinkÚreverseÚhiddenÚdimÚ	win_attrsr%   r%   r&   Úset_attributes  s:   õ


ÿ
þÿzWin32Output.set_attributesc                 C  ó   d S r�   r%   rW   r%   r%   r&   Údisable_autowrapE  ó   zWin32Output.disable_autowrapc                 C  r«   r�   r%   rW   r%   r%   r&   Úenable_autowrapI  r­   zWin32Output.enable_autowrapr   r†   r‡   c                 C  s(   t ||d�}|  tjj| jt|ƒ¡ d S ©N©r$   r#   )r   r}   r   rF   ÚSetConsoleCursorPositionrH   r'   )r3   r†   r‡   Úposr%   r%   r&   rˆ   M  s   ÿzWin32Output.cursor_gotoÚamountc                 C  s:   |   ¡ j}t|j|j| d�}|  tjj| j	t
|ƒ¡ d S r¯   )rM   rŽ   r   r$   r#   r}   r   rF   r±   rH   r'   ©r3   r³   Úsrr²   r%   r%   r&   Ú	cursor_upS  s
   
ÿzWin32Output.cursor_upc                 C  ó   |   | ¡ d S r�   )r¶   ©r3   r³   r%   r%   r&   Úcursor_downZ  ó   zWin32Output.cursor_downc                 C  s@   |   ¡ j}ttd|j| ƒ|jd�}|  tjj	| j
t|ƒ¡ d S )Nr   r°   )rM   rŽ   r   Úmaxr$   r#   r}   r   rF   r±   rH   r'   r´   r%   r%   r&   Úcursor_forward]  s
   
ÿzWin32Output.cursor_forwardc                 C  r·   r�   )r¼   r¸   r%   r%   r&   Úcursor_backwardf  rº   zWin32Output.cursor_backwardc                 C  s„   | j s
| j ¡  dS d | j ¡}tr"| j |› ¡ d ¡ | j ¡  |D ]}tƒ }t	j
 | j|dt|ƒd¡}|dks<J ‚q$g | _ dS )z3
        Write to output stream and flush.
        Nr,   rr   r   r   )rE   r>   ry   r{   rP   rS   r^   rz   r   r   rF   ÚWriteConsoleWrH   r   )r3   rZ   ÚbÚwrittenÚretvalr%   r%   r&   ry   i  s   

ÿ
zWin32Output.flushc                 C  s   |   ¡ }|jj|jj d S )Nr   )rM   rc   rf   rŽ   r#   )r3   rT   r%   r%   r&   Úget_rows_below_cursor_position‡  s   z*Win32Output.get_rows_below_cursor_positionc                 C  s    |   ¡ }|j}|j}tƒ }d|_|j|j |_|j|j }d|j|j   k r-|d k r4n n|j|_nt	||jƒ|_|j| |_|  
tjj| jdt|ƒ¡ dS )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)rM   rc   rŽ   r   re   rd   rf   rg   r#   r»   r}   r   rF   ÚSetConsoleWindowInforH   r   )r3   rT   rµ   Ú
cursor_posÚresultÚ
win_heightr%   r%   r&   Úscroll_buffer_to_prompt‹  s   $
ÿz#Win32Output.scroll_buffer_to_promptc              	   C  sZ   | j s+d}d}t|  tjj||B tdƒdtdƒd¡ƒ}|  tjj|¡ || _d| _ dS dS )z0
        Go to alternate screen buffer.
        l        i   @r   Nr   T)	rI   r   r}   r   rF   ÚCreateConsoleScreenBufferr   ÚSetConsoleActiveScreenBufferrH   )r3   ÚGENERIC_READÚGENERIC_WRITEÚhandler%   r%   r&   Úenter_alternate_screen©  s"   úÿ
îz"Win32Output.enter_alternate_screenc                 C  sP   | j r&t|  tjjt¡ƒ}|  tjj|¡ |  tjj| j	¡ || _	d| _ dS dS )z6
        Make stdout again the active buffer.
        FN)
rI   r   r}   r   rF   rG   r   rÉ   ÚCloseHandlerH   )r3   r>   r%   r%   r&   Úquit_alternate_screenÁ  s   ÿ
ùz!Win32Output.quit_alternate_screenc                 C  sV   d}d}t tj t¡ƒ}tƒ }|  tjj|t|ƒ¡ |  tjj	||j
|B | @ ¡ d S )Né   é@   ©r   r   rF   rG   r   r   r}   ÚGetConsoleModer   ÚSetConsoleModeÚvalue)r3   ÚENABLE_MOUSE_INPUTÚENABLE_QUICK_EDIT_MODErÌ   Úoriginal_moder%   r%   r&   Úenable_mouse_supportÎ  s   ýz Win32Output.enable_mouse_supportc                 C  sN   d}t tj t¡ƒ}tƒ }|  tjj|t|ƒ¡ |  tjj	||j
| @ ¡ d S )NrÐ   rÒ   )r3   rÖ   rÌ   rØ   r%   r%   r&   Údisable_mouse_supportà  s   
ýz!Win32Output.disable_mouse_supportc                 C  r«   r�   r%   rW   r%   r%   r&   Úhide_cursorì  ó   zWin32Output.hide_cursorc                 C  r«   r�   r%   rW   r%   r%   r&   Úshow_cursorï  rÜ   zWin32Output.show_cursorÚcursor_shaper   c                 C  r«   r�   r%   )r3   rÞ   r%   r%   r&   Úset_cursor_shapeò  rÜ   zWin32Output.set_cursor_shapec                 C  r«   r�   r%   rW   r%   r%   r&   Úreset_cursor_shapeõ  rÜ   zWin32Output.reset_cursor_shapec                 C  s,   t tj ¡ ƒ}d}tj |ddt|ƒ¡ dS )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   rF   ÚGetConsoleWindowÚuser32ÚRedrawWindowr	   )ÚclsrÌ   ÚRDW_INVALIDATEr%   r%   r&   Úwin32_refresh_windowø  s   
z Win32Output.win32_refresh_windowc                 C  s   | j dur| 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.
        N)rA   r   ÚDEPTH_4_BITrW   r%   r%   r&   Úget_default_color_depth  s   
z#Win32Output.get_default_color_depth)FN)r>   r   r?   r@   rA   rB   r"   r)   )r"   rU   )r"   rX   )rZ   rX   r"   r)   )r"   r   )rm   rn   ro   rp   rq   rp   r"   r=   )r"   r   )r�   rX   r"   r)   r7   )rŠ   r   r‹   rU   r"   r)   )r˜   r   r™   r   r"   r)   )r   r   )r†   rU   r‡   rU   r"   r)   )r³   rU   r"   r)   )rÞ   r   r"   r)   )r"   r   ))r8   r9   r:   r;   r2   rV   rY   r^   r_   rl   r}   rM   r„   r…   rŒ   r�   r‘   r‰   r—   rª   r¬   r®   rˆ   r¶   r¹   r¼   r½   ry   rÂ   rÇ   rÍ   rÏ   rÙ   rÚ   rÛ   rÝ   rß   rà   Úclassmethodræ   rè   r%   r%   r%   r&   r    [   sR    ü







$



	



'




	










c                   @  ó0   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )ÚFOREGROUND_COLORr   r   r   é   rš   é   é   é   é   N©r8   r9   r:   ÚBLACKÚBLUEÚGREENÚCYANÚREDÚMAGENTAÚYELLOWÚGRAYÚ	INTENSITYr%   r%   r%   r&   rë     ó    rë   c                   @  rê   )ÚBACKGROUND_COLORr   rÐ   é    é0   rÑ   éP   é`   ép   é€   Nrñ   r%   r%   r%   r&   rü      rû   rü   Ú	color_clsú/type[FOREGROUND_COLOR] | type[BACKGROUND_COLOR]údict[str, int]c                 C  s¼   i d| j “d| j “d| j“d| j | jB “d| j| jB “d| j“d| j“d| j“d	| j“d
| j“d| j“d| j| jB “d| j| jB “d| j| jB “d| j| jB “d| j| jB “d| j| jB “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õ   )r  r%   r%   r&   Ú_create_ansi_color_dict,  sF   ÿþýüûùø	÷
öõôòñðïîír  c                   @  sP   e Zd ZdZddd„Zeddd„ƒZddd„Zddd„Zddd„Z	d dd„Z
dS )!rK   z8
    Inspired by pygments/formatters/terminal256.py
    r"   r)   c                 C  s   |   ¡ | _i | _d S r�   )Ú_build_color_tableÚ_win32_colorsÚ
best_matchrW   r%   r%   r&   r2   S  s   

zColorLookupTable.__init__ú$list[tuple[int, int, int, int, int]]c                  C  sl  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
B |j|j
B fddd| j| j
B |j|j
B fddd| j| j
B |j|j
B fddd| j| j
B |j|j
B fddd| j| j
B |j|j
B fddd| j| j
B |j|j
B fddd| j| j
B |j|j
B fddd| j	| j
B |j	|j
B 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ÚBGr%   r%   r&   r  Y  s&   ðz#ColorLookupTable._build_color_tableÚrrU   Úgr¿   útuple[int, int]c                 C  sn   d}d}d}| j D ])\}}}	}
}|| }|| }||	 }|| ||  ||  }||k r2|
}|}|}q	||fS )Ni r   )r  )r3   r"  r#  r¿   ÚdistanceÚfg_matchÚbg_matchÚr_Úg_Úb_Úfg_Úbg_ÚrdÚgdÚbdÚdr%   r%   r&   Ú_closest_colort  s   €zColorLookupTable._closest_colorÚcolorrX   c                 C  s|   | j  |d ¡}|d u r<z	tt|ƒdƒ}W n ty   d}Y nw |d? d@ }|d? d@ }|d@ }|  |||¡}|| j |< |S )NrÐ   r   r  rð   )r  r/   rU   rX   Ú
ValueErrorr1  )r3   r2  ÚindexesÚrgbr"  r#  r¿   r%   r%   r&   Ú_color_indexes†  s   ÿ
zColorLookupTable._color_indexesÚfg_colorc                 C  ó   |t v rt | S |  |¡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_COLORSr6  )r3   r7  r%   r%   r&   r�   •  ó   z ColorLookupTable.lookup_fg_colorÚbg_colorc                 C  r8  )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_COLORSr6  )r3   r;  r%   r%   r&   rž   ¢  r:  z ColorLookupTable.lookup_bg_colorNr7   )r"   r  )r"  rU   r#  rU   r¿   rU   r"   r$  )r2  rX   r"   r$  )r7  rX   r"   rU   )r;  rX   r"   rU   )r8   r9   r:   r;   r2   Ústaticmethodr  r1  r6  r�   rž   r%   r%   r%   r&   rK   N  s    



rK   )r!   r   r"   r   )r  r  r"   r  ):Ú
__future__r   ÚsysÚplatformr-   Úcollections.abcr   Úctypesr   r   r   r   r	   r
   r   Úctypes.wintypesr   r   Útypingr   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'   rP   rR   Ú	Exceptionr(   r=   r    rë   rü   r  r9  r<  ÚsetrK   r%   r%   r%   r&   Ú<module>   sJ    $ÿ
   <
