a
    lcY                     @   s  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Z.e&edddZ/dZ0dZ1G dd de2Z3edZ4G dd de+Z5G dd dZ6G dd dZ7eee6 ee7 f ee8e9f dd d!Z:e:e6Z;e:e7Z<e=e;e=e ksJ e=e<e=e ksJ G d"d# d#Z>dS )$    N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)coordreturnc                 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*   W/var/www/html/django/DPS/env/lib/python3.9/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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.
    Nr'   c                    s:   dt jddv }|r&dt jd  }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__r*   r+   r6   E   s    z#NoConsoleScreenBufferError.__init__)__name__
__module____qualname____doc__r6   __classcell__r*   r*   r9   r+   r-   ?   s   r-   _Tc                   @   sD  e Zd ZdZdXeeee ddddZe	ddd	Z
edd
dZeddddZeddddZedddZedef eeedddZedddZeddddZddddZddddZddd d!Zddd"d#Zee	dd$d%d&Zddd'd(Zeedd)d*d+Z ddd,d-Z!ddd.d/Z"dYe	e	dd1d2d3Z#e	dd4d5d6Z$e	dd4d7d8Z%e	dd4d9d:Z&e	dd4d;d<Z'ddd=d>Z(e	dd?d@Z)dddAdBZ*dddCdDZ+dddEdFZ,dddGdHZ-dddIdJZ.dddKdLZ/dddMdNZ0e1ddOdPdQZ2dddRdSZ3e4dddTdUZ5eddVdWZ6dS )Zr%   zW
    I/O abstraction for rendering to Windows consoles.
    (cmd.exe and similar.)
    FN)stdoutuse_complete_widthdefault_color_depthr'   c                 C   sj   || _ || _g | _|| _ttjt| _	d| _
d| _t | _|  }|rP|jnd| _trfttd| _d S )NF   ab)rB   rC   _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)r7   rA   rB   rC   infor*   r*   r+   r6   _   s    zWin32Output.__init__r.   c                 C   s
   | j  S )zReturn file descriptor.)rA   filenor7   r*   r*   r+   rV   x   s    zWin32Output.filenoc                 C   s   | j jS )z Return encoding used for stdout.)rA   encodingrW   r*   r*   r+   rX   |   s    zWin32Output.encoding)datar'   c                 C   s"   | j rdt| }| j| d S )N )rK   r   rF   appendr7   rY   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_rawc                 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)rN   rB   dwSizer)   srWindowRightLeftBottomTopminr   )r7   rU   widthheightZmaxwidthr*   r*   r+   get_size   s    

zWin32Output.get_size.)funcakwr'   c                 O   s   |    tr| jd|j dd  | jdddd |D d d  | jdddd |D d d  | j   z||i |W S  ty } z8tr| jd	|jd
|d
| d   W Y d}~n
d}~0 0 dS )z2
        Flush and call win API function.
        %rutf-8   
s        z, c                 S   s   g | ]}d | qS rn   r*   .0ir*   r*   r+   
<listcomp>       z'Win32Output._winapi.<locals>.<listcomp>c                 S   s   g | ]}d t | qS rq   )typerr   r*   r*   r+   ru      rv   z    Error in rZ   
N)flushrQ   rT   r]   r;   encodejoinr   )r7   rk   rl   rm   er*   r*   r+   _winapi   s(     
&zWin32Output._winapic                 C   s2   |    t }tj| jt|}|r*|S tdS )z,
        Return Screen buffer info.
        N)ry   r   r   rG   ZGetConsoleScreenBufferInforI   r   r-   )r7   sbinfosuccessr*   r*   r+   rN      s    
z(Win32Output.get_win32_screen_buffer_info)titler'   c                 C   s   |  tjj| dS )z%
        Set terminal title.
        Nr}   r   rG   ZSetConsoleTitleW)r7   r   r*   r*   r+   	set_title   s    zWin32Output.set_titlec                 C   s   |  tjjd d S )Nr1   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   rN   ra   r)   r(   cursor_goto_erase)r7   startr~   lengthr*   r*   r+   erase_screen   s
    
zWin32Output.erase_screenc                 C   sD   |   }|j}|j}|j|j |j|j|jj   }| || d S N)rN   ra   dwCursorPositionr)   r(   r   )r7   r~   sizer   r   r*   r*   r+   
erase_down   s
     zWin32Output.erase_downc                 C   s.   |   }|j}|jj|jj }| || dS )r1   N)rN   r   ra   r)   r   )r7   r~   r   r   r*   r*   r+   erase_end_of_line   s    zWin32Output.erase_end_of_line)r   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   rG   ZFillConsoleOutputCharacterArI   r   r   r,   r   rN   ZFillConsoleOutputAttributerO   )r7   r   r   Z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   rG   SetConsoleTextAttributerI   rP   rK   rW   r*   r*   r+   reset_attributes  s    zWin32Output.reset_attributes)attrscolor_depthr'   c              	   C   s   |\	}}}}}}}	}
}t || _| j}|tjkrh|rL|d@ }|| j|O }|rh|d@ }|| j|O }|
r|d@ |d@ d> B |d@ d? B }| t	j
j| j| d S )Niii rD         )boolrK   rP   r$   ZDEPTH_1_BITrM   lookup_fg_colorlookup_bg_colorr}   r   rG   r   rI   )r7   r   r   Zfgcolorbgcolorbold	underlinestrikeitalicblinkreverseZhiddenZ	win_attrsr*   r*   r+   set_attributes  s8    



zWin32Output.set_attributesc                 C   s   d S r   r*   rW   r*   r*   r+   disable_autowrapB  s    zWin32Output.disable_autowrapc                 C   s   d S r   r*   rW   r*   r*   r+   enable_autowrapF  s    zWin32Output.enable_autowrapr   )r   r   r'   c                 C   s(   t ||d}| tjj| jt| d S Nr)   r(   )r   r}   r   rG   SetConsoleCursorPositionrI   r,   )r7   r   r   posr*   r*   r+   r   J  s    zWin32Output.cursor_goto)amountr'   c                 C   s:   |   j}t|j|j| d}| tjj| j	t
| d S r   )rN   r   r   r)   r(   r}   r   rG   r   rI   r,   r7   r   srr   r*   r*   r+   	cursor_upP  s
    
zWin32Output.cursor_upc                 C   s   |  |  d S r   )r   r7   r   r*   r*   r+   cursor_downW  s    zWin32Output.cursor_downc                 C   s@   |   j}ttd|j| |jd}| tjj	| j
t| d S )Nr   r   )rN   r   r   maxr)   r(   r}   r   rG   r   rI   r,   r   r*   r*   r+   cursor_forwardZ  s
    
zWin32Output.cursor_forwardc                 C   s   |  |  d S r   )r   r   r*   r*   r+   cursor_backwardc  s    zWin32Output.cursor_backwardc                 C   s   | j s| j  dS d| j }trH| jd| dd  | j  |D ]0}t }t	j
| j|dt|d}|dksLJ qLg | _ dS )z3
        Write to output stream and flush.
        Nr1   rn   ro   rp   r"   r   )rF   rA   ry   r{   rQ   rT   r]   rz   r   r   rG   ZWriteConsoleWrI   r   )r7   rY   bZwrittenretvalr*   r*   r+   ry   f  s    

zWin32Output.flushc                 C   s   |   }|jj|jj d S )Nr"   )rN   rb   re   r   r(   )r7   rU   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 rZ|d k rhn 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)rN   rb   r   r   rd   rc   re   rf   r(   r   r}   r   rG   ZSetConsoleWindowInforI   r   )r7   rU   r   Z
cursor_posresultZ
win_heightr*   r*   r+   scroll_buffer_to_prompt  s    $
z#Win32Output.scroll_buffer_to_promptc              	   C   sV   | j sRd}d}t| tjj||B tddtdd}| tjj| || _d| _ dS )z0
        Go to alternate screen buffer.
        l        i   @r   Nr"   T)	rJ   r   r}   r   rG   ZCreateConsoleScreenBufferr   SetConsoleActiveScreenBufferrI   )r7   ZGENERIC_READZGENERIC_WRITEhandler*   r*   r+   enter_alternate_screen  s     z"Win32Output.enter_alternate_screenc                 C   sL   | j rHt| tjjt}| tjj| | tjj| j	 || _	d| _ dS )z6
        Make stdout again the active buffer.
        FN)
