a
    õ£Be/#  ã                   @  s,  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	 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,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 g d¢Z6dNdddddddœdd„Z7e	d ƒZ8ddg dfddd!dd"d#œd$d%„Z9dOddddd)d*d+ddd,d-œ
d.d/„Z:dPddddd1d2œd3d4„Z;dQddddd5d6dd"d7œd8d9„Z<dRddddd5d:dd;d<œd=d>„Z=ddd?d@„ dfdddAdd1dBœdCdD„Z>dEddFdGœdHdI„Z?dJdKœdLdM„Z@dS )Sé    )ÚannotationsN)Úget_running_loop)ÚAnyÚCallableÚSequenceÚ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©r4   c                   S  s   t ƒ jdd� d S )NT©Úresult©r	   Úexit© r;   r;   úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/shortcuts/dialogs.pyÚyes_handler9   s    z"yes_no_dialog.<locals>.yes_handlerc                   S  s   t ƒ jdd� d S )NFr7   r9   r;   r;   r;   r<   Ú
no_handler<   s    z!yes_no_dialog.<locals>.no_handlerT©r0   Zdont_extend_height©r0   Úhandler©r/   ÚbodyÚbuttonsÚwith_background)r   r   r   Ú_create_app)r/   r0   r1   r2   r3   r=   r>   Údialogr;   r;   r<   r$   -   s    


þù
r$   Ú_Tzlist[tuple[str, _T]]zApplication[_T])r/   r0   rD   r3   r4   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.
    rH   r5   )Úvr4   c                 S  s   t ƒ j| d� d S ©Nr7   r9   )rI   r;   r;   r<   Úbutton_handlerZ   s    z%button_dialog.<locals>.button_handlerTr?   c                   s$   g | ]\}}t |t ˆ |¡d �‘qS )r@   )r   Ú	functoolsÚpartial)Ú.0ÚtrI   ©rK   r;   r<   Ú
<listcomp>`   s   ÿz!button_dialog.<locals>.<listcomp>rB   )r   r   rF   )r/   r0   rD   r3   rG   r;   rP   r<   r%   O   s    

þù
r%   ÚOKÚCancelFzCompleter | NonezValidator | Noner   zApplication[str])
r/   r0   Úok_textÚcancel_textÚ	completerÚ	validatorÚpasswordr3   Údefaultr4   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)Úbufr4   c                   s   t ƒ j ˆ ¡ dS )NT)r	   ÚlayoutZfocus)r[   )Ú	ok_buttonr;   r<   Úacceptz   s    zinput_dialog.<locals>.acceptr5   r6   c                     s   t ƒ jˆ jd� d S rJ   )r	   r:   r0   r;   )Ú	textfieldr;   r<   Ú
ok_handler~   s    z input_dialog.<locals>.ok_handlerr@   F)r0   Ú	multilinerX   rV   rW   Zaccept_handlerTr?   é   )Ú	preferredÚmax©ÚpaddingrB   )	r   Ú_return_noner"   r   r   r   r#   ÚDrF   )r/   r0   rT   rU   rV   rW   rX   r3   rY   r^   r`   Zcancel_buttonrG   r;   )r]   r_   r<   r&   j   s2    ú	
ý
úõr&   ÚOkzApplication[None])r/   r0   rT   r3   r4   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.
    Tr?   r@   rB   )r   r   r   rg   rF   )r/   r0   rT   r3   rG   r;   r;   r<   r'   ž   s    	
ür'   z,Sequence[tuple[_T, AnyFormattedText]] | Nonez	_T | None)r/   r0   rT   rU   ÚvaluesrY   r3   r4   c           	        sl   |du 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.
    Nr5   r6   c                     s   t ƒ jˆ jd� d S rJ   )r	   r:   Úcurrent_valuer;   ©Z
radio_listr;   r<   r`   Ã   s    z$radiolist_dialog.<locals>.ok_handler)rj   rY   Tr?   rb   re   r@   rB   )r!   r   r   r   r   rg   rF   )	r/   r0   rT   rU   rj   rY   r3   r`   rG   r;   rl   r<   r(   ±   s     þ

þör(   zSequence[_T] | NonezApplication[list[_T]])r/   r0   rT   rU   rj   Údefault_valuesr3   r4   c           	        sl   |du 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.
    Nr5   r6   c                     s   t ƒ jˆ jd� d S rJ   )r	   r:   Zcurrent_valuesr;   ©Zcb_listr;   r<   r`   ê   s    z'checkboxlist_dialog.<locals>.ok_handler)rj   rm   Tr?   rb   re   r@   rB   )r   r   r   r   r   rg   rF   )	r/   r0   rT   rU   rj   rm   r3   r`   rG   r;   rn   r<   r)   Ø   s     þ

þör)   c                  G  s   d S ©Nr;   )Úar;   r;   r<   Ú<lambda>  ó    rq   z>Callable[[Callable[[int], None], Callable[[str], None]], None])r/   r0   Úrun_callbackr3   r4   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(	 )rc   )Z	focusableÚheight©r0   rb   re   T)rC   r/   rE   Úintr5   )Úvaluer4   c                   s   t | ƒˆ_ˆ  ¡  d S ro   )rv   Ú
percentageÚ
invalidate)rw   )ÚappÚprogressbarr;   r<   Úset_percentage!  s    
z'progress_dialog.<locals>.set_percentager.   )r0   r4   c                   s   ˆ  ˆjj| ¡ ˆ  ¡  d S ro   )Úcall_soon_threadsafeÚbufferZinsert_textry   ru   )rz   ÚloopÚ	text_arear;   r<   Úlog_text%  s    z!progress_dialog.<locals>.log_textr6   c                     s&   zˆˆˆƒ W ˆ   ¡  n
ˆ   ¡  0 d S ro   )r:   r;   )rz   r�   rs   r|   r;   r<   Ústart+  s    zprogress_dialog.<locals>.startc                     s   t ˆ ƒ d S ro   r   r;   )r‚   r;   r<   Úpre_run1  s    z progress_dialog.<locals>.pre_run)r   r    r"   rh   r   r   r   r   ÚexactrF   Zpre_run_callablesÚappend)r/   r0   rs   r3   rG   rƒ   r;   )rz   r�   r   r{   rs   r|   r‚   r€   r<   r*   ÿ   s.    üýÿ÷
r*   r   zApplication[Any])rG   r3   r4   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_supportr3   Zfull_screen)r   Úaddr   r   r   r   r   r   )rG   r3   Zbindingsr;   r;   r<   rF   9  s    ûrF   r5   r6   c                   C  s   t ƒ  ¡  dS )z!Button handler that returns None.Nr9   r;   r;   r;   r<   rg   H  s    rg   )r+   r+   r,   r-   N)	r+   r+   rR   rS   NNFNr+   )r+   r+   ri   N)r+   r+   ri   rS   NNN)r+   r+   ri   rS   NNN)AÚ
__future__r   rL   Úasyncior   Útypingr   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   rh   Zprompt_toolkit.stylesr   Zprompt_toolkit.validationr   Zprompt_toolkit.widgetsr   r   r   r   r   r    r!   r"   r#   Ú__all__r$   rH   r%   r&   r'   r(   r)   r*   rF   rg   r;   r;   r;   r<   Ú<module>   sŒ   ,     ûü         ÷"5    ü       ù(       ù(ú: