a
    lcXc                     @   s  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ d dl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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. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 er6d dl7m8Z8m9Z9 ddgZ:e"Z;G dd de2Z<d%ee=e>e=edddZ?ee>eee>f dddZ@G dd de+ZAG d d! d!e2ZBG d"d de,ZCG d#d$ d$e2ZDdS )&    N)zip_longest)TYPE_CHECKINGCallableDictIterableListOptionalSequenceTupleTypeVarUnioncast)get_app)CompletionState)
Completion)Point)	ConditionFilterOrBoolhas_completionsis_done	to_filter)StyleAndTextTuplesfragment_list_widthto_formatted_text)KeyPressEvent)explode_text_fragments)
MouseEventMouseEventType)
get_cwidth   )ConditionalContainerHSplitScrollOffsetsWindow)GetLinePrefixCallable	UIContent	UIControl)	Dimension)ScrollbarMargin)KeyBindingsNotImplementedOrNoneCompletionsMenuMultiColumnCompletionsMenuc                   @   s   e Zd ZdZdZedddZeee dddZ	eeeee
 ee d	d
dZeeedddZeedddZeeedddZeeedddZeeeedddZeddddZdS )CompletionsMenuControlaJ  
    Helper for drawing the complete menu to the screen.

    :param scroll_offset: Number (integer) representing the preferred amount of
        completions to be displayed before and after the current one. When this
        is a very high number, the current completion will be shown in the
        middle most of the time.
       returnc                 C   s   dS NF selfr2   r2   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/prompt_toolkit/layout/menus.py	has_focusI   s    z CompletionsMenuControl.has_focusmax_available_widthr0   c                 C   s6   t  jj}|r.| d|}| d|}|| S dS d S )Ni  r   )r   current_buffercomplete_state_get_menu_width_get_menu_meta_width)r4   r8   r:   
menu_widthmenu_meta_widthr2   r2   r5   preferred_widthL   s    
z&CompletionsMenuControl.preferred_widthwidthmax_available_height
wrap_linesget_line_prefixr0   c                 C   s    t  jj}|rt|jS dS d S Nr   )r   r9   r:   lencompletions)r4   rA   rB   rC   rD   r:   r2   r2   r5   preferred_heightV   s    

z'CompletionsMenuControl.preferred_heightrA   heightr0   c                    s   t  jj}|r||j |j||| ||tt	d fdd}t
|tdplddt dS t
 S )z=
        Create a UIContent object for this control.
        ir0   c                    s:    |  }| k}t ||dd}r6|||7 }|S )NTspace_after)_get_menu_item_fragments_get_menu_item_meta_fragments)rL   cis_current_completionresultrG   indexr>   r=   r4   	show_metar2   r5   get_linet   s    z7CompletionsMenuControl.create_content.<locals>.get_liner   )xy)rW   Zcursor_position
line_count)r   r9   r:   rG   complete_indexr;   r<   
_show_metaintr   r%   r   rF   )r4   rA   rJ   r:   rW   r2   rT   r5   create_contentd   s     

z%CompletionsMenuControl.create_contentr:   r0   c                 C   s   t dd |jD S )zT
        Return ``True`` if we need to show a column with meta information.
        c                 s   s   | ]}|j V  qd S N)display_meta_text.0rQ   r2   r2   r5   	<genexpr>       z4CompletionsMenuControl._show_meta.<locals>.<genexpr>)anyrG   r4   r:   r2   r2   r5   r\      s    z!CompletionsMenuControl._show_meta)	max_widthr:   r0   c                 C   s&   t |t| jtdd |jD d S )z6
        Return the width of the main column.
        c                 s   s   | ]}t |jV  qd S r`   r   Zdisplay_textrb   r2   r2   r5   rd      re   z9CompletionsMenuControl._get_menu_width.<locals>.<genexpr>   )minmax	MIN_WIDTHrG   r4   rh   r:   r2   r2   r5   r;      s    z&CompletionsMenuControl._get_menu_widthc                    sD   t tddd | |r<t|t fdd|jD d S dS dS )	z6
        Return the width of the meta column.
        
completionr0   c                 S   s
   t | jS r`   r   ra   rp   r2   r2   r5   
