o
    �õ±jI8  ã                   @  sÂ  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
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 ddlmZ ddlmZmZmZ dd	l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*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= dgZ>e&Z?e@eddƒZAd&dd„ZBedƒZCG dd„ dƒZDG dd „ d e0ƒZEed!d"d#�ZFG d$d%„ d%eeF ƒZGdS )'z‹
Progress bar implementation on top of prompt_toolkit.

::

    with ProgressBar(...) as pb:
        for item in pb(data):
            ...
é    )ÚannotationsN)ÚCallableÚIterableÚIteratorÚSequenceÚSized)ÚGenericÚTextIOÚTypeVarÚcast)ÚApplication)Úget_app_session)Ú	ConditionÚis_doneÚrenderer_height_is_known)ÚAnyFormattedTextÚStyleAndTextTuplesÚto_formatted_text)ÚInput)ÚKeyBindings)ÚKeyPressEvent)ÚConditionalContainerÚFormattedTextControlÚHSplitÚLayoutÚVSplitÚWindow)Ú	UIContentÚ	UIControl)ÚAnyDimensionÚD)Ú
ColorDepthÚOutput)Ú	BaseStyle)Úin_main_threadé   )Ú	FormatterÚcreate_default_formattersÚProgressBarÚSIGWINCHÚcancel_callbackúCallable[[], None] | NoneÚreturnr   c                   s>   t ƒ }| d¡ddd„ƒ}ˆ dur| d	¡d‡ fd
d„ƒ}|S )zu
    Key bindings handled by the progress bar.
    (The main thread is not supposed to handle any key bindings.)
    zc-lÚeventÚEr,   ÚNonec                 S  s   | j j ¡  d S ©N)ÚappÚrendererÚclear©r-   © r5   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/shortcuts/progress_bar/base.pyÚ_clearE   s   z#create_key_bindings.<locals>._clearNzc-cc                   s   ˆ dusJ ‚ˆ ƒ  dS )zMKill the 'body' of the progress bar, but only if we run from the main thread.Nr5   r4   ©r*   r5   r6   Ú
_interruptK   s   
z'create_key_bindings.<locals>._interrupt)r-   r.   r,   r/   )r   Úadd)r*   Úkbr7   r9   r5   r8   r6   Úcreate_key_bindings>   s   r<   Ú_Tc                   @  sb   e Zd ZdZ										d.d/dd„Zd0dd„Zd1dd„Z		 	!	d2d3d*d+„Zd4d,d-„ZdS )5r(   a.  
    Progress bar context manager.

    Usage ::

        with ProgressBar(...) as pb:
            for item in pb(data):
                ...

    :param title: Text to be displayed above the progress bars. This can be a
        callable or formatted text as well.
    :param formatters: List of :class:`.Formatter` instances.
    :param bottom_toolbar: Text to be displayed in the bottom toolbar. This
        can be a callable or formatted text.
    :param style: :class:`prompt_toolkit.styles.BaseStyle` instance.
    :param key_bindings: :class:`.KeyBindings` instance.
    :param cancel_callback: Callback function that's called when control-c is
        pressed by the user. This can be used for instance to start "proper"
        cancellation if the wrapped code supports it.
    :param file: The file object used for rendering, by default `sys.stderr` is used.

    :param color_depth: `prompt_toolkit` `ColorDepth` instance.
    :param output: :class:`~prompt_toolkit.output.Output` instance.
    :param input: :class:`~prompt_toolkit.input.Input` instance.
    NÚtitler   Ú
formattersúSequence[Formatter] | NoneÚbottom_toolbarÚstyleúBaseStyle | NoneÚkey_bindingsúKeyBindings | Noner*   r+   ÚfileúTextIO | NoneÚcolor_depthúColorDepth | NoneÚoutputúOutput | NoneÚinputúInput | Noner,   r/   c                 C  sŒ   || _ |ptƒ | _|| _g | _|| _|| _|| _| jd u r(tƒ r(ddd„}|| _|| _	|	p0t
ƒ j| _|
p7t
ƒ j| _d | _d| _t ¡ | _d S )Nr,   r/   c                   S  s   t  t  ¡ tj¡ d S r0   )ÚosÚkillÚgetpidÚsignalÚSIGINTr5   r5   r5   r6   Ú!keyboard_interrupt_to_main_threadŒ   s   z?ProgressBar.__init__.<locals>.keyboard_interrupt_to_main_threadF©r,   r/   )r>   r'   r?   rA   ÚcountersrB   rD   r*   r$   rH   r   rJ   rL   Ú_threadÚ_has_sigwinchÚ	threadingÚEventÚ_app_started)Úselfr>   r?   rA   rB   rD   r*   rF   rH   rJ   rL   rS   r5   r5   r6   Ú__init__r   s    
zProgressBar.__init__c              
     s  t tt‡ fdd„ƒddd�t‡ fdd„ƒd�}t tt‡ fdd„d	d