rJ   r   r}   r   rG   rH   r!   r   ZCloseHandlerI   )r7   rA   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   rG   rH   r    r   r}   ZGetConsoleModer	   ZSetConsoleModevalue)r7   ENABLE_MOUSE_INPUTZ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   )r7   r   r   r   r*   r*   r+   disable_mouse_support  s    
z!Win32Output.disable_mouse_supportc                 C   s   d S r   r*   rW   r*   r*   r+   hide_cursor  s    zWin32Output.hide_cursorc                 C   s   d S r   r*   rW   r*   r*   r+   show_cursor  s    zWin32Output.show_cursor)cursor_shaper'   c                 C   s   d S r   r*   )r7   r   r*   r*   r+   set_cursor_shape  s    zWin32Output.set_cursor_shapec                 C   s   d S r   r*   rW   r*   r*   r+   reset_cursor_shape  s    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   rG   ZGetConsoleWindowZuser32ZRedrawWindowr   )clsr   Z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)rC   r$   ZDEPTH_4_BITrW   r*   r*   r+   get_default_color_depth  s    
z#Win32Output.get_default_color_depth)FN)r   r   )7r;   r<   r=   r>   r   r   r   r$   r6   intrV   strrX   r]   r^   r   rj   r   r@   objectr}   r   rN   r   r   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   r   classmethodr   r   r*   r*   r*   r+   r%   Y   sZ     $	&	c                   @   s0   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     s   r   c                   @   s0   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   "  s   r   )	color_clsr'   c                 C   sz   | j | j | j| j | jB | j| jB | j| j| j| j| j| j| j| jB | j| jB | j| jB | j| jB | j| jB | j| jB dS )zHCreate a table that maps the 16 named ansi colors to their Windows code.)Z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.  s$    







r   c                   @   s   e Zd ZdZddddZeeeeeeeef  dddZ	eeeeeef dd	d
Z
eeeef dddZeedddZeedddZdS )rL   z8
    Inspired by pygments/formatters/terminal256.py
    Nr.   c                 C   s   |   | _i | _d S r   )_build_color_table_win32_colors
best_matchrW   r*   r*   r+   r6   U  s    
zColorLookupTable.__init__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   )ZFGZBGr*   r*   r+   r   [  s&    z#ColorLookupTable._build_color_table)rgr   r'   c                 C   sn   d}d}d}| j D ]R\}}}	}
}|| }|| }||	 }|| ||  ||  }||k r|
}|}|}q||fS )Ni r   )r   )r7   r   r   r   ZdistanceZfg_matchZbg_matchZr_Zg_Zb_Zfg_Zbg_rdgdZbddr*   r*   r+   _closest_colorv  s    zColorLookupTable._closest_color)colorr'   c                 C   s|   | j |d }|d u rxztt|d}W n ty>   d}Y n0 |d? d@ }|d? d@ }|d@ }| |||}|| j |< |S )Nr   r   r   r   )r   r4   r   r   
ValueErrorr  )r7   r  Zindexesrgbr   r   r   r*   r*   r+   _color_indexes  s    

zColorLookupTable._color_indexes)fg_colorr'   c                 C   s"   |t v rt | S | |d 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   N)FG_ANSI_COLORSr  )r7   r  r*   r*   r+   r     s    z ColorLookupTable.lookup_fg_color)bg_colorr'   c                 C   s"   |t v rt | S | |d 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"   N)BG_ANSI_COLORSr  )r7   r
  r*   r*   r+   r     s    z ColorLookupTable.lookup_bg_color)r;   r<   r=   r>   r6   staticmethodr   r   r   r   r  r   r  r   r   r*   r*   r*   r+   rL   P  s   "rL   )?sysplatformr2   ctypesr   r   r   r   r   r   r	   utilsr   r   Zctypes.wintypesr   r   typingr   r   r   r   r   r   r   r   r   Zprompt_toolkit.cursor_shapesr   Zprompt_toolkit.data_structuresr   Zprompt_toolkit.stylesr   r   Zprompt_toolkit.utilsr   Zprompt_toolkit.win32_typesr   r   r   r    r!   baser#   r   r$   __all__r,   rQ   rS   	Exceptionr-   r@   r%   r   r   r   r   r   r	  r  setrL   r*   r*   r*   r+   <module>   sH   $,   @