meta_width   s    z?CompletionsMenuControl._get_menu_meta_width.<locals>.meta_widthc                 3   s   | ]} |V  qd S r`   r2   rb   rs   r2   r5   rd      re   z>CompletionsMenuControl._get_menu_meta_width.<locals>.<genexpr>rj   r   N)r   r]   r\   rk   rl   rG   rn   r2   rt   r5   r<      s    
z+CompletionsMenuControl._get_menu_meta_width)rp   rR   rA   r0   c                 C   sX   |r
d}nd}t |j|d \}}d|d |  }tttg dg | d|fg |dS )	Nz-class:completion-menu.meta.completion.currentz%class:completion-menu.meta.completionrj    r    ru   rw   style)_trim_formatted_textdisplay_metar   r   r   )r4   rp   rR   rA   	style_strtexttwpaddingr2   r2   r5   rP      s    z4CompletionsMenuControl._get_menu_item_meta_fragmentsr*   mouse_eventr0   c                 C   sd   t  j}|jtjkr*||jj d|_n6|jtj	krF|j
ddd n|jtjkr`|jddd dS )z>
        Handle mouse events: clicking and scrolling.
        N   TcountZdisable_wrap_around)r   r9   
event_typer   MOUSE_UPgo_to_completionpositionrY   r:   SCROLL_DOWNcomplete_next	SCROLL_UPcomplete_previous)r4   r   br2   r2   r5   mouse_handler   s    z$CompletionsMenuControl.mouse_handlerN)__name__
__module____qualname____doc__rm   boolr6   r]   r   r?   r$   rH   r%   r^   r   r\   r;   r<   r   r   rP   r   r   r2   r2   r2   r5   r-   :   s(   %r-   F)rp   rR   rA   rN   r0   c                 C   sv   |rd | j| j}n
d| j }t| j|r2|d n|d \}}d|d |  }tttg dg | d|fg |dS )	z_
    Get the style/text tuples for a menu item, styled and trimmed to the given
    width.
    z.class:completion-menu.completion.current {} {}z!class:completion-menu.completion rj   r   ru   rv   rw   rx   )formatry   Zselected_stylerz   displayr   r   r   )rp   rR   rA   rN   r|   r}   r~   r   r2   r2   r5   rO      s    

rO   )formatted_textrh   r0   c                 C   sx   t | }||krlg }|d }t| D ]0}t|d }||krP|| ||8 }q$ qVq$|d ||| fS | |fS dS )zo
    Trim the text to `max_width`, append dots when the text is too long.
    Returns (text, width) tuple.
    r   r   )rw   z...N)r   r   r   append)r   rh   rA   rS   Zremaining_widthZstyle_and_chZch_widthr2   r2   r5   rz      s    


rz   c                       sB   e Zd Zd	ee eeeg ef f eeedd fddZ  Z	S )
r+   Nr   TF )
max_heightscroll_offsetextra_filterdisplay_arrowsz_indexr0   c                    sb   t |}t |}t jtt tddtd|dt||dt|dgdd|d	tt	 @ |@ d
 d S )N   rk   r   )rk   rl   )topbottom)r   Tclass:completion-menu)contentrA   rJ   Zscroll_offsetsZright_marginsZdont_extend_widthry   r   r   filter)
r   super__init__r#   r-   r'   r"   r(   r   r   )r4   r   r   r   r   r   	__class__r2   r5   r     s    	


zCompletionsMenu.__init__)Nr   TFr   )
r   r   r   r   r]   r   r   r   r   __classcell__r2   r2   r   r5   r+     s        c                   @   s   e Zd ZdZdZd eeddddZddd	d
ZedddZ	ee
e dddZeeee
e e
e dddZeeedddZeedddZeddddZddddZdS )! MultiColumnCompletionMenuControla  
    Completion menu that displays all the completions in several columns.
    When there are more completions than space for them to be displayed, an
    arrow is shown on the left or right side.

    `min_rows` indicates how many rows will be available in any possible case.
    When this is larger than one, it will try to use less columns and more
    rows until this value is reached.
    Be careful passing in a too big value, if less than the given amount of
    rows are available, more columns would have been required, but
    `preferred_width` doesn't know about that and reports a too small value.
    This results in less completions displayed and additional scrolling.
    (It's a limitation of how the layout engine currently works: first the
    widths are calculated, then the heights.)

    :param suggested_max_column_width: The suggested max width of a column.
        The column can still be bigger than this, but if there is place for two
        columns of this width, we will display two columns. This to avoid that
        if there is one very wide completion, that it doesn't significantly
        reduce the amount of columns.
    r      N)min_rowssuggested_max_column_widthr0   c                 C   sL   |dksJ || _ || _d| _d| _d| _d| _i | _d| _d| _d| _	d S )Nr   r   F)
r   r   scroll_rendered_rows_rendered_columns_total_columns_render_pos_to_completion_render_left_arrow_render_right_arrow_render_width)r4   r   r   r2   r2   r5   r   I  s    z)MultiColumnCompletionMenuControl.__init__r/   c                 C   s
   d| _ d S rE   )r   r3   r2   r2   r5   resetY  s    z&MultiColumnCompletionMenuControl.resetc                 C   s   dS r1   r2   r3   r2   r2   r5   r6   \  s    z*MultiColumnCompletionMenuControl.has_focusr7   c                 C   sl   t  jj}|du rdS | |}t|tt|jt	| j
  }||krb||| j krb||8 }qB|| j S )z{
        Preferred width: prefer to use at least min_rows, but otherwise as much
        as possible horizontally.
        Nr   )r   r9   r:   _get_column_widthr]   mathceilrF   rG   floatr   _required_margin)r4   r8   r:   column_widthrS   r2   r2   r5   r?   _  s    

	
z0MultiColumnCompletionMenuControl.preferred_widthr@   c                 C   sP   t  jj}|du rdS | |}td|| j | }ttt	|j
t| S )z^
        Preferred height: as much as needed in order to display all the completions.
        Nr   r   )r   r9   r:   r   rl   r   r]   r   r   rF   rG   r   )r4   rA   rB   rC   rD   r:   r   column_countr2   r2   r5   rH   x  s    


z1MultiColumnCompletionMenuControl.preferred_heightrI   c              	      s  t  jjdu rt S | }i | _td}dtt| t	| tt
t	|   ddd}ttd fdd}d	}t|| |}|| jkr||| j  }td
|| j | }t||j}	tt|	 }
jpd| }t|t| j|| d
 | _| jdk}| jt|
d | k }g t|
D ](\}}g }|t|
d k}|r^|d|rTdndf n|rn|d t|| jd d| D ]f\}  dur|t | |dd7 }t|D ]} | j|| | |f< qn|dd| f q|s|r|d |r&|d|rdndf n|r6|d t|dd q || _|| _t|	| _|| _|| _ || | | d
 | _!tt"dfdd}t|t|
dS )z:
        Create a UIContent object for this menu.
        N_T)niterable	fillvaluer0   c                 S   s   t |g|  }t|d|iS )z*grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxxr   )iterr   )r   r   r   argsr2   r2   r5   grouper  s    z@MultiColumnCompletionMenuControl.create_content.<locals>.grouperro   c                    s   duoj duo jkS )z@Returns True when this completion is the currently selected one.N)r[   current_completionrr   )rQ   r:   r2   r5   rR     s
    zNMultiColumnCompletionMenuControl.create_content.<locals>.is_current_completionr   r   r   rj   zclass:scrollbar<ru   rv   FrM   class:completion)r   ru   >r   rx   rK   c                    s    |  S r`   r2   rL   )fragments_for_liner2   r5   rW     s    zAMultiColumnCompletionMenuControl.create_content.<locals>.get_linerW   rZ   )N)#r   r9   r:   r%   r   r   r   r]   r   r   r	   r   r   rk   r   rl   r   listrG   zipr[   r   rF   	enumerater   rO   ranger   r   r   r   r   r   r   r   )r4   rA   rJ   r   r   r   rR   ZHORIZONTAL_MARGIN_REQUIREDZvisible_columnsZcolumns_Zrows_Zselected_columnZrender_left_arrowZrender_right_arrowZ	row_indexrow	fragmentsZ
middle_rowcolumn_indexrX   rW   r2   )rQ   r:   r   r5   r^     s|    

 



"




z/MultiColumnCompletionMenuControl.create_contentr_   c                 C   s   t dd |jD d S )z2
        Return the width of each column.
        c                 s   s   | ]}t |jV  qd S r`   ri   rb   r2   r2   r5   rd     re   zEMultiColumnCompletionMenuControl._get_column_width.<locals>.<genexpr>r   )rl   rG   rg   r2   r2   r5   r      s    z2MultiColumnCompletionMenuControl._get_column_widthr*   r   c                    s   t  j dd fdd}dd fdd}|jtjkrD|  n|jtjkrX|  nl|jtjkr|jj}|jj	}|dkrj
r|  n:|jd krjr|  nj||f}|rĈ | dS )	z1
        Handle scroll and click events.
        Nr/   c                      s&    j jdd tdjd _d S )NTr   r   r   )r   r   rl   r   r2   r   r4   r2   r5   scroll_left  s    zCMultiColumnCompletionMenuControl.mouse_handler.<locals>.scroll_leftc                      s.    j jdd tjj jd _d S )NTr   r   )r   r   rk   r   r   r   r2   r   r2   r5   scroll_right  s    zDMultiColumnCompletionMenuControl.mouse_handler.<locals>.scroll_rightr   r   )r   r9   r   r   r   r   r   r   rX   rY   r   r   r   r   getZapply_completion)r4   r   r   r   rX   rY   rp   r2   r   r5   r     s(    
z.MultiColumnCompletionMenuControl.mouse_handlerr)   c                    s   ddl m} | }ttdfdd}dtddfd	d
 |jdd|dtdd fdd}|jdd|dtdd fdd}|S )zo
        Expose key bindings that handle the left/right arrow keys when the menu
        is displayed.
        r   )r)   r/   c                     s>   t  } | jj}|du s |jdu r$dS t fdd| jjD S )z1Only handle key bindings if this menu is visible.NFc                 3   s   | ]}|j  kV  qd S r`   r   )rc   Zwindowr3   r2   r5   rd   F  re   zTMultiColumnCompletionMenuControl.get_key_bindings.<locals>.filter.<locals>.<genexpr>)r   r9   r:   r[   rf   ZlayoutZvisible_windows)appr:   r3   r2   r5   r   ;  s
    zAMultiColumnCompletionMenuControl.get_key_bindings.<locals>.filterFN)rightr0   c                    sl   t  j}|j}|d urh|jd urh|j}| r6| j7 }n
| j8 }d|  krZt|jk rhn n
|| d S rE   )r   r9   r:   r[   r   rF   rG   r   )r   Zbuffr:   Z	new_indexr3   r2   r5   moveH  s    
z?MultiColumnCompletionMenuControl.get_key_bindings.<locals>.moveleftT)	is_globalr   )eventr0   c                    s
      d S r`   r2   r   r   r2   r5   _leftZ  s    z@MultiColumnCompletionMenuControl.get_key_bindings.<locals>._leftr   c                    s    d d S )NTr2   r   r   r2   r5   _right^  s    zAMultiColumnCompletionMenuControl.get_key_bindings.<locals>._right)F)'prompt_toolkit.key_binding.key_bindingsr)   r   r   addE)r4   r)   kbr   r   r   r2   )r   r4   r5   get_key_bindings2  s    z1MultiColumnCompletionMenuControl.get_key_bindings)r   r   )r   r   r   r   r   r]   r   r   r   r6   r   r?   r$   rH   r%   r^   r   r   r   r   r   r2   r2   r2   r5   r   0  s    u,r   c                       s2   e Zd ZdZd
