
    %efX                    V   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!          Z3d)d%Z4 e4e2          Z5 e4e3          Z6 e7e5           e7e          k    sJ  e7e6           e7e          k    sJ  G d& d'          Z8dS )*    )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Win32Outputcoordr   returnr   c                F    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)r"   s    ;lib/python3.11/site-packages/prompt_toolkit/output/win32.py_coord_byvalr(   '   s%     %'G#eg&667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.
    r#   Nonec                    dt           j                            dd          v }|rdt           j        d         z  }nd}t                                          |           d S )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super__init__)selfr.   message	__class__s      r'   r5   z#NoConsoleScreenBufferError.__init__F   sj    2:>>&"555 		K/ 24F1C	D G KG!!!!!r)   r#   r,   )__name__
__module____qualname____doc__r5   __classcell__)r8   s   @r'   r+   r+   @   sG         
" " " " " " " " " "r)   r+   _Tc                  J   e Zd ZdZ	 	 dIdJdZdKdZdLdZdMdZdMdZdNdZ	dOdZ
dPdZdQd ZdRd!ZdRd"ZdRd#ZdRd$ZdSd(ZdRd)ZdTd.ZdRd/ZdRd0ZdUdVd4ZdWd6ZdWd7ZdWd8ZdWd9ZdRd:ZdKd;ZdRd<ZdRd=ZdRd>ZdRd?Z dRd@Z!dRdAZ"dRdBZ#dXdEZ$dRdFZ%e&dRdG            Z'dYdHZ(dS )Zr!   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                z   || _         || _        g | _        || _        t	          t
          j                            t                              | _	        d| _
        d| _        t                      | _        |                                 }|r|j        nd| _        t"          rt%          t&          d          | _        d S d S )NF   ab)rB   rD   _bufferrA   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)r6   rA   rB   rD   infos        r'   r5   zWin32Output.__init__`   s     #5#6 "$$v;;<MNNOO$)!"2"4"4 002215=T--2 	A94@@DHHH	A 	Ar)   intc                4    | j                                         S )zReturn file descriptor.)rA   filenor6   s    r'   r[   zWin32Output.filenoy   s    {!!###r)   strc                    | j         j        S )z Return encoding used for stdout.)rA   encodingr\   s    r'   r_   zWin32Output.encoding}   s    {##r)   datac                l    | j         rdt          |          z  }| j                            |           d S )N )rN   r   rI   appendr6   r`   s     r'   writezWin32Output.write   s:    < 	*D)))DD!!!!!r)   c                0    |                      |           dS )z>For win32, there is no difference between write and write_raw.N)re   rd   s     r'   	write_rawzWin32Output.write_raw   s    

4r)   r   c                   |                                  }| 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)rQ   rB   dwSizer&   srWindowRightLeftBottomTopminr   )r6   rX   widthheightmaxwidths        r'   get_sizezWin32Output.get_size   s    0022
 " 	=KMEEM'$-*<<E%(99A= ;=1$He$$ ////r)   funcCallable[..., _T]aobjectkwr?   c           
        |                                   t          r| j                            d|j        z                      d          dz              | j                            dd                    d |D                                           d          z   dz              | j                            dd                    d |D                                           d          z   dz              | j                                          	  ||i |S # t          $ rH}t          r;| j                            d|j        d	|d	| d
                                            d}~ww xY w)z2
        Flush and call win API function.
        %rutf-8   
s        z, c                    g | ]}d |z  S r|    .0is     r'   
<listcomp>z'Win32Output._winapi.<locals>.<listcomp>   s    %:%:%:1dQh%:%:%:r)   c                2    g | ]}d t          |          z  S r   )typer   s     r'   r   z'Win32Output._winapi.<locals>.<listcomp>   s"    777TDGG^777r)   z    Error in rb   
N)flushrT   rW   re   r:   encodejoinr   )r6   rv   rx   rz   es        r'   _winapizWin32Output._winapi   s    	

 
	HNND4=088AAEIJJJHNN499%:%:%:%:%:;;BB7KKKeS   HNN))77Q77788??HHI  
 HNN	4>b>>! 	 	 	# Y M M M! M M M M MUUWWXXX		s   D 
E( AE##E(r   c                    |                                   t                      }t          j                            | j        t          |                    }|r|S t          )z,
        Return Screen buffer info.
        )r   r   r    rJ   GetConsoleScreenBufferInforL   r   r+   )r6   sbinfosuccesss      r'   rQ   z(Win32Output.get_win32_screen_buffer_info   sU    , 	

