
    Mh^X                    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    [/var/www/html/test/jupyter/venv/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                   }nd}t	                                          |           d S )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'   r6   z#NoConsoleScreenBufferError.__init__F   sn    2:>>&"555 		K/ 06vbj6H/I/I G KG!!!!!r)   r#   r,   )__name__
__module____qualname____doc__r6   __classcell__)r9   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)rC   rE   _bufferrB   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)r7   rB   rC   rE   infos        r'   r6   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.)rB   filenor7   s    r'   r\   zWin32Output.filenoy   s    {!!###r)   strc                    | j         j        S )z Return encoding used for stdout.)rB   encodingr]   s    r'   r`   zWin32Output.encoding}   s    {##r)   datac                l    | j         rdt          |          z  }| j                            |           d S )N )rO   r   rJ   appendr7   ra   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)rf   re   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)rR   rC   dwSizer&   srWindowRightLeftBottomTopminr   )r7   rY   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                            |j                                        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.
           
s        z, c                    g | ]}|S  r   .0is     r'   
<listcomp>z'Win32Output._winapi.<locals>.<listcomp>   s    %:%:%:1h%:%:%:r)   zutf-8c                .    g | ]}t          |          S r   )typer   s     r'   r   z'Win32Output._winapi.<locals>.<listcomp>   s     777Q^777r)   z    Error in rc   
N)flushrU   rX   rf   r;   encodejoinr   )r7   rw   ry   r{   es        r'   _winapizWin32Output._winapi   s|    	

 
	HNNt}088::UBCCC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    rK   GetConsoleScreenBufferInforM   r   r+   )r7   sbinfosuccesss      r'   rR   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    rK   SetConsoleTitleW)r7   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   rR   rl   r&   r%   cursor_goto_erase)r7   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)rR   rl   dwCursorPositionr&   r%   r   )r7   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)rR   r   rl   r&   r   )r7   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    rK   FillConsoleOutputCharacterArM   r   r   r(   r   rR   FillConsoleOutputAttributerS   )r7   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    rK   SetConsoleTextAttributerM   rT   rO   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 rH         )rD   rO   rT   r   DEPTH_1_BITrQ   lookup_fg_colorlookup_bg_colorr   r    rK   r   rM   )r7   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    rK   SetConsoleCursorPositionrM   r(   )r7   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   )rR   r   r   r&   r%   r   r    rK   r   rM   r(   r7   r   srr   s       r'   	cursor_upzWin32Output.cursor_upQ  sg    ..00AbdbdVm,,,O4dm\RUEVEV	
 	
 	
 	
 	
r)   c                2    |                      |            d S r   )r   r7   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   )rR   r   r   maxr&   r%   r   r    rK   r   rM   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          rI| j                            |                                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   )rJ   rB   r   r   rU   rX   rf   r   r   r    rK   WriteConsoleWrM   r   )r7   ra   bwrittenretvals        r'   r   zWin32Output.flushg  s     | 	 KFwwt|$$ 	HNNtK//11E9:::HNN
  	 	AggG_22q!U7^^T F Q;;;;;r)   c                b    |                                  }|j        j        |j        j        z
  dz   S )Nr   )rR   rm   rp   r   r%   )r7   rY   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)rR   rm   r   r   ro   rn   rp   rq   r%   r   r   r    rK   SetConsoleWindowInforM   r   )r7   rY   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)	rN   r   r   r    rK   CreateConsoleScreenBufferr   SetConsoleActiveScreenBufferrM   )r7   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)
rN   r   r   r    rK   rL   r   r   CloseHandlerM   )r7   rB   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    rK   rL   r   r   r   GetConsoleModer   SetConsoleModevalue)r7   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   )r7   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   )r7   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    rK   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.
        )rE   r   DEPTH_4_BITr]   s    r'   get_default_color_depthz#Win32Output.get_default_color_depth  s     #/++%%r)   )FN)rB   r   rC   rD   rE   rF   r#   r,   )r#   rZ   )r#   r^   )ra   r^   r#   r,   )r#   r   )rw   rx   ry   rz   r{   rz   r#   r@   )r#   r   )r   r^   r#   r,   r:   )r   r   r   rZ   r#   r,   )r   r   r   r   r#   r,   )r   r   )r   rZ   r   rZ   r#   r,   )r   rZ   r#   r,   )r  r   r#   r,   )r#   r   ))r;   r<   r=   r>   r6   r\   r`   rf   rh   rv   r   rR   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_dictr@  *  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 )rP   z8
    Inspired by pygments/formatters/terminal256.py
    r#   r,   c                F    |                                  | _        i | _        d S r   )_build_color_table_win32_colors
best_matchr]   s    r'   r6   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'   rC  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)   rrZ   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   )rD  )r7   rN  rO  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   rK  r  )rE  r3   rZ   r^   
ValueErrorr^  )r7   r_  indexesrgbrN  rO  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_COLORSrd  )r7   re  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_COLORSrd  )r7   ri  s     r'   r   z ColorLookupTable.lookup_bg_color  rh  r)   Nr:   )r#   rF  )rN  rZ   rO  rZ   r   rZ   r#   rP  )r_  r^   r#   rP  )re  r^   r#   rZ   )ri  r^   r#   rZ   )r;   r<   r=   r>   r6   staticmethodrC  r^  rd  r   r   r   r)   r'   rP   rP   L  s         9 9 9 9 
 
 
 \
4" " " "$   4 4 4 44 4 4 4 4 4r)   rP   )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(   rU   rW   	Exceptionr+   r@   r!   r  r$  r@  rg  rk  setrP   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)   