
     hX                    n   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 esd dlmZ d dlmZmZ d d	lmZmZmZmZ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"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gZ0d(dZ1dZ2dZ3 G d de4          Z5 ed          Z6 G d de-          Z7 G d d          Z8 G d  d!          Z9d)d%Z: e:e8          Z; e:e9          Z< e=e;           e=e"          k    sJ  e=e<           e=e"          k    sJ  G d& d'          Z>dS )*    )annotationsNwin32)ArgumentErrorbyrefc_charc_longc_uintc_ulongpointer   )SPHINX_AUTODOC_RUNNING)windll)DWORDHANDLE)	CallableDictListOptionalTextIOTupleTypeTypeVarUnion)CursorShape)Size)ANSI_COLOR_NAMESAttrs)
get_cwidth)CONSOLE_SCREEN_BUFFER_INFOCOORD
SMALL_RECTSTD_INPUT_HANDLESTD_OUTPUT_HANDLE   )Output)
ColorDepth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    W/var/www/html/Sam_Eipo/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         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__)selfr4   message	__class__s      r-   r;   z#NoConsoleScreenBufferError.__init__G   sj    2:>>&"555 		K/ 24F1C	D G KG!!!!!r/   r)   r2   )__name__
__module____qualname____doc__r;   __classcell__)r>   s   @r-   r1   r1   A   sG         
" " " " " " " " " "r/   r1   _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)   r2   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)rH   rJ   _bufferrG   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)r<   rG   rH   rJ   infos        r-   r;   zWin32Output.__init__a   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.)rG   filenor<   s    r-   ra   zWin32Output.filenoz   s    {!!###r/   strc                    | j         j        S )z Return encoding used for stdout.)rG   encodingrb   s    r-   re   zWin32Output.encoding~   s    {##r/   datac                l    | j         rdt          |          z  }| j                            |           d S )N )rT   r   rO   appendr<   rf   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)rk   rj   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)rW   rH   dwSizer,   srWindowRightLeftBottomTopminr   )r<   r^   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kwrE   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 rh   
N)flushrZ   r]   rk   r@   encodejoinr   )r<   r|   r~   r   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   rP   GetConsoleScreenBufferInforR   r   r1   )r<   sbinfosuccesss      r-   rW   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   rP   SetConsoleTitleW)r<   r   s     r-   	set_titlezWin32Output.set_title   s#     	V_5u=====r/   c                P    |                      t          j        j        d           d S )Nr6   r   rb   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    rW   rq   r,   r+   cursor_goto_erase)r<   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)rW   rq   dwCursorPositionr,   r+   r   )r<   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 )r6   N)rW   r   rq   r,   r   )r<   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   rP   FillConsoleOutputCharacterArR   r   r   r.   r   rW   FillConsoleOutputAttributerX   )r<   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   rP   SetConsoleTextAttributerR   rY   rT   rb   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 rM         )rI   rT   rY   r&   DEPTH_1_BITrV   lookup_fg_colorlookup_bg_colorr   r   rP   r   rR   )r<   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   rb   s    r-   disable_autowrapzWin32Output.disable_autowrapD      r/   c                    d S r   r   rb   s    r-   enable_autowrapzWin32Output.enable_autowrapH  r   r/   r   r   r   c                    t          ||          }|                     t          j        j        | j        t          |                     d S Nr,   r+   )r    r   r   rP   SetConsoleCursorPositionrR   r.   )r<   r   r   poss       r-   r   zWin32Output.cursor_gotoL  sK    f$$$O4dm\RUEVEV	
 	
 	
 	
 	
r/   amountc                    |                                  j        }t          |j        |j        |z
            }|                     t          j        j        | j	        t          |                     d S r   )rW   r   r    r,   r+   r   r   rP   r   rR   r.   r<   r   srr   s       r-   	cursor_upzWin32Output.cursor_upR  sg    ..00AbdbdVm,,,O4dm\RUEVEV	
 	
 	
 	
 	