+--/<<M5==
 
  	-M,,r)   titlec                P    |                      t          j        j        |           dS )z%
        Set terminal title.
        Nr   r    rJ   SetConsoleTitleW)r6   r   s     r'   	set_titlezWin32Output.set_title   s#     	V_5u=====r)   c                P    |                      t          j        j        d           d S )Nr0   r   r\   s    r'   clear_titlezWin32Output.clear_title   s!    V_5r:::::r)   c                    t          dd          }|                                 }|j        j        |j        j        z  }|                     dd           |                     ||           d S )Nr   )rowcolumn)r   rQ   rk   r&   r%   cursor_goto_erase)r6   startr   lengths       r'   erase_screenzWin32Output.erase_screen   se    a22446=?2Qq)))E6"""""r)   c                    |                                  }|j        }|j        }|j        |j        z
  |j        |j        |j        j        z
  z  z   }|                     ||           d S N)rQ   rk   dwCursorPositionr&   r%   r   )r6   r   sizer   r   s        r'   
erase_downzWin32Output.erase_down   sb    2244}'&46/TVtv8O8Q/Q%RRE6"""""r)   c                    |                                  }|j        }|j        j        |j        j        z
  }|                     ||           dS )r0   N)rQ   r   rk   r&   r   )r6   r   r   r   s       r'   erase_end_of_linezWin32Output.erase_end_of_line   sI    2244'6#:#<<E6"""""r)   r   r   r   c           
        t                      }|                     t          j        j        | j        t          d          t          |          t          |          t          |                     | 
                                }|                     t          j        j        | j        |j        |t          |          t          |                     d S )N    )r
   r   r    rJ   FillConsoleOutputCharacterArL   r   r   r(   r   rQ   FillConsoleOutputAttributerR   )r6   r   r   chars_writtenr   s        r'   r   zWin32Output._erase   s    		O7M4LL&MM-  	
 	
 	
 2244O6M-  	
 	
 	
 	
 	
r)   c                t    |                      t          j        j        | j        | j                   d| _        dS )z.Reset the console foreground/background color.FN)r   r    rJ   SetConsoleTextAttributerL   rS   rN   r\   s    r'   reset_attributeszWin32Output.reset_attributes  s6    O3T]DDV	
 	
 	
 r)   attrsr   color_depthr   c           	        |\	  }}}}}}}	}
}t          |          | _        | j        }|t          j        k    rH|r"|dz  }|| j                            |          z  }|r"|dz  }|| j                            |          z  }|
r|dz  |dz  dz  z  |dz  dz	  z  }|                     t          j
        j        | j        |           d S )Niii rG         )rC   rN   rS   r   DEPTH_1_BITrP   lookup_fg_colorlookup_bg_colorr   r    rJ   r   rL   )r6   r   r   fgcolorbgcolorbold	underlinestrikeitalicblinkreversehidden	win_attrss                r'   set_attributeszWin32Output.set_attributes  s	    
	
F|| +	*000 N%,	T4DDWMMM	  N%-	T4DDWMMM	  	U"O)+$*,  	V_<dmYWWWWWr)   c                    d S r   r   r\   s    r'   disable_autowrapzWin32Output.disable_autowrapC      r)   c                    d S r   r   r\   s    r'   enable_autowrapzWin32Output.enable_autowrapG  r   r)   r   r   r   c                    t          ||          }|                     t          j        j        | j        t          |                     d S Nr&   r%   )r   r   r    rJ   SetConsoleCursorPositionrL   r(   )r6   r   r   poss       r'   r   zWin32Output.cursor_gotoK  sK    f$$$O4dm\RUEVEV	
 	
 	
 	
 	
r)   amountc                    |                                  j        }t          |j        |j        |z
            }|                     t          j        j        | j	        t          |                     d S r   )rQ   r   r   r&   r%   r   r    rJ   r   rL   r(   r6   r   srr   s       r'   	cursor_upzWin32Output.cursor_upQ  sg    ..00AbdbdVm,,,O4dm\RUEVEV	
 	
 	
 	
 	
r)   c                2    |                      |            d S r   )r   r6   r   s     r'   cursor_downzWin32Output.cursor_downX  s    wr)   c                    |                                  j        }t          t          d|j        |z             |j                  }|                     t          j        j	        | j
        t          |                     d S )Nr   r   )rQ   r   r   maxr&   r%   r   r    rJ   r   rL   r(   r   s       r'   cursor_forwardzWin32Output.cursor_forward[  ss    ..00A c!RTF]++rt444O4dm\RUEVEV	
 	
 	
 	
 	
r)   c                2    |                      |            d S r   )r   r   s     r'   cursor_backwardzWin32Output.cursor_backwardd  s    VG$$$$$r)   c           	        | j         s| j                                         dS d                    | j                   }t          rL| j                            d|z                      d          dz              | j                                         |D ]M}t                      }t          j
                            | j        |dt          |          d          }|dk    sJ Ng | _         dS )z3
        Write to output stream and flush.
        Nr0   r|   r}   r~   r   r   )rI   rA   r   r   rT   rW   re   r   r   r    rJ   WriteConsoleWrL   r   )r6   r`   bwrittenretvals        r'   r   zWin32Output.flushg  s     | 	 KFwwt|$$ 	HNND4K//885@AAAHNN
  	 	AggG_22q!U7^^T F Q;;;;;r)   c                b    |                                  }|j        j        |j        j        z
  dz   S )Nr   )rQ   rl   ro   r   r%   )r6   rX   s     r'   get_rows_below_cursor_positionz*Win32Output.get_rows_below_cursor_position  s.    0022}#d&;&==AAr)   c                   |                                  }|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
  |_        | 
                    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)rQ   rl   r   r   rn   rm   ro   rp   r%   r   r   r    rJ   SetConsoleWindowInforL   r   )r6   rX   r   
cursor_posresult
win_heights         r'   scroll_buffer_to_promptz#Win32Output.scroll_buffer_to_prompt  s     0022]*
 x"') Y'
ry:<'8888*q.88888IFMM
JL99FM]Z/
 	O0$-uV}}	
 	
 	
 	
 	
r)   c                ,   | j         sd}d}t          |                     t          j        j        ||z  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)	rM   r   r   r    rJ   CreateConsoleScreenBufferr   SetConsoleActiveScreenBufferrL   )r6   GENERIC_READGENERIC_WRITEhandles       r'   enter_alternate_screenz"Win32Output.enter_alternate_screen  s     ( 	-%L&M O= =0!HH!HH 	 	F LLEvNNN"DM(,D%%%%	- 	-r)   c                @   | j         rt          |                     t          j        j        t                              }|                     t          j        j        |           |                     t          j        j        | j	                   || _	        d| _         dS dS )z6
        Make stdout again the active buffer.
        FN)
rM   r   r   r    rJ   rK   r   r   CloseHandlerL   )r6   rA   s     r'   quit_alternate_screenz!Win32Output.quit_alternate_screen  s     $ 	.V_9;LMM F LLEvNNNLL4dmDDD"DM(-D%%%	. 	.r)   c                V   d}d}t          t          j                            t                              }t                      }|                     t          j        j        |t          |                     |                     t          j        j	        ||j
        |z  | z             d S )N   @   r   r    rJ   rK   r   r   r   GetConsoleModer   SetConsoleModevalue)r6   ENABLE_MOUSE_INPUTENABLE_QUICK_EDIT_MODEr   original_modes        r'   enable_mouse_supportz Win32Output.enable_mouse_support  s    !
 "(445EFFGGV_3VW]=S=STTTO* #55:P9PP	
 	
 	
 	
 	
r)   c                L   d}t          t          j                            t                              }t                      }|                     t          j        j        |t          |                     |                     t          j        j	        ||j
        | z             d S )Nr   r   )r6   r   r   r   s       r'   disable_mouse_supportz!Win32Output.disable_mouse_support  s    !445EFFGGV_3VW]=S=STTTO*#5"55	
 	
 	
 	
 	
r)   c                    d S r   r   r\   s    r'   hide_cursorzWin32Output.hide_cursor      r)   c                    d S r   r   r\   s    r'   show_cursorzWin32Output.show_cursor  r   r)   cursor_shaper   c                    d S r   r   )r6   r  s     r'   set_cursor_shapezWin32Output.set_cursor_shape  r   r)   c                    d S r   r   r\   s    r'   reset_cursor_shapezWin32Output.reset_cursor_shape  r   r)   c                    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    rJ   GetConsoleWindowuser32RedrawWindowr	   )clsr   RDW_INVALIDATEs      r'   win32_refresh_windowz Win32Output.win32_refresh_window  sN     88::;;""64vn7M7MNNNNNr)   c                6    | 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.
        )rD   r   DEPTH_4_BITr\   s    r'   get_default_color_depthz#Win32Output.get_default_color_depth  s     #/++%%r)   )FN)rA   r   rB   rC   rD   rE   r#   r,   )r#   rY   )r#   r]   )r`   r]   r#   r,   )r#   r   )rv   rw   rx   ry   rz   ry   r#   r?   )r#   r   )r   r]   r#   r,   r9   )r   r   r   rY   r#   r,   )r   r   r   r   r#   r,   )r   r   )r   rY   r   rY   r#   r,   )r   rY   r#   r,   )r  r   r#   r,   )r#   r   ))r:   r;   r<   r=   r5   r[   r_   re   rg   ru   r   rQ   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  r   r)   r'   r!   r!   Z   s         $)15	A A A A A2$ $ $ $$ $ $ $" " " "   0 0 0 0(   4"- "- "- "-H> > > >; ; ; ;# # # ## # # ## # # #
 
 
 
