o
    �õ±j’/  ã                   @  s–  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	 d dl
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mZmZmZ d dl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&m'Z'm(Z( d dl)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 g d¢Z7e Z8G dd„ de(ƒZ9G dd„ dƒZ:G dd„ dƒZ;G dd„ dƒZ<G dd„ de.ƒZ=G dd„ dƒZ>G dd „ d ƒZ?d!S )"é    )Úannotations)ÚAny)Úget_app)ÚBuffer)ÚSYSTEM_BUFFER)
Ú	ConditionÚFilterOrBoolÚ
emacs_modeÚhas_argÚhas_completionsÚ	has_focusÚhas_validation_errorÚ	to_filterÚvi_modeÚvi_navigation_mode)ÚAnyFormattedTextÚStyleAndTextTuplesÚfragment_list_lenÚto_formatted_text)ÚConditionalKeyBindingsÚKeyBindingsÚKeyBindingsBaseÚmerge_key_bindings)ÚKeyPressEvent)Ú	InputMode)ÚKeys)ÚConditionalContainerÚ	ContainerÚWindow)ÚBufferControlÚFormattedTextControlÚSearchBufferControlÚ	UIContentÚ	UIControl)Ú	Dimension)ÚBeforeInput)ÚSimpleLexer)ÚSearchDirection)Ú
ArgToolbarÚCompletionsToolbarÚFormattedTextToolbarÚSearchToolbarÚSystemToolbarÚValidationToolbarc                      s    e Zd Zdd‡ fd
d„Z‡  ZS )r*   Ú Útextr   ÚstyleÚstrÚkwr   ÚreturnÚNonec                   s*   t ƒ jt|fi |¤Ž|dtdd�d� d S )NTé   )Úmin)r0   Údont_extend_heightÚheight)ÚsuperÚ__init__r    r$   )Úselfr/   r0   r2   ©Ú	__class__© ú’/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/widgets/toolbars.pyr:   =   s   
üzFormattedTextToolbar.__init__)r.   )r/   r   r0   r1   r2   r   r3   r4   )Ú__name__Ú
__module__Ú__qualname__r:   Ú__classcell__r>   r>   r<   r?   r*   <   s    r*   c                   @  s>   e Zd ZdZ		ddd
d„Zddd„Zddd„Zddd„ZdS )r,   z^
    Toolbar for a system prompt.

    :param prompt: Prompt to be displayed to the user.
    úShell command: TÚpromptr   Úenable_global_bindingsr   r3   r4   c                   s€   |ˆ _ t|ƒˆ _ttd�ˆ _ˆ  ¡ ˆ _tˆ jt	dd�t
‡ fdd„dd�gˆ jd�ˆ _tˆ jddd	�ˆ _tˆ jtˆ jƒd
�ˆ _d S )N)Únameúclass:system-toolbar.text©r0   c                     s   ˆ j S ©N)rE   r>   ©r;   r>   r?   Ú<lambda>_   s    z(SystemToolbar.__init__.<locals>.<lambda>úclass:system-toolbar)ÚbufferÚlexerÚinput_processorsÚkey_bindingsr5   ©r8   r0   ©ÚcontentÚfilter)rE   r   rF   r   r   Úsystem_bufferÚ_build_key_bindingsÚ	_bindingsr   r&   r%   Úbuffer_controlr   Úwindowr   r   Ú	container)r;   rE   rF   r>   rK   r?   r:   O   s"   

ÿú	ÿÿzSystemToolbar.__init__r   c                 C  s   dd| j jfdgS )N)rM   rD   rH   )r.   Ú
)rV   r/   rK   r>   r>   r?   Ú_get_display_before_textl   s   
ýz&SystemToolbar._get_display_before_textr   c                   s(  t ˆ jƒ}tƒ }|j}|d|d�|d|d�|d|d�d‡ fd	d
„ƒƒƒ}|d|d�d‡ fdd„ƒ}tƒ }|j}|d|d�|d|d�d‡ fdd„ƒƒ}|d|d�d‡ fdd„ƒ}tƒ }	|	j}|tjd| t@ dd�d‡ fdd„ƒ}
|d| t@ t@ dd�d‡ fdd„ƒ}t	t
|tƒt
|tƒt
|	ˆ jƒgƒS )NÚescape)rU   zc-gzc-cÚeventÚEr3   r4   c                   s   ˆ j  ¡  | jj ¡  dS ©zHide system prompt.N)rV   ÚresetÚappÚlayoutÚ
focus_last©r_   rK   r>   r?   Ú_cancelz   s   
z2SystemToolbar._build_key_bindings.<locals>._cancelÚenterc                 “  s>   �| j jˆ jjˆ  ¡ d�I dH  ˆ jjdd� | j j ¡  dS ©zRun system command.)Údisplay_before_textNT)Úappend_to_history)rc   Úrun_system_commandrV   r/   r]   rb   rd   re   rf   rK   r>   r?   Ú_accept‚   s   €þz2SystemToolbar._build_key_bindings.<locals>._acceptc                   s&   t j| jj_ˆ j ¡  | jj ¡  dS ra   )	r   Ú
NAVIGATIONrc   Úvi_stateÚ
input_moderV   rb   rd   re   rf   rK   r>   r?   Ú
_cancel_vi�   s   
z5SystemToolbar._build_key_bindings.<locals>._cancel_vic                 “  sJ   �t j| jj_| jjˆ jjˆ  ¡ d�I dH  ˆ jj	dd� | jj
 ¡  dS ri   )r   rn   rc   ro   rp   rl   rV   r/   r]   rb   rd   re   rf   rK   r>   r?   Ú
