a
    [fV                     @   s$  d dl mZ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 d dlmZmZ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mZm Z m!Z!m"Z" d
dl#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/m0Z0 d
dl1m2Z2 d
dl3m4Z4 d
dl5m6Z6 d
dl7m8Z8m9Z9 d
dl:m;Z;m<Z< ede=Z>e!dZ?eg e@f ZAG dd deZBdie"ee? ee? f eCee@ eDee' eDeeg e@f  e@e6e6e6e6e@eDee? d#d$d%ZEG d&d' d'eZFG d(d) d)eFZGG d*d+ d+eFZHG d,d- d-eFZIG d.d/ d/eFZJG d0d1 d1eFZKG d2d3 d3eFZLG d4d5 d5eFZMG d6d7 d7eFZNG d8d9 d9eFZOG d:d; d;eFZPG d<d= d=eZQeG d>d? d?ZRG d@dA dAe.ZSeTdBkr d dlUZUd dlVZVd
dClWmXZX d
dDlYmZZZ d
dEl[m\Z\ d
dFl7m9Z9 e\dGdHddIZ[e9dJdKdLZ7e7]dMdNdO dPeXdQdRe7dSdTdUdVdWe[eZdXg	Z^d dYl_m`Z` e`e^Zae'ddZZ&eSeH eId[eJ eId\eL eK e&dd]Zbebjcd^d_d`Zdebjcdad_d`Zeebjcdbd_ddcZfebjgs ebjhedddde ebjheedfde eVidg eUjd dhd
k rebkelea qW d   n1 s0    Y  dS )j    )ABCabstractmethod)deque)Sized)	dataclassfield)	timedelta)ceil)EventRLockThread)TracebackType)AnyCallableDequeDictIterableList
NamedTupleNewTypeOptionalSequenceTupleTypeTypeVarUnion   )filesizeget_console)ConsoleJustifyMethodRenderableTypeGroup)Highlighter)JupyterMixin)Live)ProgressBar)Spinner)	StyleType)ColumnTable)TextTextTypeTaskIDProgressTypec                       sj   e Zd ZdZdded fddZddd	d
Zd dddZee	e
  ee
 ee ddddZ  ZS )_TrackThreadz)A thread to periodically update progress.Progressr-   )progresstask_idupdate_periodc                    s.   || _ || _|| _t | _d| _t   d S )Nr   )r1   r2   r3   r
   done	completedsuper__init__)selfr1   r2   r3   	__class__ kC:\Users\91910\OneDrive\Desktop\vts_admin_rebuild-master\vts\Lib\site-packages\pip/_vendor/rich/progress.pyr7   /   s    z_TrackThread.__init__Nreturnc                 C   sd   | j }| jj}| j}d}| jj}||sJ| j}||kr ||||  |}q | jj| j | jdd d S )Nr   T)r5   refresh)r2   r1   advancer3   r4   waitr5   update)r8   r2   r@   r3   last_completedrA   r5   r;   r;   r<   run8   s    z_TrackThread.runc                 C   s   |    | S Nstartr8   r;   r;   r<   	__enter__F   s    z_TrackThread.__enter__exc_typeexc_valexc_tbr>   c                 C   s   | j   |   d S rE   )r4   setjoinr8   rK   rL   rM   r;   r;   r<   __exit__J   s    
z_TrackThread.__exit__)__name__
__module____qualname____doc__floatr7   rD   rI   r   r   BaseExceptionr   rQ   __classcell__r;   r;   r9   r<   r/   ,   s   	
r/   
Working...NTF
   bar.backbar.completebar.finished	bar.pulse皙?)sequencedescriptiontotalauto_refreshconsole	transientget_timerefresh_per_secondstylecomplete_stylefinished_stylepulse_styler3   disabler>   c              	   c   s   |rt dgng }|t||	|
|dt dt f t||||||pFd|d}|( |j| |||dE dH  W d   n1 s0    Y  dS )a  Track progress by iterating over a sequence.

    Args:
        sequence (Iterable[ProgressType]): A sequence (must support "len") you wish to iterate over.
        description (str, optional): Description of task show next to progress bar. Defaults to "Working".
        total: (float, optional): Total number of steps. Default is len(sequence).
        auto_refresh (bool, optional): Automatic refresh, disable to force a refresh after each iteration. Default is True.
        transient: (bool, optional): Clear the progress on exit. Defaults to False.
        console (Console, optional): Console to write to. Default creates internal Console instance.
        refresh_per_second (float): Number of times per second to refresh the progress information. Defaults to 10.
        style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
        complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
        finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.done".
        pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
        update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1.
        disable (bool, optional): Disable display of progress.
    Returns:
        Iterable[ProgressType]: An iterable of the values in the sequence.

    ([progress.description]{task.description})rh   ri   rj   rk   -[progress.percentage]{task.percentage:>3.0f}%rZ   )rc   rd   re   rf   rg   rl   )rb   ra   r3   N)
TextColumnextend	BarColumnTimeRemainingColumnr0   track)r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   r3   rl   columnsr1   r;   r;   r<   rs   T   s4    &
rs   c                   @   sj   e Zd ZU dZdZee ed< dee ddddZ	eddd	Z
d
edddZed
edddZdS )ProgressColumnz3Base class for a widget to use in progress display.Nmax_refresh)table_columnr>   c                 C   s   || _ i | _d | _d S rE   )_table_column_renderable_cache_update_time)r8   rw   r;   r;   r<   r7      s    zProgressColumn.__init__r=   c                 C   s   | j p
t S )z.Get a table column, used to build tasks table.)rx   r)   rH   r;   r;   r<   get_table_column   s    zProgressColumn.get_table_columnTasktaskr>   c                 C   sp   |  }| jdurR|jsRz| j|j \}}W n ty>   Y n0 || j |krR|S | |}||f| j|j< |S )a  Called by the Progress object to return a renderable for the given task.

        Args:
            task (Task): An object containing information regarding the task.

        Returns:
            RenderableType: Anything renderable (including str).
        N)rf   rv   r5   ry   idKeyErrorrender)r8   r~   current_time	timestamp
renderabler;   r;   r<   __call__   s    	
zProgressColumn.__call__c                 C   s   dS )z"Should return a renderable object.Nr;   r8   r~   r;   r;   r<   r      s    zProgressColumn.render)N)rR   rS   rT   rU   rv   r   rV   __annotations__r)   r7   r{   r!   r   r   r   r;   r;   r;   r<   ru      s   
ru   c                       sD   e Zd ZdZdddeee d fddZded	d
dZ  Z	S )RenderableColumnzA column to insert an arbitrary column.

    Args:
        renderable (RenderableType, optional): Any renderable. Defaults to empty string.
     Nrw   )r   rw   c                   s   || _ t j|d d S Nr   )r   r6   r7   )r8   r   rw   r9   r;   r<   r7      s    zRenderableColumn.__init__r|   r}   c                 C   s   | j S rE   )r   r   r;   r;   r<   r      s    zRenderableColumn.render)r   )
rR   rS   rT   rU   r!   r   r)   r7   r   rX   r;   r;   r9   r<   r      s    r   c                       sb   e Zd ZdZdeee eeee	 d fdd	Z
deee edd
ddZdedddZ  ZS )SpinnerColumna  A column with a 'spinner' animation.

    Args:
        spinner_name (str, optional): Name of spinner animation. Defaults to "dots".
        style (StyleType, optional): Style of spinner. Defaults to "progress.spinner".
        speed (float, optional): Speed factor of spinner. Defaults to 1.0.
        finished_text (TextType, optional): Text used when task is finished. Defaults to " ".
    dotsprogress.spinner      ? N)spinner_namerh   speedfinished_textrw   c                    s<   t |||d| _t|tr$t|n|| _t j|d d S )Nrh   r   r   )	r'   spinner
isinstancestrr+   from_markupr   r6   r7   )r8   r   rh   r   r   rw   r9   r;   r<   r7      s    zSpinnerColumn.__init__)r   spinner_styler   r>   c                 C   s   t |||d| _dS )a-  Set a new spinner.

        Args:
            spinner_name (str): Spinner name, see python -m rich.spinner.
            spinner_style (Optional[StyleType], optional): Spinner style. Defaults to "progress.spinner".
            speed (float, optional): Speed factor of spinner. Defaults to 1.0.
        r   N)r'   r   )r8   r   r   r   r;   r;   r<   set_spinner   s    zSpinnerColumn.set_spinnerr|   r}   c                 C   s    |j r| jn| j| }|S rE   )finishedr   r   r   rf   )r8   r~   textr;   r;   r<   r      s
    zSpinnerColumn.render)r   r   r   r   N)r   r   )rR   rS   rT   rU   r   r   r(   rV   r,   r)   r7   r   r!   r   rX   r;   r;   r9   r<   r      s,          r   c                	       sL   e Zd ZdZdeeeeee	 ee
 dd fddZd	ed
ddZ  ZS )ro   zA column containing text.noneleftTN)text_formatrh   justifymarkuphighlighterrw   r>   c                    s:   || _ || _|| _|| _|| _t j|p0tddd d S )NTno_wrapr   )r   r   rh   r   r   r6   r7   r)   )r8   r   rh   r   r   r   rw   r9   r;   r<   r7     s    	zTextColumn.__init__r|   r}   c                 C   sR   | j j|d}| jr*tj|| j| jd}nt|| j| jd}| jrN| j| |S )Nr~   )rh   r   )	r   formatr   r+   r   rh   r   r   	highlight)r8   r~   _textr   r;   r;   r<   r     s    zTextColumn.render)r   r   TNN)rR   rS   rT   rU   r   r(   r    boolr   r#   r)   r7   r+   r   rX   r;   r;   r9   r<   ro     s         ro   c                	       sL   e Zd ZdZdee eeeeee dd fd	d
Zde	dddZ
  ZS )rq   a  Renders a visual progress bar.

    Args:
        bar_width (Optional[int], optional): Width of bar or None for full width. Defaults to 40.
        style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
        complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
        finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.done".
        pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
    (   r[   r\   r]   r^   N)	bar_widthrh   ri   rj   rk   rw   r>   c                    s0   || _ || _|| _|| _|| _t j|d d S r   )r   rh   ri   rj   rk   r6   r7   )r8   r   rh   ri   rj   rk   rw   r9   r;   r<   r7   .  s    	zBarColumn.__init__r|   r}   c                 C   sP   t td|jtd|j| jdu r$dn
td| j|j | | j| j| j	| j
d	S )z&Gets a progress bar widget for a task.r   Nr   )	rb   r5   widthpulseanimation_timerh   ri   rj   rk   )r&   maxrb   r5   r   startedrf   rh   ri   rj   rk   r   r;   r;   r<   r   >  s    

zBarColumn.render)r   r[   r\   r]   r^   N)rR   rS   rT   rU   r   intr(   r)   r7   r&   r   rX   r;   r;   r9   r<   rq   #  s"         rq   c                   @   s    e Zd ZdZdedddZdS )TimeElapsedColumnzRenders time elapsed.r|   r}   c                 C   sD   |j r|jn|j}|du r&tdddS tt|d}tt|ddS )Show time remaining.N-:--:--zprogress.elapsedrh   seconds)r   finished_timeelapsedr+   r   r   r   )r8   r~   r   deltar;   r;   r<   r   P  s
    zTimeElapsedColumn.renderNrR   rS   rT   rU   r+   r   r;   r;   r;   r<   r   M  s   r   c                   @   s$   e Zd ZdZdZdedddZdS )rr   z!Renders estimated time remaining.      ?r|   r}   c                 C   s8   |j }|du rtdddS tt|d}tt|ddS )r   Nr   zprogress.remainingr   r   )time_remainingr+   r   r   r   )r8   r~   	remainingremaining_deltar;   r;   r<   r   _  s
    zTimeRemainingColumn.renderN)rR   rS   rT   rU   rv   r+   r   r;   r;   r;   r<   rr   Y  s   rr   c                   @   s    e Zd ZdZdedddZdS )FileSizeColumnzRenders completed filesize.r|   r}   c                 C   s   t t|j}t|ddS )Show data completed.zprogress.filesizer   )r   decimalr   r5   r+   r8   r~   	data_sizer;   r;   r<   r   k  s    zFileSizeColumn.renderNr   r;   r;   r;   r<   r   h  s   r   c                   @   s    e Zd ZdZdedddZdS )TotalFileSizeColumnzRenders total filesize.r|   r}   c                 C   s   t t|j}t|ddS )r   zprogress.filesize.totalr   )r   r   r   rb   r+   r   r;   r;   r<   r   t  s    zTotalFileSizeColumn.renderNr   r;   r;   r;   r<   r   q  s   r   c                       s@   e Zd ZdZdeee dd fddZdedd	d
Z	  Z
S )DownloadColumnzRenders file size downloaded and total, e.g. '0.5/2.3 GB'.

    Args:
        binary_units (bool, optional): Use binary units, KiB, MiB etc. Defaults to False.
    FN)binary_unitsrw   r>   c                    s   || _ t j|d d S r   )r   r6   r7   )r8   r   rw   r9   r;   r<   r7     s    zDownloadColumn.__init__r|   r}   c                 C   s   t |j}t |j}| jr2t|g dd\}}nt|g dd\}}|| }|| }|dkrddnd}|d| d}	|d| d}
|	 d	|
 d
| }t|dd}|S )z.Calculate common unit for completed and total.)	bytesKiBMiBGiBTiBPiBEiBZiBYiBi   )	r   KBMBGBTBPBEBZBYB  r   r   z,.f/r   zprogress.downloadr   )r   r5   rb   r   r   pick_unit_and_suffixr+   )r8   r~   r5   rb   unitsuffixcompleted_ratiototal_ratio	precisioncompleted_str	total_strdownload_statusdownload_textr;   r;   r<   r     s&    



zDownloadColumn.render)FN)rR   rS   rT   rU   r   r   r)   r7   r+   r   rX   r;   r;   r9   r<   r   z  s    r   c                   @   s    e Zd ZdZdedddZdS )TransferSpeedColumnz&Renders human readable transfer speed.r|   r}   c                 C   s@   |j p
|j}|du r tdddS tt|}t| dddS )zShow data transfer speed.N?zprogress.data.speedr   z/s)finished_speedr   r+   r   r   r   )r8   r~   r   
data_speedr;   r;   r<   r     s
    zTransferSpeedColumn.renderNr   r;   r;   r;   r<   r     s   r   c                   @   s"   e Zd ZU dZeed< eed< dS )ProgressSamplez$Sample of progress for a given time.r   r5   N)rR   rS   rT   rU   rV   r   r;   r;   r;   r<   r     s   
r   c                   @   s  e Zd ZU dZeed< eed< eed< eed< eed< dZ	e
e ed< d	Zeed
< eedZeeef ed< eddddZe
e ed< eddddZe
e ed< dZe
e ed< eedddZee ed< ededZeed< edddZeedddZeedddZee
e dddZeeddd Z eedd!d"Z!ee
e dd#d$Z"ee
e dd%d&Z#ddd'd(Z$dS ))r|   zInformation regarding a progress task.

    This object should be considered read-only outside of the :class:`~Progress` class.

    r   ra   rb   r5   	_get_timeNr   Tvisible)default_factoryfieldsF)defaultinitrepr
start_time	stop_timer   )r   r   r   	_progress)r   r   _lockr=   c                 C   s   |   S )z(float: Get the current time, in seconds.)r   rH   r;   r;   r<   rf     s    zTask.get_timec                 C   s
   | j duS )z#bool: Check if the task as started.N)r   rH   r;   r;   r<   r     s    zTask.startedc                 C   s   | j | j S )z)float: Get the number of steps remaining.)rb   r5   rH   r;   r;   r<   r     s    zTask.remainingc                 C   s2   | j du rdS | jdur$| j| j  S |  | j  S )z]Optional[float]: Time elapsed since task was started, or ``None`` if the task hasn't started.N)r   r   rf   rH   r;   r;   r<   r     s
    

zTask.elapsedc                 C   s
   | j duS )zCheck if the task has finished.N)r   rH   r;   r;   r<   r     s    zTask.finishedc                 C   s.   | j s
dS | j| j  d }tdtd|}|S )z,float: Get progress of task as a percentage.              Y@)rb   r5   minr   )r8   r5   r;   r;   r<   
percentage   s
    zTask.percentagec                 C   s   | j du rdS | j | j}|s0W d   dS |d j|d j }|dkr\W d   dS t|}t| tdd |D }|| }|W  d   S 1 s0    Y  dS )z=Optional[float]: Get the estimated speed in steps per second.Nr   c                 s   s   | ]}|j V  qd S rE   )r5   ).0sampler;   r;   r<   	<genexpr>      zTask.speed.<locals>.<genexpr>)r   r   r   r   iternextsum)r8   r1   
total_timeiter_progresstotal_completedr   r;   r;   r<   r   	  s    
z
Task.speedc                 C   s*   | j r
dS | j}|sdS t| j| }|S )zJOptional[float]: Get estimated time to completion, or ``None`` if no data.r   N)r   r   r	   r   )r8   r   estimater;   r;   r<   r     s    zTask.time_remainingc                 C   s   | j   d| _d| _dS )zReset progress.N)r   clearr   r   rH   r;   r;   r<   _reset&  s    
zTask._reset)%rR   rS   rT   rU   r-   r   r   rV   GetTimeCallabler   r   r   r   r   dictr   r   r   r   r   r   r   r   r   r   r   r   rf   propertyr   r   r   r   r   r   r   r  r;   r;   r;   r<   r|     s@   

r|   c                   @   sX  e Zd ZdZddddddddddd
eeef ee e	e
e
e	e	e	ee e	e	ddd	d
ZeedddZeee dddZeee dddZee	dddZddddZddddZd dddZeee  ee ee ddddZdEeee ee f ee
 ee ee
ee dd d!Zedd"d#d$Z edd"d%d&Z!ddddddd'eee
 ee
 ee
 ee ee	 e	e"dd(	d)d*Z#ddd+ddd,ee	ee
 e$ee	 ee e"dd-d.d/Z%dFee
dd1d2d3Z&ddd4d5Z'e(dd6d7Z)ee( dd8d9Z*ee e+d:d;d<Z,e(dd=d>Z-dGee	e
e$e	e"ed@dAdBZ.edd"dCdDZ/dS )Hr0   a  Renders an auto-updating progress bar(s).

    Args:
        console (Console, optional): Optional Console instance. Default will an internal Console instance writing to stdout.
        auto_refresh (bool, optional): Enable auto refresh. If disabled, you will need to call `refresh()`.
        refresh_per_second (Optional[float], optional): Number of times per second to refresh the progress information or None to use default (10). Defaults to None.
        speed_estimate_period: (float, optional): Period (in seconds) used to calculate the speed estimate. Defaults to 30.
        transient: (bool, optional): Clear the progress on exit. Defaults to False.
        redirect_stdout: (bool, optional): Enable redirection of stdout, so ``print`` may be used. Defaults to True.
        redirect_stderr: (bool, optional): Enable redirection of stderr. Defaults to True.
        get_time: (Callable, optional): A callable that gets the current time, or None to use Console.get_time. Defaults to None.
        disable (bool, optional): Disable progress display. Defaults to False
        expand (bool, optional): Expand tasks table to fit width. Defaults to False.
    NTrZ   g      >@F)
rd   rc   rg   speed_estimate_periodre   redirect_stdoutredirect_stderrrf   rl   expand)rt   rd   rc   rg   r  re   r  r  rf   rl   r  r>   c       
      	   G   s   |d u s|dksJ dt  | _|p:tdt tdt f| _|| _|	| _|
| _i | _	t
d| _t|pjt |||||| jd| _|p| jj| _| jj| _| jj| _d S )Nr   zrefresh_per_second must be > 0rm   rn   )rd   rc   rg   re   r  r  get_renderable)r   r   ro   rq   rr   rt   r  rl   r  _tasksr-   _task_indexr%   r   r  liverd   rf   printlog)r8   rd   rc   rg   r  re   r  r  rf   rl   r  rt   r;   r;   r<   r7   =  s<    
	
zProgress.__init__r=   c                 C   s   | j jS rE   )r  rd   rH   r;   r;   r<   rd   h  s    zProgress.consolec                 C   s8   | j  t| j W  d   S 1 s*0    Y  dS )zGet a list of Task instances.N)r   listr  valuesrH   r;   r;   r<   tasksl  s    zProgress.tasksc                 C   s8   | j  t| j W  d   S 1 s*0    Y  dS )zA list of task IDs.N)r   r  r  keysrH   r;   r;   r<   task_idsr  s    zProgress.task_idsc                 C   sX   | j > | jsW d   dS tdd | j D W  d   S 1 sJ0    Y  dS )z'Check if all tasks have been completed.NTc                 s   s   | ]}|j V  qd S rE   )r   )r   r~   r;   r;   r<   r   ~  r   z$Progress.finished.<locals>.<genexpr>)r   r  allr  rH   r;   r;   r<   r   x  s    zProgress.finishedc                 C   s   | j s| jjdd dS )zStart the progress display.T)r?   N)rl   r  rG   rH   r;   r;   r<   rG     s    zProgress.startc                 C   s    | j   | jjs| j  dS )zStop the progress display.N)r  stoprd   is_interactiver  rH   r;   r;   r<   r    s    
zProgress.stopc                 C   s   |    | S rE   rF   rH   r;   r;   r<   rI     s    zProgress.__enter__rJ   c                 C   s   |    d S rE   )r  rP   r;   r;   r<   rQ     s    zProgress.__exit__rY   r_   )r`   rb   r2   ra   r3   r>   c                 c   s   |du r2t |tr tt|}q6td|dn|}|du rN| j||d}n| j||d | jjrt	| ||.}|D ]}|V  | j
d7  _
qvW d   q1 s0    Y  n,| j}	| j}
|D ]}|V  |	|d |
  qdS )a  Track progress by iterating over a sequence.

        Args:
            sequence (Sequence[ProgressType]): A sequence of values you want to iterate over and track progress.
            total: (float, optional): Total number of steps. Default is len(sequence).
            task_id: (TaskID): Task to track. Default is new task.
            description: (str, optional): Description of task, if new task is created.
            update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1.

        Returns:
            Iterable[ProgressType]: An iterable of values taken from the provided sequence.
        Nzunable to get size of z, please specify 'total'rb   r   )r   r   rV   len