�ddd�t t@ t‡ fdd„ƒ@ d�}d‡ fdd„‰‡ ‡fdd„ˆ jD ƒ}tdtt	|t
|‡ fdd„d�tƒ |gƒƒˆ jˆ jdˆ jˆ jˆ jd�ˆ _d ‡ fdd„}t ¡ }tj|j|fd�ˆ _ˆ j ¡  ˆ S )!Nc                     ó   ˆ j S r0   ©r>   r5   ©r[   r5   r6   Ú<lambda>    ó    z'ProgressBar.__enter__.<locals>.<lambda>r%   zclass:progressbar,title)ÚheightrB   c                     ó
   ˆ j d uS r0   r^   r5   r_   r5   r6   r`   ¤   ó   
 )Úfilterc                     r]   r0   ©rA   r5   r_   r5   r6   r`   ª   ra   zclass:bottom-toolbar.text)rB   zclass:bottom-toolbar)rB   rb   c                     rc   r0   rf   r5   r_   r5   r6   r`   ±   rd   Ú	formatterr&   r,   r   c                   s   | j ˆ d�S )N)Úprogress_bar)Ú	get_width)rg   r_   r5   r6   Úwidth_for_formatter´   s   z2ProgressBar.__enter__.<locals>.width_for_formatterc                   s*   g | ]}t tˆ |ˆ jƒt ˆ|¡d �‘qS ))ÚcontentÚwidth)r   Ú_ProgressControlr*   Ú	functoolsÚpartial)Ú.0Úf©r[   rj   r5   r6   Ú
<listcomp>¹   s    ü
þÿz)ProgressBar.__enter__.<locals>.<listcomp>gš™™™™™©?c                     s   t tˆ jƒtˆ jƒd�S )N)Ú	preferredÚmax)r    ÚlenrU   r5   r_   r5   r6   r`   É   s    ÿ)rb   g333333Ó?)Úmin_redraw_intervalÚlayoutrB   rD   Úrefresh_intervalrH   rJ   rL   r/   c               
     sP   zˆ j jˆ jjd� W d S  ty' }  zt ¡  t| ƒ W Y d } ~ d S d } ~ ww )N)Úpre_run)r1   ÚrunrZ   ÚsetÚBaseExceptionÚ	tracebackÚ	print_excÚprint)Úer_   r5   r6   r{   Û   s   €þz"ProgressBar.__enter__.<locals>.run)ÚtargetÚargs)rg   r&   r,   r   rT   )r   r   r   r   r   r   r?   r   r   r   r   rB   rD   rH   rJ   rL   r1   ÚcontextvarsÚcopy_contextrX   ÚThreadr{   rV   Ústart)r[   Útitle_toolbarrA   Úprogress_controlsr{   Úctxr5   rr   r6   Ú	__enter__œ   sh   ýú	ÿûÿþøû