_accept_vi˜   s   €þz5SystemToolbar._build_key_bindings.<locals>._accept_viú!T)rU   Ú	is_globalc                   s   | j j ˆ j¡ dS )z#M-'!' will focus this user control.N)rc   rd   ÚfocusrZ   rf   rK   r>   r?   Ú	_focus_me¨   s   z4SystemToolbar._build_key_bindings.<locals>._focus_mec                   s    t j| jj_| jj ˆ j¡ dS )zFocus.N)r   ÚINSERTrc   ro   rp   rd   ru   rZ   rf   rK   r>   r?   Ú_focus_me_vi­   s   z7SystemToolbar._build_key_bindings.<locals>._focus_me_vi)r_   r`   r3   r4   )r   rV   r   Úaddr   ÚEscaper	   r   r   r   r   rF   )r;   ÚfocusedÚemacs_bindingsÚhandlerg   rm   Úvi_bindingsrq   rr   Úglobal_bindingsrv   rx   r>   rK   r?   rW   s   s8   









ýÿz!SystemToolbar._build_key_bindingsr   c                 C  ó   | j S rJ   ©r[   rK   r>   r>   r?   Ú__pt_container__»   ó   zSystemToolbar.__pt_container__N)rD   T)rE   r   rF   r   r3   r4   ©r3   r   )r3   r   ©r3   r   )r@   rA   rB   Ú__doc__r:   r]   rW   r‚   r>   r>   r>   r?   r,   H   s    ý

Hr,   c                   @  ó    e Zd Zd	dd„Zd
dd„ZdS )r(   r3   r4   c                 C  s0   ddd„}t t|ƒdd�| _t| jtd�| _d S )	Nr3   r   c                  S  s&   t ƒ jjpd} | dkrd} dd| fgS )Nr.   ú-z-1)zclass:arg-toolbarzRepeat: zclass:arg-toolbar.text)r   Úkey_processorÚarg)rŠ   r>   r>   r?   Úget_formatted_textÁ   s   þz/ArgToolbar.__init__.<locals>.get_formatted_textr5   ©r8   rS   r„   )r   r    rZ   r   r
   r[   )r;   r‹   r>   r>   r?   r:   À   s   

zArgToolbar.__init__r   c                 C  r€   rJ   r�   rK   r>   r>   r?   r‚   Ï   rƒ   zArgToolbar.__pt_container__N©r3   r4   r…   ©r@   rA   rB   r:   r‚   r>   r>   r>   r?   r(   ¿   s    
r(   c                   @  s2   e Zd ZdZ						dddd„Zddd„ZdS )r+   zs
    :param vi_mode: Display '/' and '?' instead of I-search.
    :param ignore_case: Search case insensitive.
    NFr.   ú
I-search: úI-search backward: Úsearch_bufferúBuffer | Noner   ÚboolÚtext_if_not_searchingr   Úforward_search_promptÚbackward_search_promptÚignore_caser   r3   r4   c                   s~   |d u rt ƒ }td‡fdd„ƒ‰d‡ ‡‡‡‡‡fdd„}|ˆ_t|t|dd	�gtd
d	�|d�ˆ_ttˆjddd�ˆd�ˆ_	d S )Nr3   r“   c                     s   ˆ j tƒ jjv S rJ   )Úcontrolr   rd   Úsearch_linksr>   rK   r>   r?   Úis_searchingå   s   z,SearchToolbar.__init__.<locals>.is_searchingr   c                     s2   ˆƒ sˆS ˆj jjtjkrˆrdS ˆ S ˆrdS ˆS )Nú?ú/)r˜   Úsearcher_search_stateÚ	directionr'   ÚBACKWARDr>   ©r–   r•   rš   r;   r”   r   r>   r?   Úget_before_inputé   s
   z0SearchToolbar.__init__.<locals>.get_before_inputzclass:search-toolbar.promptrI   zclass:search-toolbar.text)rN   rP   rO   r—   r5   zclass:search-toolbarrR   rS   )r3   r“   )r3   r   )
r   r   r‘   r!   r%   r&   r˜   r   r   r[   )r;   r‘   r   r”   r•   r–   r—   r¡   r>   r    r?   r:   Ù   s"   	