ValueErroradd_taskrB   r  rc   r/   r5   r@   r?   )r8   r`   rb   r2   ra   r3   
task_totaltrack_threadvaluer@   r?   r;   r;   r<   rs     s*    

0
zProgress.track)r2   r>   c                 C   sH   | j . | j| }|jdu r&|  |_W d   n1 s:0    Y  dS )zStart a task.

        Starts a task (used when calculating elapsed time). You may need to call this manually,
        if you called ``add_task`` with ``start=False``.

        Args:
            task_id (TaskID): ID of task.
        N)r   r  r   rf   )r8   r2   r~   r;   r;   r<   
start_task  s    	

zProgress.start_taskc                 C   sR   | j 8 | j| }|  }|jdu r*||_||_W d   n1 sD0    Y  dS )zStop a task.

        This will freeze the elapsed time on the task.

        Args:
            task_id (TaskID): ID of task.
        N)r   r  rf   r   r   )r8   r2   r~   r   r;   r;   r<   	stop_task  s    

zProgress.stop_task)rb   r5   r@   ra   r   r?   )	r2   rb   r5   r@   ra   r   r?   r   r>   c                K   sH  | j  | j| }	|	j}
|dur:||	jkr:||	_|	  |durP|	 j|7  _|dur^||	_|durl||	_|durz||	_|	j| |	j|
 }| 	 }|| j
 }|	j}|j}|r|d j|k r|  qt|dkr|  q|dkr|t|| |	j|	jkr|	jdu r|	j|	_W d   n1 s,0    Y  |rD|   dS )a  Update information associated with a task.

        Args:
            task_id (TaskID): Task id (returned by add_task).
            total (float, optional): Updates task.total if not None.
            completed (float, optional): Updates task.completed if not None.
            advance (float, optional): Add a value to task.completed if not None.
            description (str, optional): Change task description if not None.
            visible (bool, optional): Set visible flag if not None.
            refresh (bool): Force a refresh of progress information. Default is False.
            **fields (Any): Additional data fields required for rendering.
        Nr   r   )r   r  r5   rb   r  ra   r   r   rB   rf   r  r   popleftr   r  appendr   r   r   r?   )r8   r2   rb   r5   r@   ra   r   r?   r   r~   completed_startupdate_completedr   old_sample_timer   r&  r;   r;   r<   rB     s<    



(zProgress.updater   )rG   rb   r5   r   ra   )r2   rG   rb   r5   r   ra   r   r>   c          
      K   s   |   }| jp | j| }	|	  |r*|nd|	_|dur>||	_||	_|durR||	_|r\||	_|durj||	_	d|	_
W d   n1 s0    Y  |   dS )a  Reset a task so completed is 0 and the clock is reset.

        Args:
            task_id (TaskID): ID of task.
            start (bool, optional): Start the task after reset. Defaults to True.
            total (float, optional): New total steps in task, or None to use current total. Defaults to None.
            completed (int, optional): Number of steps completed. Defaults to 0.
            **fields (str): Additional data fields required for rendering.
        N)rf   r   r  r  r   rb   r5   r   r   ra   r   r?   )
r8   r2   rG   rb   r5   r   ra   r   r   r~   r;   r;   r<   reset   s     
$zProgress.resetr   )r2   r@   r>   c           
      C   s   |   }| j | j| }|j}| j|7  _|j| }|| j }|j}|j}	|rh|d j|k rh|	  qNt|dkr||	  qh|	t
|| |j|jkr|jdu r|j|_|j|_W d   n1 s0    Y  dS )zAdvance task by a number of steps.

        Args:
            task_id (TaskID): ID of task.
            advance (float): Number of steps to advance. Default is 1.
        r   r   N)rf   r   r  r5   r  r   r&  r   r  r'  r   rb   r   r   r   r   )
r8   r2   r@   r   r~   r(  r)  r*  r   r&  r;   r;   r<   r@   E  s"    


zProgress.advancec                 C   s   | j s| jjr| j  dS )z*Refresh (render) the progress information.N)rl   r  
is_startedr?   rH   r;   r;   r<   r?   _  s    zProgress.refreshc                 C   s   t |   }|S )z*Get a renderable for the progress display.)r"   get_renderables)r8   r   r;   r;   r<   r  d  s    zProgress.get_renderablec                 c   s   |  | j}|V  dS )z5Get a number of renderables for the progress display.N)make_tasks_tabler  )r8   tabler;   r;   r<   r-  i  s    zProgress.get_renderables)r  r>   c                    sR   dd | j D }tj|d| jd}|D ]$  jr(|j fdd| j D   q(|S )zGet a table to render the Progress display.

        Args:
            tasks (Iterable[Task]): An iterable of Task instances, one per row of the table.

        Returns:
            Table: A table instance.
        c                 s   s.   | ]&}t |trtd dn
|  V  qdS )Tr   N)r   r   r)   r{   copy)r   _columnr;   r;   r<   r   w  s   z,Progress.make_tasks_table.<locals>.<genexpr>)r   r   )paddingr  c                 3   s,   | ]$}t |tr|j d n| V  qdS )r   N)r   r   r   )r   columnr   r;   r<   r     s   )rt   r*   gridr  r   add_row)r8   r  table_columnsr/  r;   r   r<   r.  n  s    	

zProgress.make_tasks_tablec                 C   s2   | j  |  W  d   S 1 s$0    Y  dS )z+Makes the Progress class itself renderable.N)r   r  rH   r;   r;   r<   __rich__  s    zProgress.__rich__r   )ra   rG   rb   r5   r   r   r>   c           	      K   s   | j d t| j|||||| j| j d}|| j| j< |rB| | j | j}tt| jd | _W d   n1 sp0    Y  |   |S )a  Add a new 'task' to the Progress display.

        Args:
            description (str): A description of the task.
            start (bool, optional): Start the task immediately (to calculate elapsed time). If set to False,
                you will need to call `start` manually. Defaults to True.
            total (float, optional): Number of total steps in the progress if know. Defaults to 100.
            completed (int, optional): Number of steps completed so far.. Defaults to 0.
            visible (bool, optional): Enable display of the task. Defaults to True.
            **fields (str): Additional data fields required for rendering.

        Returns:
            TaskID: An ID you can use when calling `update`.
        )r   r   r   r   r   N)	r   r|   r  rf   r  r$  r-   r   r?   )	r8   ra   rG   rb   r5   r   r   r~   new_task_indexr;   r;   r<   r     s$    
2zProgress.add_taskc                 C   s2   | j  | j|= W d   n1 s$0    Y  dS )z]Delete a task if it exists.

        Args:
            task_id (TaskID): A task ID.

        N)r   r  )r8   r2   r;   r;   r<   remove_task  s    zProgress.remove_task)NNrY   r_   )r   )Tr   r   T)0rR   rS   rT   rU   r   r   ru   r   r   r   rV   r  r7   r
  rd   r   r|   r  r-   r  r   rG   r  rI   r   rW   r   rQ   r   r.   r   rs   r$  r%  r   rB   r   r+  r@   r?   r!   r  r-  r*   r.  r7  r   r9  r;   r;   r;   r<   r0   -  s   
+
    1?%!    *r0   __main__)Panel)Rule)Syntax)r*   a~  def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
    """Iterate and generate a tuple with a flag for last value."""
    iter_values = iter(values)
    try:
        previous_value = next(iter_values)
    except StopIteration:
        return
    for value in iter_values:
        yield False, previous_value
        previous_value = value
    yield True, previous_valuepython)line_numbersZfoobarZbaz123z:Text may be printed while the progress bars are rendering.z(In fact, [i]any[/i] renderable will workzSuch as [magenta]tables[/]...zPretty printed structures...ZexamplezPretty printed)typer   z	Syntax...zGive it a try!)cycle)recordrm   rn   )rd   re   z[red]Downloadingr   r  z[green]Processingz[yellow]Thinking)rb   rG   r   )r@   g333333?g{Gz?d   )rY   NTNFNrZ   r[   r\   r]   r^   r_   F)mabcr   r   collectionsr   collections.abcr   dataclassesr   r   datetimer   mathr	   	threadingr
   r   r   typesr   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rd   r   r    r!   r"   r   r#   jupyterr$   r  r%   progress_barr&   r   r'   rh   r(   r/  r)   r*   r   r+   r,   r   r-   r.   rV   r  r/   r   r   rs   ru   r   r   ro   rq   r   rr   r   r   r   r   r   r|   r0   rR   randomtimepanelr;  ruler<  syntaxr=  r5  Zprogress_renderables	itertoolsrE  Zexamplesr1   r   Ztask1Ztask2Ztask3r   rB   sleeprandintr  r   r;   r;   r;   r<   <module>   s   @
*             D*2*		%	x   

	