þ÷ÿÿê
zProgressBar.__enter__ÚaÚobjectc                 G  sL   | j  ¡  | jjr| jjd ur| jj | jj¡ | jd ur$| j ¡  d S d S r0   )	rZ   Úwaitr1   Ú
is_runningÚloopÚcall_soon_threadsafeÚexitrV   Újoin)r[   rŒ   r5   r5   r6   Ú__exit__é   s   

ÿzProgressBar.__exit__Ú FÚdataúIterable[_T] | NoneÚlabelÚremove_when_doneÚboolÚtotalú
int | NoneúProgressBarCounter[_T]c                 C  s"   t | ||||d�}| j |¡ |S )aL  
        Start a new counter.

        :param label: Title text or description for this progress. (This can be
            formatted text as well).
        :param remove_when_done: When `True`, hide this progress bar.
        :param total: Specify the maximum value if it can't be calculated by
            calling ``len``.
        )r˜   r™   r›   )ÚProgressBarCounterrU   Úappend)r[   r–   r˜   r™   r›   Úcounterr5   r5   r6   Ú__call__ö   s
   
ÿzProgressBar.__call__c                 C  s   | j  ¡  d S r0   )r1   Ú
invalidater_   r5   r5   r6   r¢     s   zProgressBar.invalidate)
NNNNNNNNNN)r>   r   r?   r@   rA   r   rB   rC   rD   rE   r*   r+   rF   rG   rH   rI   rJ   rK   rL   rM   r,   r/   )r,   r(   )rŒ   r�   r,   r/   ©Nr•   FN)
r–   r—   r˜   r   r™   rš   r›   rœ   r,   r�   rT   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r\   r‹   r”   r¡   r¢   r5   r5   r5   r6   r(   W   s*    õ
*
Mûc                   @  s8   e Zd ZdZdd
d„Zddd„Zddd„Zddd„ZdS )rm   z,
    User control for the progress bar.
    rh   r(   rg   r&   r*   r+   r,   r/   c                 C  s   || _ || _t|ƒ| _d S r0   )rh   rg   r<   Ú_key_bindings)r[   rh   rg   r*   r5   r5   r6   r\     s   z_ProgressControl.__init__rl   Úintrb   r   c              	     sv   g ‰ | j jD ]$}z| j | j ||¡}W n ty"   t ¡  d}Y nw ˆ  t|ƒ¡ qd
‡ fdd„}t	|t
ˆ ƒdd	�S )NÚERRORÚir©   r,   r   c                   s   ˆ |  S r0   r5   )r«   ©Úitemsr5   r6   Úget_line+  s   z1_ProgressControl.create_content.<locals>.get_lineF)r®   Ú
line_countÚshow_cursor)r«   r©   r,   r   )rh   rU   rg   Úformatr}   r~   r   rŸ   r   r   rv   )r[   rl   rb   ÚprÚtextr®   r5   r¬   r6   Úcreate_content  s   þz_ProgressControl.create_contentrš   c                 C  s   dS )NTr5   r_   r5   r5   r6   Úis_focusable0  s   z_ProgressControl.is_focusabler   c                 C  ó   | j S r0   )r¨   r_   r5   r5   r6   Úget_key_bindings3  s   z!_ProgressControl.get_key_bindingsN)rh   r(   rg   r&   r*   r+   r,   r/   )rl   r©   rb   r©   r,   r   ©r,   rš   )r,   r   )r¤   r¥   r¦   r§   r\   r´   rµ   r·   r5   r5   r5   r6   rm     s    



rm   Ú_CounterItemT)Ú	covariantc                   @  sž   e Zd ZdZ				d(d)dd„Zd*dd„Zd+dd„Zed,dd„ƒZej	d-dd„ƒZed,dd„ƒZ
e
j	d-dd„ƒZ
ed.d d!„ƒZed/d#d$„ƒZed0d&d'„ƒZdS )1rž   zL
    An individual counter (A progress bar can have multiple counters).
    Nr•   Frh   r(   r–   úIterable[_CounterItem] | Noner˜   r   r™   rš   r›   rœ   r,   r/   c                 C  s~   t j  ¡ | _d | _|| _|| _d| _|| _|| _d| _	|  |d u r:zt
tt|ƒƒ| _W d S  ty9   d | _Y d S w || _d S )Nr   F)ÚdatetimeÚnowÚ
start_timeÚ	stop_timerh   r–   Úitems_completedr˜   r™   Ú_donerv   r   r   r›   Ú	TypeError)r[   rh   r–   r˜   r™   r›   r5   r5   r6   r\   ?  s    ÿ
zProgressBarCounter.__init__úIterator[_CounterItem]c                 c  sJ   � | j d ur!z| j D ]	}|V  |  ¡  q
d| _W d| _d S d| _w tdƒ‚)NTz No data defined to iterate over.)r–   Úitem_completedÚdoneÚstoppedÚNotImplementedError)r[   Úitemr5   r5   r6   Ú__iter__Y  s   €


zProgressBarCounter.__iter__c                 C  s   |  j d7  _ | j ¡  dS )z…
        Start handling the next item.

        (Can be called manually in case we don't have a collection to loop through.)
        r%   N)rÀ   rh   r¢   r_   r5   r5   r6   rÄ   i  s   z!ProgressBarCounter.item_completedc                 C  r¶   )a6  Whether a counter has been completed.

        Done counter have been stopped (see stopped) and removed depending on
        remove_when_done value.

        Contrast this with stopped. A stopped counter may be terminated before
        100% completion. A done counter has reached its 100% completion.
        )rÁ   r_   r5   r5   r6   rÅ   r  s   
