U
    dF#                     @  sB  d dl mZ 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 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 d dlmZ d dlmZ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' d dl(m)Z* d dl+m,Z, d dl-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 dddddddgZ9dMddd d d!d"d#d$dZ:ed%Z;ddg dfddd&d!d'd(d)dZ<dNddd d d-d.d/d!d d0d1
d2dZ=dOddd d!d4d5d6dZ>dPddd d d7d8d!d'd9d:dZ?dQddd d d7d;d!d<d=d>dZ@ddd?d@ dfdddAd!d4dBdCdZAdDd!dEdFdGdHZBdIdJdKdLZCdS )R    )annotationsN)get_running_loop)AnyCallableListOptionalSequenceTupleTypeVar)Application)get_app)Buffer)	Completerrun_in_executor_with_context)FilterOrBool)AnyFormattedText)
focus_nextfocus_previous)load_key_bindings)KeyBindingsmerge_key_bindings)Layout)AnyContainerHSplit)	Dimension)	BaseStyle)	Validator)	BoxButtonCheckboxListDialogLabelProgressBar	RadioListTextAreaValidationToolbaryes_no_dialogbutton_dialoginput_dialogmessage_dialogradiolist_dialogcheckboxlist_dialogprogress_dialog YesNor   strzBaseStyle | NonezApplication[bool])titletextyes_textno_textstylereturnc                 C  sR   dddd}dddd}t | t|ddt||d	t||d	gdd
}t||S )z8
    Display a Yes/No dialog.
    Return a boolean.
    Noner7   c                   S  s   t  jdd d S )NTresultr   exit r>   r>   D/tmp/pip-unpacked-wheel-4x_7prb2/prompt_toolkit/shortcuts/dialogs.pyyes_handler9   s    z"yes_no_dialog.<locals>.yes_handlerc                   S  s   t  jdd d S )NFr:   r<   r>   r>   r>   r?   
no_handler<   s    z!yes_no_dialog.<locals>.no_handlerTr3   Zdont_extend_heightr3   handlerr2   bodybuttonswith_background)r!   r"   r   _create_app)r2   r3   r4   r5   r6   r@   rA   dialogr>   r>   r?   r'   -   s    



_Tzlist[tuple[str, _T]]zApplication[_T])r2   r3   rG   r6   r7   c                   s@   ddddd t | t|dd fdd	|D dd
}t||S )zx
    Display a dialog with button choices (given as a list of tuples).
    Return the value associated with button.
    rK   r8   )vr7   c                 S  s   t  j| d d S Nr:   r<   )rL   r>   r>   r?   button_handlerZ   s    z%button_dialog.<locals>.button_handlerTrB   c                   s$   g | ]\}}t |t |d qS )rC   )r   	functoolspartial).0trL   rN   r>   r?   
<listcomp>`   s   z!button_dialog.<locals>.<listcomp>rE   )r!   r"   rI   )r2   r3   rG   r6   rJ   r>   rS   r?   r(   O   s    


OKCancelFzCompleter | NonezValidator | Noner   zApplication[str])
r2   r3   ok_textcancel_text	completer	validatorpasswordr6   defaultr7   c	                   s   ddd fdd}	ddfdd	}
t ||
d
 t |td
}t|d||||	dt| tt|ddt gtdddd |gdd}t||S )zV
    Display a text input box.
    Return the given text, or None when cancelled.
    r   bool)bufr7   c                   s   t  j  dS )NT)r   layoutZfocus)r^   )	ok_buttonr>   r?   acceptz   s    zinput_dialog.<locals>.acceptr8   r9   c                     s   t  j jd d S rM   )r   r=   r3   r>   )	textfieldr>   r?   
ok_handler~   s    z input_dialog.<locals>.ok_handlerrC   F)r3   	multiliner[   rY   rZ   Zaccept_handlerTrB      )	preferredmaxpaddingrE   )	r   _return_noner%   r!   r   r"   r&   DrI   )r2   r3   rW   rX   rY   rZ   r[   r6   r\   ra   rc   Zcancel_buttonrJ   r>   )r`   rb   r?   r)   j   s2    	

OkzApplication[None])r2   r3   rW   r6   r7   c                 C  s,   t | t|ddt|tdgdd}t||S )zM
    Display a simple message box and wait until the user presses enter.
    TrB   rC   rE   )r!   r"   r   rj   rI   )r2   r3   rW   r6   rJ   r>   r>   r?   r*      s    	
