o
    jY#                     @  s  d dl mZ d dlZd dlmZ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 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.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 g dZ5					dWdXd#d$Z6ed%Z7ddg dfdYd)d*Z8			+	,			-		dZd[d8d9Z9			:	d\d]d<d=Z:			:	,			d^d_dBdCZ;			:	,			d^d`dGdHZ<dddIdJ dfdadMdNZ=dbdRdSZ>dcdUdVZ?dS )d    )annotationsN)CallableSequence)Any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Notitler   textyes_textstrno_textstyleBaseStyle | NonereturnApplication[bool]c                 C  sJ   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.
    r4   Nonec                   S     t  jdd d S )NTresultr   exit r<   r<   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/shortcuts/dialogs.pyyes_handler9      z"yes_no_dialog.<locals>.yes_handlerc                   S  r7   )NFr8   r:   r<   r<   r<   r=   
no_handler<   r?   z!yes_no_dialog.<locals>.no_handlerTr.   dont_extend_heightr.   handlerr-   bodybuttonswith_backgroundNr4   r6   )r   r   r   _create_app)r-   r.   r/   r1   r2   r>   r@   dialogr<   r<   r=   r#   -   s   






r#   _TrG   list[tuple[str, _T]]Application[_T]c                   s:   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.
    vrL   r4   r6   c                 S  s   t  j| d d S Nr8   r:   )rO   r<   r<   r=   button_handlerZ   r?   z%button_dialog.<locals>.button_handlerTrA   c                   s$   g | ]\}}t |t |d qS )rC   )r   	functoolspartial).0trO   rQ   r<   r=   
<listcomp>`   s    z!button_dialog.<locals>.<listcomp>rE   N)rO   rL   r4   r6   )r   r   rJ   )r-   r.   rG   r2   rK   r<   rV   r=   r$   O   s   




r$   OKCancelFok_textcancel_text	completerCompleter | None	validatorValidator | Nonepasswordr   defaultApplication[str | None]c	                   s   d f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.
    bufr	   r4   boolc                   s   t  j  dS )NT)r   layoutfocus)rc   )	ok_buttonr<   r=   acceptz   s   zinput_dialog.<locals>.acceptr6   c                        t  j jd d S rP   )r   r;   r.   r<   )	textfieldr<   r=   
ok_handler~      z input_dialog.<locals>.ok_handlerrC   F)r.   	multiliner`   r\   r^   accept_handlerTrA      )	preferredmaxpaddingrE   N)rc   r	   r4   rd   rI   )	r   _return_noner!   r   r   r   r"   DrJ   )r-   r.   rZ   r[   r\   r^   r`   r2   ra   rh   rk   cancel_buttonrK   r<   )rg   rj   r=   r%   j   s2   	


r%   OkApplication[None]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.
    TrA   rC   rE   )r   r   r   rt   rJ   )r-   r.   rZ   r2   rK   r<   r<   r=   r&      s   	

r&   values,Sequence[tuple[_T, AnyFormattedText]] | None	_T | NoneApplication[_T | None]c           	        h   |du rg }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.
    Nr4   r6   c                     ri   rP   )r   r;   current_valuer<   
radio_listr<   r=   rk      rl   z$radiolist_dialog.<locals>.ok_handler)ry   ra   TrA   ro   rr   rC   rE   rI   )r    r   r   r   r   rt   rJ   )	r-   r.   rZ   r[   ry   ra   r2   rk   rK   r<   r   r=   r'          


r'   default_valuesSequence[_T] | NoneApplication[list[_T] | None]c           	        r}   )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.
    Nr4   r6   c                     ri   rP   )r   r;   current_valuesr<   cb_listr<   r=   rk      rl   z'checkboxlist_dialog.<locals>.ok_handler)ry   r   TrA   ro   rr   rC   rE   rI   )r   r   r   r   r   rt   rJ   )	r-   r.   rZ   r[   ry   r   r2   rk   rK   r<   r   r=   r(      r   r(   c                  G  s   d S Nr<   )ar<   r<   r=   <lambda>  s    r   run_callback>Callable[[Callable[[int], None], Callable[[str], None]], None]c                   s   t  tdtdddtttt|dttddg| dd	}t|| d fddd fddd f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(	 )rp   )	focusableheight)r.   ro   rr   T)rF   r-   rH   valueintr4   r6   c                   s   t | _   d S r   )r   
percentage
invalidate)r   )appprogressbarr<   r=   set_percentage   s   
z'progress_dialog.<locals>.set_percentager.   r0   c                   s.    j }|d ur|jj|     d S d S r   )loopcall_soon_threadsafebufferinsert_textr   )r.   r   )r   	text_arear<   r=   log_text$  s
   z!progress_dialog.<locals>.log_textc                     s$   z W     d S     w r   )r;   r<   )r   r   r   r   r<   r=   start,  s   zprogress_dialog.<locals>.startc                     s   t   d S r   r   r<   )r   r<   r=   pre_run2  s   z progress_dialog.<locals>.pre_runN)r   r   r4   r6   )r.   r0   r4   r6   rI   )r   r!   ru   r   r   r   r   exactrJ   pre_run_callablesappend)r-   r.   r   r2   rK   r   r<   )r   r   r   r   r   r   r   r=   r)      s,   
r)   rK   r   Application[Any]c                 C  sB   t  }|dt |dt tt| tt |gd|ddS )Ntabzs-tabT)re   key_bindingsmouse_supportr2   full_screen)r   addr   r   r   r   r   r   )rK   r2   bindingsr<   r<   r=   rJ   :  s   rJ   r6   c                   C  s   t    dS )z!Button handler that returns None.Nr:   r<   r<   r<   r=   rt   I  s   rt   )r*   r*   r+   r,   N)r-   r   r.   r   r/   r0   r1   r0   r2   r3   r4   r5   )
r-   r   r.   r   rG   rM   r2   r3   r4   rN   )	r*   r*   rX   rY   NNFNr*   )r-   r   r.   r   rZ   r0   r[   r0   r\   r]   r^   r_   r`   r   r2   r3   ra   r0   r4   rb   )r*   r*   rw   N)
r-   r   r.   r   rZ   r0   r2   r3   r4   rx   )r*   r*   rw   rY   NNN)r-   r   r.   r   rZ   r0   r[   r0   ry   rz   ra   r{   r2   r3   r4   r|   )r-   r   r.   r   rZ   r0   r[   r0   ry   rz   r   r   r2   r3   r4   r   )
r-   r   r.   r   r   r   r2   r3   r4   rx   )rK   r   r2   r3   r4   r   rI   )@
__future__r   rR   collections.abcr   r   typingr   r   prompt_toolkit.applicationr   "prompt_toolkit.application.currentr   prompt_toolkit.bufferr	   prompt_toolkit.completionr
   prompt_toolkit.eventloopr   prompt_toolkit.filtersr   prompt_toolkit.formatted_textr   )prompt_toolkit.key_binding.bindings.focusr   r   #prompt_toolkit.key_binding.defaultsr   'prompt_toolkit.key_binding.key_bindingsr   r   prompt_toolkit.layoutr    prompt_toolkit.layout.containersr   r   prompt_toolkit.layout.dimensionr   ru   prompt_toolkit.stylesr   prompt_toolkit.validationr   prompt_toolkit.widgetsr   r   r   r   r   r   r    r!   r"   __all__r#   rL   r$   r%   r&   r'   r(   r)   rJ   rt   r<   r<   r<   r=   <module>   s    ,5((
;