U
    Z+d0                     @  s  d dl mZ d dl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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l0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 ddddddgZ8e!Z9G dd de)Z:G 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Optional)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||dtddd d S )NT   )min)r3   Zdont_extend_heightheight)super__init__r!   r%   )selfr2   r3   r4   	__class__ C/tmp/pip-unpacked-wheel-blk8czbf/prompt_toolkit/widgets/toolbars.pyr:   =   s    
zFormattedTextToolbar.__init__)r/   )__name__
__module____qualname__r:   __classcell__r>   r>   r<   r?   r+   <   s   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	   r1   )promptenable_global_bindingsr5   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r3   c                     s    j S N)rE   r>   r;   r>   r?   <lambda>_       z(SystemToolbar.__init__.<locals>.<lambda>class:system-toolbar)bufferlexerinput_processorsZkey_bindingsr6   r8   r3   contentfilter)rE   r   rF   r   r   system_buffer_build_key_bindingsZ	_bindingsr    r'   r&   Zbuffer_controlr   windowr   r   	container)r;   rE   rF   r>   rK   r?   r:   O   s(    

	   zSystemToolbar.__init__r   r5   c                 C  s   dd| j jfdgS )N)rN   rD   rH   )r/   
)rV   r2   rK   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)rU   zc-gzc-cEr1   )eventr5   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>._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)rb   run_system_commandrV   r2   r\   ra   rc   rd   re   rK   r>   r?   _accept   s    z2SystemToolbar._build_key_bindings.<locals>._acceptc                   s&   t j| jj_ j  | jj  dS r`   )	r   
NAVIGATIONrb   vi_state
input_moderV   ra   rc   rd   re   rK   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 rg   )r   rj   rb   rk   rl   rh   rV   r2   r\   ra   rc   rd   re   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)rb   rc   focusrX   re   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   ZINSERTrb   rk   rl   rc   rq   rX   re   rK   r>   r?   _focus_me_vi   s    z7SystemToolbar._build_key_bindings.<locals>._focus_me_vi)r   rV   r   addr   ZEscaper
   r   r   r   r   rF   )r;   ZfocusedZemacs_bindingshandlerf   ri   Zvi_bindingsrm   rn   Zglobal_bindingsrr   rs   r>   rK   r?   rW   s   s8    









z!SystemToolbar._build_key_bindingsr   c                 C  s   | j S rJ   rY   rK   r>   r>   r?   __pt_container__   s    zSystemToolbar.__pt_container__N)rD   T)r@   rA   rB   __doc__r:   r\   rW   rw   r>   r>   r>   r?   r-   H   s     Hc                   @  s(   e Zd ZddddZddddZdS )	r)   r1   rZ   c                 C  s4   dddd}t t|dd| _t| jtd| _d S )Nr   rZ   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_textr6   r8   rS   )r   r!   rX   r   r   rY   )r;   r{   r>   r>   r?   r:      s    
zArgToolbar.__init__r   c                 C  s   | j S rJ   rv   rK   r>   r>   r?   rw      s    zArgToolbar.__pt_container__Nr@   rA   rB   r:   rw   r>   r>   r>   r?   r)      s   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	   r1   )search_bufferr   text_if_not_searchingforward_search_promptbackward_search_promptignore_caser5   c                   s   |d k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   rZ   c                     s    j t jjkS rJ   )controlr   rc   Zsearch_linksr>   rK   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.promptrI   zclass:search-toolbar.text)rO   rQ   rP   r   r6   zclass:search-toolbarrR   rS   )
r   r   r   r"   r&   r'   r   r   r   rY   )r;   r   r   r   r   r   r   r   r>   r   r?   r:      s"    	

	zSearchToolbar.__init__r   rZ   c                 C  s   | j S rJ   rv   rK   r>   r>   r?   rw     s    zSearchToolbar.__pt_container__)NFr/   r~   r   F)r@   rA   rB   rx   r:   rw   r>   r>   r>   r?   r,      s         *c                   @  s   e Zd ZddddddZdS )_CompletionsToolbarControlintr#   )widthr8   r5   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rdndd |	d q<|	d	d
|t|	  f |	d | }	 d  d|rd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.completionrI   )r/    r/   r   zclass:completion-toolbar.arrow<>r   r   )ir5   c                   s    S rJ   r>   )r   Zall_fragmentsr>   r?   get_lineE  s    z;_CompletionsToolbarControl.create_content.<locals>.get_liner6   )r   
line_count)r   current_buffercomplete_statecompletionsZcomplete_index	enumerater   lenZdisplay_textextendr   appendr#   )r;   r   r8   r   r   indexZcontent_widthZcut_leftZ	cut_rightZ	fragmentsr   cr   r>   r   r?   create_content  sP    






z)_CompletionsToolbarControl.create_contentN)r@   rA   rB   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*   r1   rZ   c                 C  s   t tt dddtd| _d S )Nr6   zclass:completion-toolbarrR   rS   )r   r   r   r   rY   rK   r>   r>   r?   r:   L  s      zCompletionsToolbar.__init__r   c                 C  s   | j S rJ   rv   rK   r>   r>   r?   rw   T  s    z#CompletionsToolbar.__pt_container__Nr}   r>   r>   r>   r?   r*   K  s   c                   @  s,   e Zd ZddddddZddd	d
ZdS )r.   Fr   r1   )show_positionr5   c                   s8   dd fdd}t || _tt| jddtd| _d S )Nr   rZ   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={})r6   zclass:validation-toolbar)r   r   Zvalidation_errordocumentZtranslate_index_to_positionZcursor_positionformatmessage)Zbuffrowcolumnr2   r   r>   r?   r{   Z  s    
z6ValidationToolbar.__init__.<locals>.get_formatted_textr6   r|   rS   )r!   r   r   r   r   rY   )r;   r   r{   r>   r   r?   r:   Y  s    
 zValidationToolbar.__init__r   rZ   c                 C  s   | j S rJ   rv   rK   r>   r>   r?   rw   u  s    z"ValidationToolbar.__pt_container__N)Fr}   r>   r>   r>   r?   r.   X  s   N)A
__future__r   typingr   r   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>   s>   0	w4D