.   $X $X $X $XL      
 
 
 
 

 
 
 
       
 
 
 
% % % %   <B B B B
 
 
 
<- - - -0. . . .
 
 
 
$

 

 

 

             O O O [O
& 
& 
& 
& 
& 
&r)   c                  2    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r:   r;   r<   BLACKBLUEGREENCYANREDMAGENTAYELLOWGRAY	INTENSITYr   r)   r'   r  r    ;        EDED
CGFDIIIr)   r  c                  2    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )BACKGROUND_COLORr   r       0   r   P   `   p      Nr  r   r)   r'   r&  r&    r$  r)   r&  	color_cls/type[FOREGROUND_COLOR] | type[BACKGROUND_COLOR]dict[str, int]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  )r-  s    r'   _create_ansi_color_dictrB  *  s:   yY_ 	IN 	9?Y-@@	
 	Y^i&99 	9= 	Y_ 	i& 	IN 	y( 	IN 	)<< 	9?Y-@@  	I,y/BB!" 	).9+>>#$ 	Y.1DD%& 	).9+>>' r)   c                  R    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
dS )rO   z8
    Inspired by pygments/formatters/terminal256.py
    r#   r,   c                F    |                                  | _        i | _        d S r   )_build_color_table_win32_colors
best_matchr\   s    r'   r5   zColorLookupTable.__init__Q  s#    !4466 79r)   $list[tuple[int, int, int, int, int]]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'   rE  z#ColorLookupTable._build_color_tableW  s   
  4rx24rw04rx24rw04rvrv.4rz2:64ry")44rw04rw5rw7MN4rx",62<8OP4rw5rw7MN4rv4bfr|6KL4rzBL8"*r|:ST4ry2<7R\9QR4rx",62<8OP4rw5rw7MN!
 	
r)   rrY   gr   tuple[int, int]c                    d}d}d}| j         D ]4\  }}}	}
}||z
  }||z
  }||	z
  }||z  ||z  z   ||z  z   }||k     r|
}|}|}5||fS )Ni r   )rF  )r6   rP  rQ  r   distancefg_matchbg_matchr_g_b_fg_bg_rdgdbdds                   r'   _closest_colorzColorLookupTable._closest_colorr  s     $($6 
	 
	 BBSRBRBRBR"r'!BG+A8||!!r)   colorr]   c                   | j                             |d           }|g	 t          t          |          d          }n# t          $ r d}Y nw xY w|dz	  dz  }|dz	  dz  }|dz  }|                     |||          }|| j         |<   |S )Nr   r   rM  r  )rG  r3   rY   r]   
ValueErrorr`  )r6   ra  indexesrgbrP  rQ  r   s          r'   _color_indexeszColorLookupTable._color_indexes  s    /%%eT22?#e**b))    d"AT!Ad
A))!Q22G%,DOE"s   = AAfg_colorc                d    |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_COLORSrf  )r6   rg  s     r'   r   z ColorLookupTable.lookup_fg_color  3     ~%%!(++&&x0033r)   bg_colorc                d    |t           v rt           |         S |                     |          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_COLORSrf  )r6   rk  s     r'   r   z ColorLookupTable.lookup_bg_color  rj  r)   Nr9   )r#   rH  )rP  rY   rQ  rY   r   rY   r#   rR  )ra  r]   r#   rR  )rg  r]   r#   rY   )rk  r]   r#   rY   )r:   r;   r<   r=   r5   staticmethodrE  r`  rf  r   r   r   r)   r'   rO   rO   L  s         9 9 9 9 
 
 
 \
4" " " "$   4 4 4 44 4 4 4 4 4r)   rO   )r"   r   r#   r   )r-  r.  r#   r/  )9
__future__r   sysplatformr1   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(   rT   rV   	Exceptionr+   r?   r!   r  r&  rB  ri  rm  setrO   r   r)   r'   <module>r     s:   " " " " " " 




|w 				 Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q ) ) ) ) ) ) ) ) , , , , , , , , , , 4 4 4 4 4 4 / / / / / / 9 9 9 9 9 9 9 9 + + + + + +              + * * * * *       # # # # # #   
8 8 8 8*   D " " " " " " " ". WT]]u& u& u& u& u&& u& u& u&p	 	 	 	 	 	 	 		 	 	 	 	 	 	 	   6 )()9::(()9::
s>cc"2333333
s>cc"2333333_4 _4 _4 _4 _4 _4 _4 _4 _4 _4r)   