r/   c                2    |                      |            d S r   )r   r<   r   s     r-   cursor_downzWin32Output.cursor_downY  s    wr/   c                    |                                  j        }t          t          d|j        |z             |j                  }|                     t          j        j	        | j
        t          |                     d S )Nr   r   )rW   r   r    maxr,   r+   r   r   rP   r   rR   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_backwarde  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.
        Nr6   r   r   r   r$   r   )rO   rG   r   r   rZ   r]   rk   r   r   r   rP   WriteConsoleWrR   r   )r<   rf   bwrittenretvals        r-   r   zWin32Output.flushh  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$   )rW   rr   ru   r   r+   )r<   r^   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)rW   rr   r   r!   rt   rs   ru   rv   r+   r   r   r   rP   SetConsoleWindowInforR   r   )r<   r^   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)	rS   r   r   r   rP   CreateConsoleScreenBufferr   SetConsoleActiveScreenBufferrR   )r<   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)
rS   r   r   r   rP   rQ   r#   r   CloseHandlerR   )r<   rG   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   rP   rQ   r"   r   r   GetConsoleModer   SetConsoleModevalue)r<   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   )r<   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   rb   s    r-   hide_cursorzWin32Output.hide_cursor      r/   c                    d S r   r   rb   s    r-   show_cursorzWin32Output.show_cursor  r  r/   cursor_shaper   c                    d S r   r   )r<   r	  s     r-   set_cursor_shapezWin32Output.set_cursor_shape  r  r/   c                    d S r   r   rb   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   rP   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.
        )rJ   r&   DEPTH_4_BITrb   s    r-   get_default_color_depthz#Win32Output.get_default_color_depth  s     #/++%%r/   )FN)rG   r   rH   rI   rJ   rK   r)   r2   )r)   r_   )r)   rc   )rf   rc   r)   r2   )r)   r   )r|   r}   r~   r   r   r   r)   rE   )r)   r   )r   rc   r)   r2   r?   )r   r    r   r_   r)   r2   )r   r   r   r&   r)   r2   )r   r   )r   r_   r   r_   r)   r2   )r   r_   r)   r2   )r	  r   r)   r2   )r)   r&   ))r@   rA   rB   rC   r;   ra   re   rk   rm   r{   r   rW   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'   [   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@   rA   rB   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$  )r3  s    r-   _create_ansi_color_dictrH  +  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 )rU   z8
    Inspired by pygments/formatters/terminal256.py
    r)   r2   c                F    |                                  | _        i | _        d S r   )_build_color_table_win32_colors
best_matchrb   s    r-   r;   zColorLookupTable.__init__R  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-   rK  z#ColorLookupTable._build_color_tableX  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/   rr_   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   )rL  )r<   rV  rW  r   distancefg_matchbg_matchr_g_b_fg_bg_rdgdbdds                   r-   _closest_colorzColorLookupTable._closest_colors  s     $($6 
	 
	 BBSRBRBRBR"r'!BG+A8||!!r/   colorrc   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   rS  r  )rM  r9   r_   rc   
ValueErrorrf  )r<   rg  indexesrgbrV  rW  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_COLORSrl  )r<   rm  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_COLORSrl  )r<   rq  s     r-   r   z ColorLookupTable.lookup_bg_color  rp  r/   Nr?   )r)   rN  )rV  r_   rW  r_   r   r_   r)   rX  )rg  rc   r)   rX  )rm  rc   r)   r_   )rq  rc   r)   r_   )r@   rA   rB   rC   r;   staticmethodrK  rf  rl  r   r   r   r/   r-   rU   rU   M  s         9 9 9 9 
 
 
 \
4" " " "$   4 4 4 44 4 4 4 4 4r/   rU   )r(   r    r)   r   )r3  r4  r)   r5  )?
__future__r   sysplatformr7   ctypesr   r   r   r   r	   r
   r   utilsr   r   ctypes.wintypesr   r   typingr   r   r   r   r   r   r   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#   baser%   r   r&   __all__r.   rZ   r\   	Exceptionr1   rE   r'   r  r,  rH  ro  rs  setrU   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 * * * * * *   ) ) ) ) ) ) ) ) V V V V V V V V V V V V V V V V V V V V V V 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/   