zProgressBarCounter.doneÚvaluec                 C  s0   || _ || _|r| jr| jj | ¡ d S d S d S r0   )rÁ   rÆ   r™   rh   rU   Úremove©r[   rÊ   r5   r5   r6   rÅ   ~  s
   
ÿc                 C  s
   | j duS )aÉ  Whether a counter has been stopped.

        Stopped counters no longer have increasing time_elapsed. This distinction is
        also used to prevent the Bar formatter with unknown totals from continuing to run.

        A stopped counter (but not done) can be used to signal that a given counter has
        encountered an error but allows other counters to continue
        (e.g. download X of Y failed). Given how only done counters are removed
        (see remove_when_done) this can help aggregate failures from a large number of
        successes.

        Contrast this with done. A done counter has reached its 100% completion.
        A stopped counter may be terminated before 100% completion.
        N)r¿   r_   r5   r5   r6   rÆ   †  s   
zProgressBarCounter.stoppedc                 C  s(   |r| j stj ¡ | _ d S d S d | _ d S r0   )r¿   r¼   r½   rÌ   r5   r5   r6   rÆ   ˜  s
   ÿ
Úfloatc                 C  s$   | j d u rdS | jd t| j dƒ S )Nr   éd   r%   )r›   rÀ   ru   r_   r5   r5   r6   Ú
percentage¢  s   
zProgressBarCounter.percentageúdatetime.timedeltac                 C  s&   | j du rtj ¡ | j S | j | j S )zH
        Return how much time has been elapsed since the start.
        N)r¿   r¼   r½   r¾   r_   r5   r5   r6   Útime_elapsed©  s   
zProgressBarCounter.time_elapsedúdatetime.timedelta | Nonec                 C  s@   | j du s| js
dS | js| jrt d¡S | jd| j  | j S )z7
        Timedelta representing the time left.
        Nr   rÎ   )r›   rÏ   rÅ   rÆ   r¼   Ú	timedeltarÑ   r_   r5   r5   r6   Ú	time_left³  s
   
zProgressBarCounter.time_leftr£   )rh   r(   r–   r»   r˜   r   r™   rš   r›   rœ   r,   r/   )r,   rÃ   rT   r¸   )rÊ   rš   r,   r/   )r,   rÍ   )r,   rÐ   )r,   rÒ   )r¤   r¥   r¦   r§   r\   rÉ   rÄ   ÚpropertyrÅ   ÚsetterrÆ   rÏ   rÑ   rÔ   r5   r5   r5   r6   rž   :  s.    ú

			rž   )r*   r+   r,   r   )Hr§   Ú
__future__r   r„   r¼   rn   rN   rQ   rX   r~   Úcollections.abcr   r   r   r   r   Útypingr   r	   r
   r   Úprompt_toolkit.applicationr   Ú"prompt_toolkit.application.currentr   Úprompt_toolkit.filtersr   r   r   Úprompt_toolkit.formatted_textr   r   r   Úprompt_toolkit.inputr   Úprompt_toolkit.key_bindingr   Ú(prompt_toolkit.key_binding.key_processorr   Úprompt_toolkit.layoutr   r   r   r   r   r   Úprompt_toolkit.layout.controlsr   r   Úprompt_toolkit.layout.dimensionr   r    Úprompt_toolkit.outputr!   r"   Úprompt_toolkit.stylesr#   Úprompt_toolkit.utilsr$   r?   r&   r'   Ú__all__r.   ÚgetattrÚ	_SIGWINCHr<   r=   r(   rm   r¹   rž   r5   r5   r5   r6   Ú<module>   sF    
 
 :'