eeeeedd fdd	Z  ZS )r,   z
    Container that displays the completions in several columns.
    When `show_meta` (a :class:`~prompt_toolkit.filters.Filter`) evaluates
    to True, it shows the meta information at the bottom.
    r   r   Tr   N)r   r   rV   r   r   r0   c           
         s   t |}t |}tt @ |@ }ttddd}ttt||dtddtddd|d	}ttt	 d
||@ |@ d	}	t
 j||	g|d d S )Nr/   c                  S   s&   t  jj} | d uo$tdd | jD S )Nc                 s   s   | ]}|j V  qd S r`   )r{   rb   r2   r2   r5   rd     s   zWMultiColumnCompletionsMenu.__init__.<locals>.any_completion_has_meta.<locals>.<genexpr>)r   r9   r:   rf   rG   )r:   r2   r2   r5   any_completion_has_meta|  s    
zDMultiColumnCompletionsMenu.__init__.<locals>.any_completion_has_meta)r   r   r   r   r   )r   rA   rJ   r   r   )r   )r   r   r   r   r   r    r#   r   r'   _SelectedCompletionMetaControlr   r   )
r4   r   r   rV   r   r   Zfull_filterr   Zcompletions_windowZmeta_windowr   r2   r5   r   l  s*    	

z#MultiColumnCompletionsMenu.__init__)r   r   TTr   )r   r   r   r   r]   r   r   r   r2   r2   r   r5   r,   e  s        c                   @   sb   e Zd ZdZeee dddZeeeee ee dddZ	eee
dd	d
ZedddZdS )r   zM
    Control that shows the meta information of the selected completion.
    r7   c                 C   s6   t  }|jjr.|jj}dtdd |jD  S dS dS )a?  
        Report the width of the longest meta text as the preferred width of this control.

        It could be that we use less width, but this way, we're sure that the
        layout doesn't change when we select another completion (E.g. that
        completions are suddenly shown in more or fewer columns.)
        rj   c                 s   s   | ]}t |jV  qd S r`   rq   rb   r2   r2   r5   rd     re   zA_SelectedCompletionMetaControl.preferred_width.<locals>.<genexpr>r   N)r   r9   r:   rl   rG   )r4   r8   r   stater2   r2   r5   r?     s
    z._SelectedCompletionMetaControl.preferred_widthr@   c                 C   s   dS )Nr   r2   )r4   rA   rB   rC   rD   r2   r2   r5   rH     s    z/_SelectedCompletionMetaControl.preferred_heightrI   c                    s0   |    ttd fdd}t| r(dnddS )NrK   c                    s    S r`   r2   r   r   r2   r5   rW     s    z?_SelectedCompletionMetaControl.create_content.<locals>.get_liner   r   r   )_get_text_fragmentsr]   r   r%   )r4   rA   rJ   rW   r2   r   r5   r^     s    z-_SelectedCompletionMetaControl.create_contentr/   c                 C   sF   d}t  jj}|rB|jrB|jjrBtttdg|jj dg |dS g S )Nz'class:completion-menu.multi-column-metarv   rx   )	r   r9   r:   r   ra   r   r   r   r{   )r4   ry   r   r2   r2   r5   r     s"    

z2_SelectedCompletionMetaControl._get_text_fragmentsN)r   r   r   r   r]   r   r?   r   r$   rH   r%   r^   r   r   r2   r2   r2   r5   r     s   	r   )F)Er   	itertoolsr   typingr   r   r   r   r   r   r	   r
   r   r   r   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.bufferr   Zprompt_toolkit.completionr   Zprompt_toolkit.data_structuresr   Zprompt_toolkit.filtersr   r   r   r   r   Zprompt_toolkit.formatted_textr   r   r   Z(prompt_toolkit.key_binding.key_processorr   Zprompt_toolkit.layout.utilsr   Zprompt_toolkit.mouse_eventsr   r   Zprompt_toolkit.utilsr   
containersr    r!   r"   r#   Zcontrolsr$   r%   r&   	dimensionr'   Zmarginsr(   r   r)   r*   __all__r   r-   r   r]   rO   rz   r+   r   r,   r   r2   r2   r2   r5   <module>   sR   4  
!  79