z,Sequence[tuple[_T, AnyFormattedText]] | Nonez	_T | None)r2   r3   rW   rX   valuesr\   r6   r7   c           	        sl   |dkrg }dd fdd}t ||d t| tt|dd gd	d
t||dt|tdgdd}t||S )z
    Display a simple list of element the user can choose amongst.

    Only one element can be selected at a time using Arrow keys and Enter.
    The focus can be moved between the list and the Ok/Cancel button with tab.
    Nr8   r9   c                     s   t  j jd d S rM   )r   r=   Zcurrent_valuer>   Z
radio_listr>   r?   rc      s    z$radiolist_dialog.<locals>.ok_handler)rm   r\   TrB   re   rh   rC   rE   )r$   r!   r   r"   r   rj   rI   )	r2   r3   rW   rX   rm   r\   r6   rc   rJ   r>   rn   r?   r+      s     

zSequence[_T] | NonezApplication[list[_T]])r2   r3   rW   rX   rm   default_valuesr6   r7   c           	        sl   |dkrg }dd fdd}t ||d t| tt|dd gd	d
t||dt|tdgdd}t||S )z
    Display a simple list of element the user can choose multiple values amongst.

    Several elements can be selected at a time using Arrow keys and Enter.
    The focus can be moved between the list and the Ok/Cancel button with tab.
    Nr8   r9   c                     s   t  j jd d S rM   )r   r=   Zcurrent_valuesr>   Zcb_listr>   r?   rc      s    z'checkboxlist_dialog.<locals>.ok_handler)rm   ro   TrB   re   rh   rC   rE   )r    r!   r   r"   r   rj   rI   )	r2   r3   rW   rX   rm   ro   r6   rc   rJ   r>   rp   r?   r,      s     

c                  G  s   d S Nr>   )ar>   r>   r?   <lambda>      rs   z>Callable[[Callable[[int], None], Callable[[str], None]], None])r2   r3   run_callbackr6   r7   c                   s   t  t tdtdddtttt|dttddg| dd	}t	|| d
dd fddddd fdddd fddddfdd} j
|  S )zz
    :param run_callback: A function that receives as input a `set_percentage`
        function and it does the work.
    Fl    d(	 )rf   )Z	focusableheightr3   re   rh   T)rF   r2   rH   intr8   )valuer7   c                   s   t | _   d S rq   )rx   
percentage
invalidate)ry   )appprogressbarr>   r?   set_percentage!  s    
z'progress_dialog.<locals>.set_percentager1   )r3   r7   c                   s    jj|     d S rq   )Zcall_soon_threadsafebufferZinsert_textr{   rw   )r|   loop	text_arear>   r?   log_text%  s    z!progress_dialog.<locals>.log_textr9   c                     s   z W 5     X d S rq   )r=   r>   )r|   r   ru   r~   r>   r?   start+  s    zprogress_dialog.<locals>.startc                     s   t   d S rq   r   r>   )r   r>   r?   pre_run1  s    z progress_dialog.<locals>.pre_run)r   r#   r%   rk   r!   r   r   r"   exactrI   Zpre_run_callablesappend)r2   r3   ru   r6   rJ   r   r>   )r|   r   r   r}   ru   r~   r   r   r?   r-      s.    
r   zApplication[Any])rJ   r6   r7   c                 C  sB   t  }|dt |dt tt| tt |gd|ddS )Ntabzs-tabT)r_   Zkey_bindingsZmouse_supportr6   Zfull_screen)r   addr   r   r   r   r   r   )rJ   r6   Zbindingsr>   r>   r?   rI   9  s    rI   r8   r9   c                   C  s   t    dS )z!Button handler that returns None.Nr<   r>   r>   r>   r?   rj   H  s    rj   )r.   r.   r/   r0   N)	r.   r.   rU   rV   NNFNr.   )r.   r.   rl   N)r.   r.   rl   rV   NNN)r.   r.   rl   rV   NNN)D
__future__r   rO   Zasyncior   typingr   r   r   r   r   r	   r
   Zprompt_toolkit.applicationr   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.bufferr   Zprompt_toolkit.completionr   Zprompt_toolkit.eventloopr   Zprompt_toolkit.filtersr   Zprompt_toolkit.formatted_textr   Z)prompt_toolkit.key_binding.bindings.focusr   r   Z#prompt_toolkit.key_binding.defaultsr   Z'prompt_toolkit.key_binding.key_bindingsr   r   Zprompt_toolkit.layoutr   Z prompt_toolkit.layout.containersr   r   Zprompt_toolkit.layout.dimensionr   rk   Zprompt_toolkit.stylesr   Zprompt_toolkit.validationr   Zprompt_toolkit.widgetsr   r   r    r!   r"   r#   r$   r%   r&   __all__r'   rK   r(   r)   r*   r+   r,   r-   rI   rj   r>   r>   r>   r?   <module>   s   $,              "5           (       (: