a
    õ£Beý/  ã                   @  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ddddœ‡ fdd„Z‡  ZS )
r*   Ú r   Ústrr   ÚNone)ÚtextÚstyleÚkwÚreturnc                   s*   t ƒ jt|fi |¤Ž|dtdd�d� d S )NTé   )Úmin)r2   Zdont_extend_heightÚheight)ÚsuperÚ__init__r    r$   )Úselfr1   r2   r3   ©Ú	__class__© úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/widgets/toolbars.pyr9   =   s    üzFormattedTextToolbar.__init__)r.   )Ú__name__Ú
__module__Ú__qualname__r9   Ú__classcell__r=   r=   r;   r>   r*   <   s   r*   c                   @  sN   e Zd ZdZdddddœdd	„Zd
dœdd„Zddœdd„Zddœdd„ZdS )r,   z^
    Toolbar for a system prompt.

    :param prompt: Prompt to be displayed to the user.
    úShell command: Tr   r   r0   )ÚpromptÚenable_global_bindingsr4   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©r2   c                     s   ˆ j S ©N)rD   r=   ©r:   r=   r>   Ú<lambda>_   ó    z(SystemToolbar.__init__.<locals>.<lambda>úclass:system-toolbar)ÚbufferÚlexerÚinput_processorsZkey_bindingsr5   ©r7   r2   ©ÚcontentÚfilter)rD   r   rE   r   r   Úsystem_bufferÚ_build_key_bindingsZ	_bindingsr   r&   r%   Zbuffer_controlr   Úwindowr   r   Ú	container)r:   rD   rE   r=   rJ   r>   r9   O   s"    

ÿú	ÿÿzSystemToolbar.__init__r   ©r4   c                 C  s   dd| j jfdgS )N)rM   rC   rG   )r.   Ú
)rU   r1   rJ   r=   r=   r>   Ú_get_display_before_textl   s    
ýz&SystemToolbar._get_display_before_textr   c                   sL  t ˆ jƒ}tƒ }|j}|d|d�|d|d�|d|d�dddœ‡ fdd	„ƒƒƒ}|d
|d�dddœ‡ fdd„ƒ}tƒ }|j}|d|d�|d|d�dddœ‡ fdd„ƒƒ}|d
|d�dddœ‡ fdd„ƒ}tƒ }	|	j}|tjd| t@ dd�dddœ‡ fdd„ƒ}
|d| t@ t@ dd�dddœ‡ fdd„ƒ}t	t
|tƒt
|tƒt
|	ˆ jƒgƒS )NÚescape)rT   zc-gzc-cÚEr0   )Úeventr4   c                   s   ˆ j  ¡  | jj ¡  dS ©zHide system prompt.N)rU   ÚresetÚappÚlayoutÚ
focus_last©r^   rJ   r=   r>   Ú_cancelz   s    
z2SystemToolbar._build_key_bindings.<locals>._cancelZenterc                 “  s<   | j jˆ jjˆ  ¡ d�I dH  ˆ jjdd� | j j ¡  dS ©zRun system command.)Zdisplay_before_textNT)Zappend_to_history)ra   Úrun_system_commandrU   r1   r[   r`   rb   rc   rd   rJ   r=   r>   Ú_accept‚   s    þz2SystemToolbar._build_key_bindings.<locals>._acceptc                   s&   t j| jj_ˆ j ¡  | jj ¡  dS r_   )	r   Ú
NAVIGATIONra   Úvi_stateÚ
input_moderU   r`   rb   rc   rd   rJ   r=   r>   Ú
_cancel_vi�   s    
z5SystemToolbar._build_key_bindings.<locals>._cancel_vic                 “  sH   t j| jj_| jjˆ jjˆ  ¡ d�I dH  ˆ jj	dd� | jj
 ¡  dS rf   )r   ri   ra   rj   rk   rg   rU   r1   r[   r`   rb   rc   rd   rJ   r=   r>   Ú
_accept_vi˜   s    þz5SystemToolbar._build_key_bindings.<locals>._accept_viú!T)rT   Ú	is_globalc                   s   | j j ˆ j¡ dS )z#M-'!' will focus this user control.N)ra   rb   ÚfocusrW   rd   rJ   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   ZINSERTra   rj   rk   rb   rp   rW   rd   rJ   r=   r>   Ú_focus_me_vi­   s    z7SystemToolbar._build_key_bindings.<locals>._focus_me_vi)r   rU   r   Úaddr   ÚEscaper	   r   r   r   r   rE   )r:   ZfocusedZemacs_bindingsÚhandlere   rh   Zvi_bindingsrl   rm   Zglobal_bindingsrq   rr   r=   rJ   r>   rV   s   s8    









ýÿz!SystemToolbar._build_key_bindingsr   c                 C  s   | j S rI   ©rX   rJ   r=   r=   r>   Ú__pt_container__»   s    zSystemToolbar.__pt_container__N)rC   T)r?   r@   rA   Ú__doc__r9   r[   rV   rw   r=   r=   r=   r>   r,   H   s     ýHr,   c                   @  s(   e Zd Zddœdd„Zddœdd„ZdS )	r(   r0   rY   c                 C  s4   ddœdd„}t t|ƒdd�| _t| jtd�| _d S )Nr   rY   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   Zkey_processorÚarg)rz   r=   r=   r>   Úget_formatted_textÁ   s    þz/ArgToolbar.__init__.<locals>.get_formatted_textr5   ©r7   rR   )r   r    rW   r   r
   rX   )r:   r{   r=   r=   r>   r9   À   s    
zArgToolbar.__init__r   c                 C  s   | j S rI   rv   rJ   r=   r=   r>   rw   Ï   s    zArgToolbar.__pt_container__N©r?   r@   rA   r9   rw   r=   r=   r=   r>   r(   ¿   s   r(   c                	   @  s:   e Zd ZdZdddd	d	d	d
ddœdd„Zd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: zBuffer | NoneÚboolr   r   r0   )Úsearch_bufferr   Útext_if_not_searchingÚforward_search_promptÚbackward_search_promptÚignore_caser4   c                   s†   |d u rt ƒ }tddœ‡fdd„ƒ‰ddœ‡ ‡‡‡‡‡fdd„}|ˆ_t|t|dd	�gtd
d	�|d�ˆ_ttˆjddd�ˆd�ˆ_	d S )Nr€   rY   c                     s   ˆ j tƒ jjv S rI   )Úcontrolr   rb   Zsearch_linksr=   rJ   r=   r>   Úis_searchingå   s    z,SearchToolbar.__init__.<locals>.is_searchingr   c                     s6   ˆƒ s
ˆS ˆj jjtjkr&ˆr"dS ˆ S ˆr.dS ˆS d S )Nú?ú/)r†   Zsearcher_search_stateÚ	directionr'   ZBACKWARDr=   ©r„   rƒ   r‡   r:   r‚   r   r=   r>   Úget_before_inputé   s    ÿz0SearchToolbar.__init__.<locals>.get_before_inputzclass:search-toolbar.promptrH   zclass:search-toolbar.text)rN   rP   rO   r…   r5   zclass:search-toolbarrQ   rR   )
r   r   r�   r!   r%   r&   r†   r   r   rX   )r:   r�   r   r‚   rƒ   r„   r…   rŒ   r=   r‹   r>   r9   Ù   s"    	

ÿú	þzSearchToolbar.__init__r   rY   c                 C  s   | j S rI   rv   rJ   r=   r=   r>   rw     s    zSearchToolbar.__pt_container__)NFr.   r~   r   F)r?   r@   rA   rx   r9   rw   r=   r=   r=   r>   r+   Ó   s         ù*r+   c                   @  s   e Zd Zddddœdd„ZdS )Ú_CompletionsToolbarControlÚintr"   )Úwidthr7   r4   c                   sJ  g ‰ t ƒ jj}|�r*|j}|j}|d }d}d}g }	t|ƒD ]f\}
}t|	ƒt|jƒ |krx|
|pbdkrpg }	d}nd} q¤|	 	t
|j|
|krŽdndd�¡ |	 d¡ q<|	 d	d
|t|	ƒ  f¡ |	d |… }	ˆ  d¡ ˆ  d|râdnd
f¡ ˆ  d¡ ˆ  	|	¡ ˆ  d¡ ˆ  d|�rdnd
f¡ ˆ  d¡ dddœ‡ fdd„}t|dd�S )Né   Fr   Tz+class:completion-toolbar.completion.currentz#class:completion-toolbar.completionrH   )r.   ú r.   r‘   zclass:completion-toolbar.arrowú<ú>rŽ   r   )Úir4   c                   s   ˆ S rI   r=   )r”   ©Zall_fragmentsr=   r>   Úget_lineE  s    z;_CompletionsToolbarControl.create_content.<locals>.get_liner5   )r–   Ú
line_count)r   Úcurrent_bufferÚcomplete_stateÚcompletionsZcomplete_indexÚ	enumerater   ÚlenZdisplay_textÚextendr   Úappendr"   )r:   r�   r7   r™   rš   ÚindexZcontent_widthZcut_leftZ	cut_rightÚ	fragmentsr”   Úcr–   r=   r•   r>   Úcreate_content  sP    
ÿûÿ

ÿ


ÿ
z)_CompletionsToolbarControl.create_contentN)r?   r@   rA   r¢   r=   r=   r=   r>   r�     s   r�   c                   @  s(   e Zd Zddœdd„Zddœdd„ZdS )	r)   r0   rY   c                 C  s   t ttƒ ddd�td�| _d S )Nr5   zclass:completion-toolbarrQ   rR   )r   r   r�   r   rX   rJ   r=   r=   r>   r9   L  s    ÿüzCompletionsToolbar.__init__r   c                 C  s   | j S rI   rv   rJ   r=   r=   r>   rw   T  s    z#CompletionsToolbar.__pt_container__Nr}   r=   r=   r=   r>   r)   K  s   r)   c                   @  s,   e Zd Zddddœdd„Zddœd	d
„ZdS )r-   Fr€   r0   )Úshow_positionr4   c                   s8   ddœ‡ fdd„}t |ƒ| _tt| jdd�td�| _d S )Nr   rY   c                    s\   t ƒ j} | jrT| j | jj¡\}}ˆ rBd | jj|d |d ¡}n| jj}d|fgS g S d S )Nz{} (line={} column={})r5   zclass:validation-toolbar)r   r˜   Zvalidation_errorZdocumentZtranslate_index_to_positionZcursor_positionÚformatÚmessage)ZbuffÚrowÚcolumnr1   ©r£   r=   r>   r{   Z  s    ÿý
z6ValidationToolbar.__init__.<locals>.get_formatted_textr5   r|   rR   )r    r†   r   r   r   rX   )r:   r£   r{   r=   r¨   r>   r9   Y  s
    
ÿzValidationToolbar.__init__r   rY   c                 C  s   | j S rI   rv   rJ   r=   r=   r>   rw   u  s    z"ValidationToolbar.__pt_container__N)Fr}   r=   r=   r=   r>   r-   X  s   r-   N)@Ú
__future__r   Útypingr   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.bufferr   Zprompt_toolkit.enumsr   Zprompt_toolkit.filtersr   r   r	   r
   r   r   r   r   r   r   Zprompt_toolkit.formatted_textr   r   r   r   Z'prompt_toolkit.key_binding.key_bindingsr   r   r   r   Z(prompt_toolkit.key_binding.key_processorr   Z#prompt_toolkit.key_binding.vi_stater   Zprompt_toolkit.keysr   Z prompt_toolkit.layout.containersr   r   r   Zprompt_toolkit.layout.controlsr   r    r!   r"   r#   Zprompt_toolkit.layout.dimensionr$   Z prompt_toolkit.layout.processorsr%   Zprompt_toolkit.lexersr&   Zprompt_toolkit.searchr'   Ú__all__r]   r*   r,   r(   r+   r�   r)   r-   r=   r=   r=   r>   Ú<module>   s2   0	w4D