ÿú	þzSearchToolbar.__init__r   c                 C  r€   rJ   r�   rK   r>   r>   r?   r‚     rƒ   zSearchToolbar.__pt_container__)NFr.   r�   r�   F)r‘   r’   r   r“   r”   r   r•   r   r–   r   r—   r   r3   r4   r…   )r@   rA   rB   r†   r:   r‚   r>   r>   r>   r?   r+   Ó   s    ù*r+   c                   @  s   e Zd Zd	dd„ZdS )
Ú_CompletionsToolbarControlÚwidthÚintr8   r3   r"   c                   s@  g ‰ t ƒ jj}|r“|j}|j}|d }d}d}g }	t|ƒD ]3\}
}t|	ƒt|jƒ |kr;|
|p0dkr7g }	d}nd} n|	 	t
|j|
|krFdndd�¡ |	 d¡ q|	 d	d
|t|	ƒ  f¡ |	d |… }	ˆ  d¡ ˆ  d|rpdnd
f¡ ˆ  d¡ ˆ  	|	¡ ˆ  d¡ ˆ  d|rŠdnd
f¡ ˆ  d¡ d‡ fdd„}t|dd�S )Né   Fr   Tz+class:completion-toolbar.completion.currentz#class:completion-toolbar.completionrI   )r.   ú r.   r¦   zclass:completion-toolbar.arrowú<ú>Úir¤   r3   r   c                   s   ˆ S rJ   r>   )r©   ©Úall_fragmentsr>   r?   Úget_lineE  s   z;_CompletionsToolbarControl.create_content.<locals>.get_liner5   )r¬   Ú
line_count)r©   r¤   r3   r   )r   Úcurrent_bufferÚcomplete_stateÚcompletionsÚcomplete_indexÚ	enumerater   ÚlenÚdisplay_textÚextendr   Úappendr"   )r;   r£   r8   r¯   r°   ÚindexÚcontent_widthÚcut_leftÚ	cut_rightÚ	fragmentsr©   Úcr¬   r>   rª   r?   Úcreate_content  sP   
ÿûÿ

ÿ


ÿ
z)_CompletionsToolbarControl.create_contentN)r£   r¤   r8   r¤   r3   r"   )r@   rA   rB   r½   r>   r>   r>   r?   r¢     s    r¢   c                   @  r‡   )r)   r3   r4   c                 C  s   t ttƒ ddd�td�| _d S )Nr5   zclass:completion-toolbarrR   rS   )r   r   r¢   r   r[   rK   r>   r>   r?   r:   L  s   ÿüzCompletionsToolbar.__init__r   c                 C  r€   rJ   r�   rK   r>   r>   r?   r‚   T  rƒ   z#CompletionsToolbar.__pt_container__Nr�   r…   rŽ   r>   r>   r>   r?   r)   K  s    
r)   c                   @  s"   e Zd Zdddd„Zdd	d
„ZdS )r-   FÚshow_positionr“   r3   r4   c                   s4   d‡ fdd„}t |ƒ| _tt| jdd�td�| _d S )	Nr3   r   c                    s`   t ƒ j} | jr.| j | jj¡\}}ˆ r%| jj› d|d › d|d › d�}n| jj}d|fgS g S )Nz (line=r5   z column=ú)zclass:validation-toolbar)r   r®   Úvalidation_errorÚdocumentÚtranslate_index_to_positionÚcursor_positionÚmessage)ÚbuffÚrowÚcolumnr/   ©r¾   r>   r?   r‹   Z  s   ÿ$
z6ValidationToolbar.__init__.<locals>.get_formatted_textr5   rŒ   rS   r„   )r    r˜   r   r   r   r[   )r;   r¾   r‹   r>   rÈ   r?   r:   Y  s
   
ÿzValidationToolbar.__init__r   c                 C  r€   rJ   r�   rK   r>   r>   r?   r‚   q  rƒ   z"ValidationToolbar.__pt_container__N)F)r¾   r“   r3   r4   r…   rŽ   r>   r>   r>   r?   r-   X  s    r-   N)@Ú
__future__r   Útypingr   Ú"prompt_toolkit.application.currentr   Úprompt_toolkit.bufferr   Úprompt_toolkit.enumsr   Úprompt_toolkit.filtersr   r   r	   r
   r   r   r   r   r   r   Úprompt_toolkit.formatted_textr   r   r   r   Ú'prompt_toolkit.key_binding.key_bindingsr   r   r   r   Ú(prompt_toolkit.key_binding.key_processorr   Ú#prompt_toolkit.key_binding.vi_stater   Úprompt_toolkit.keysr   Ú prompt_toolkit.layout.containersr   r   r   Úprompt_toolkit.layout.controlsr   r    r!   r"   r#   Úprompt_toolkit.layout.dimensionr$   Ú prompt_toolkit.layout.processorsr%   Úprompt_toolkit.lexersr&   Úprompt_toolkit.searchr'   Ú__all__r`   r*   r,   r(   r+   r¢   r)   r-   r>   r>   r>   r?   Ú<module>   s4    0	w4D