o
    MfV                      @   st  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ZB										 	!	"	dwd#e"ee? ee? f d$eCd%ee@ d&eDd'ee' d(eDd)eeg e@f  d*e@d+e6d,e6d-e6d.e6d/e@d0eDd1ee? fd2d3ZEG d4d5 d5eZFG d6d7 d7eFZGG d8d9 d9eFZHG d:d; d;eFZIG d<d= d=eFZJG d>d? d?eFZKG d@dA dAeFZLG dBdC dCeFZMG dDdE dEeFZNG dFdG dGeFZOG dHdI dIeFZPG dJdK dKeZQeG dLdM dMZRG dNdO dOe.ZSeTdPkr8d dlUZUd dlVZVd
dQlWmXZX d
dRlYmZZZ d
dSl[m\Z\ d
dTl7m9Z9 e\dUdVddWZ[e9dXdYdZZ7e7]d[d\d] d^eXd_d`e7dadbdcdddee[eZdfg	Z^d dgl_m`Z` e`e^Zae'ddhZ&eSeH eIdieJ eIdjeL eK e&ddkRZbebjcdldmdnZdebjcdodmdnZeebjcdpdmddqZfebjgs%ebjheddrds ebjheedtds eVidu eUjd dvd
k rebkelea ebjgrW d   dS W d   dS 1 s1w   Y  dS dS )x    )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                       sl   e Zd ZdZdddddef fddZdddZdddZdee	e
  dee
 dee d	d
fddZ  ZS )_TrackThreadz)A thread to periodically update progress.progressProgresstask_idr-   update_periodc                    s.   || _ || _|| _t | _d| _t   d S )Nr   )r0   r2   r3   r
   done	completedsuper__init__)selfr0   r2   r3   	__class__ [/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/pip/_vendor/rich/progress.pyr7   /   s   z_TrackThread.__init__returnNc                 C   sj   | j }| jj}| j}d}| jj}||s(| j}||kr$||||  |}||r| jj| j | jdd d S )Nr   T)r5   refresh)r2   r0   advancer3   r4   waitr5   update)r8   r2   r?   r3   last_completedr@   r5   r;   r;   r<   run8   s   z_TrackThread.runc                 C      |    | S Nstartr8   r;   r;   r<   	__enter__F      z_TrackThread.__enter__exc_typeexc_valexc_tbc                 C   s   | j   |   d S rE   )r4   setjoinr8   rK   rL   rM   r;   r;   r<   __exit__J   s   
z_TrackThread.__exit__r=   N)r=   r/   )__name__
__module____qualname____doc__floatr7   rC   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||||||p$d|d}| |j| |||dE dH  W d   dS 1 sCw   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})ri   rj   rk   rl   -[progress.percentage]{task.percentage:>3.0f}%r[   )rd   re   rf   rg   rh   rm   )rc   rb   r3   N)
TextColumnextend	BarColumnTimeRemainingColumnr1   track)ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   r3   rm   columnsr0   r;   r;   r<   rt   T   s8   &
"rt   c                   @   sp   e Zd ZU dZdZee ed< ddee ddfddZ	defdd	Z
d
ddefddZed
ddef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   rx   r;   r;   r<   r7      s   
zProgressColumn.__init__c                 C   s   | j pt S )z.Get a table column, used to build tasks table.)ry   r)   rH   r;   r;   r<   get_table_column   s   zProgressColumn.get_table_columntaskTaskc                 C   sp   |  }| jdur)|js)z
| j|j \}}W n	 ty   Y n
w || j |kr)|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)rg   rw   r5   rz   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.renderrE   )rS   rT   rU   rV   rw   r   rW   __annotations__r)   r7   r|   r!   r   r   r   r;   r;   r;   r<   rv      s   
 rv   c                       sJ   e Zd ZdZ	ddddedee f fddZd	d
defddZ  Z	S )RenderableColumnzA column to insert an arbitrary column.

    Args:
        renderable (RenderableType, optional): Any renderable. Defaults to empty string.
     Nrx   r   rx   c                      || _ t j|d d S Nr   )r   r6   r7   )r8   r   rx   r9   r;   r<   r7         zRenderableColumn.__init__r}   r~   r=   c                 C   s   | j S rE   )r   r   r;   r;   r<   r      s   zRenderableColumn.render)r   )
rS   rT   rU   rV   r!   r   r)   r7   r   rY   r;   r;   r9   r<   r      s    r   c                       s   e Zd ZdZ					ddedee d	ed
edee	 f
 fddZ
		ddedee d	eddfddZdddef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_nameri   speedfinished_textrx   c                    s<   t |||d| _t|trt|n|| _t j|d d S )Nri   r   r   )	r'   spinner
isinstancestrr+   from_markupr   r6   r7   )r8   r   ri   r   r   rx   r9   r;   r<   r7      s   zSpinnerColumn.__init__spinner_styler=   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}|S | j| }|S rE   )finishedr   r   r   rg   )r8   r}   textr;   r;   r<   r      s   zSpinnerColumn.render)r   r   r   r   N)r   r   )rS   rT   rU   rV   r   r   r(   rW   r,   r)   r7   r   r!   r   rY   r;   r;   r9   r<   r      s<    
r   c                       sd   e Zd ZdZ					ddededed	ed
ee	 dee
 ddf fddZdddefddZ  ZS )rp   zA column containing text.noneleftTNtext_formatri   justifymarkuphighlighterrx   r=   c                    s:   || _ || _|| _|| _|| _t j|ptddd d S )NTno_wrapr   )r   r   ri   r   r   r6   r7   r)   )r8   r   ri   r   r   r   rx   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r'| j| |S )Nr}   )ri   r   )	r   formatr   r+   r   ri   r   r   	highlight)r8   r}   _textr   r;   r;   r<   r     s   zTextColumn.render)r   r   TNN)rS   rT   rU   rV   r   r(   r    boolr   r#   r)   r7   r+   r   rY   r;   r;   r9   r<   rp     s.    rp   c                       sf   e Zd ZdZ						ddee d	ed
edededee ddf fddZddde	fddZ
  ZS )rr   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_widthri   rj   rk   rl   rx   r=   c                    s0   || _ || _|| _|| _|| _t j|d d S r   )r   ri   rj   rk   rl   r6   r7   )r8   r   ri   rj   rk   rl   rx   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   )	rc   r5   widthpulseanimation_timeri   rj   rk   rl   )r&   maxrc   r5   r   startedrg   ri   rj   rk   rl   r   r;   r;   r<   r   >  s   

zBarColumn.render)r   r\   r]   r^   r_   N)rS   rT   rU   rV   r   intr(   r)   r7   r&   r   rY   r;   r;   r9   r<   rr   #  s0    rr   c                   @   "   e Zd ZdZdddefddZdS )TimeElapsedColumnzRenders time elapsed.r}   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ri   seconds)r   finished_timeelapsedr+   r   r   r   )r8   r}   r   deltar;   r;   r<   r   P  s
   zTimeElapsedColumn.renderNrS   rT   rU   rV   r+   r   r;   r;   r;   r<   r   M      r   c                   @   s&   e Zd ZdZdZdddefddZdS )	rs   z!Renders estimated time remaining.      ?r}   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)rS   rT   rU   rV   rw   r+   r   r;   r;   r;   r<   rs   Y  s    rs   c                   @   r   )FileSizeColumnzRenders completed filesize.r}   r~   r=   c                 C      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     zFileSizeColumn.renderNr   r;   r;   r;   r<   r   h  r   r   c                   @   r   )TotalFileSizeColumnzRenders total filesize.r}   r~   r=   c                 C   r   )r   zprogress.filesize.totalr   )r   r   r   rc   r+   r   r;   r;   r<   r   t  r   zTotalFileSizeColumn.renderNr   r;   r;   r;   r<   r   q  r   r   c                       sH   e Zd ZdZ	ddedee ddf fddZd	d
def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_unitsrx   r=   c                    r   r   )r   r6   r7   )r8   r   rx   r9   r;   r<   r7     r   zDownloadColumn.__init__r}   r~   c                 C   s   t |j}t |j}| jrt|g dd\}}nt|g dd\}}|| }|| }|dkr2d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   rc   r   r   pick_unit_and_suffixr+   )r8   r}   r5   rc   unitsuffixcompleted_ratiototal_ratio	precisioncompleted_str	total_strdownload_statusdownload_textr;   r;   r<   r     s&   



zDownloadColumn.render)FN)rS   rT   rU   rV   r   r   r)   r7   r+   r   rY   r;   r;   r9   r<   r   z  s    r   c                   @   r   )TransferSpeedColumnz&Renders human readable transfer speed.r}   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     r   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)rS   rT   rU   rV   rW   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< 	 defddZedefddZedefddZede
e fddZedefdd Z edefd!d"Z!ede
e fd#d$Z"ede
e fd%d&Z#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   rb   rc   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<   rg        zTask.get_timec                 C   
   | j duS )z#bool: Check if the task as started.N)r   rH   r;   r;   r<   r        
zTask.startedc                 C   s   | j | j S )z)float: Get the number of steps remaining.)rc   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   rg   rH   r;   r;   r<   r     s
   

zTask.elapsedc                 C   r   )zCheck if the task has finished.N)r   rH   r;   r;   r<   r     r   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@)rc   r5   minr   )r8   r5   r;   r;   r<   
percentage   s
   zTask.percentagec                 C   s   | j du rdS | jD | j}|s	 W d   dS |d j|d j }|dkr0	 W d   dS t|}t| tdd |D }|| }|W  d   S 1 sQw   Y  dS )z=Optional[float]: Get the estimated speed in steps per second.Nr   c                 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   r0   
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._resetrR   )%rS   rT   rU   rV   r-   r   r   rW   GetTimeCallabler   r   r   r   r   dictr   r   r   r   r   r   r   r   r   r   r   r   rg   propertyr   r   r   r   r  r   r   r  r;   r;   r;   r<   r~     sZ   
 
r~   c                   @   s  e Zd ZdZddddddddddd
deeef d	ee d
e	de
de
de	de	de	dee de	de	ddfddZedefddZedee fddZedee fddZede	fddZdVddZdVd d!ZdWd"d#Zd$eee  d%ee d&ee ddfd'd(Z			)	*dXd+eee ee f d,ee
 d-ee d.ed/e
dee fd0d1Zd-eddfd2d3Z d-eddfd4d5Z!ddddddd6d-ed,ee
 d7ee
 d8ee
 d.ee d9ee	 d:e	d;e"ddfd<d=Z#ddd>ddd?d-ed@e	d,ee
 d7e$d9ee	 d.ee d;e"ddfdAdBZ%dYd-ed8e
ddfdDdEZ&dVdFdGZ'de(fdHdIZ)dee( fdJdKZ*dLee de+fdMdNZ,de(fdOdPZ-		Q	>	dZd.ed@e	d,e
d7e$d9e	d;e"defdRdSZ.d-eddfdTdUZ/dS )[r1   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.
    NTr[   g      >@F)
re   rd   rh   speed_estimate_periodrf   redirect_stdoutredirect_stderrrg   rm   expandru   re   rd   rh   r  rf   r  r  rg   rm   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|p5t |||||| jd| _|pE| jj| _| jj| _| jj| _d S )Nr   zrefresh_per_second must be > 0rn   ro   )re   rd   rh   rf   r  r  get_renderable)r   r   rp   rr   rs   ru   r  rm   r  _tasksr-   _task_indexr%   r   r  livere   rg   printlog)r8   re   rd   rh   r  rf   r  r  rg   rm   r  ru   r;   r;   r<   r7   =  s6   
	
zProgress.__init__c                 C   s   | j jS rE   )r  re   rH   r;   r;   r<   re   h  r   zProgress.consolec                 C   8   | j  t| j W  d   S 1 sw   Y  dS )zGet a list of Task instances.N)r   listr  valuesrH   r;   r;   r<   tasksl     $zProgress.tasksc                 C   r  )zA list of task IDs.N)r   r   r  keysrH   r;   r;   r<   task_idsr  r#  zProgress.task_idsc                 C   sZ   | j   | js	 W d   dS tdd | j D W  d   S 1 s&w   Y  dS )z'Check if all tasks have been completed.NTc                 s   r  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 dS )zStart the progress display.T)r>   N)rm   r  rG   rH   r;   r;   r<   rG     s   zProgress.startc                 C   s$   | j   | jjs| j  dS dS )zStop the progress display.N)r  stopre   is_interactiver  rH   r;   r;   r<   r'    s   
zProgress.stopc                 C   rD   rE   rF   rH   r;   r;   r<   rI     rJ   zProgress.__enter__rK   rL   rM   c                 C   s   |    d S rE   )r'  rP   r;   r;   r<   rQ     s   zProgress.__exit__rZ   r`   ra   rc   r2   rb   r3   c                 c   s    |du rt |trtt|}n
td|d|}|du r'| j||d}n| j||d | jjrZt	| ||}|D ]}|V  | j
d7  _
q;W d   dS 1 sSw   Y  dS | j}	| j}
|D ]}|V  |	|d |
  qbd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'rc   r   )r   r   rW   len
ValueErroradd_taskrA   r  rd   r/   r5   r?   r>   )r8   ra   rc   r2   rb   r3   
task_totaltrack_threadvaluer?   r>   r;   r;   r<   rt     s2   

"
zProgress.trackc                 C   sZ   | j   | j| }|jdu r|  |_W d   dS W d   dS 1 s&w   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   rg   )r8   r2   r}   r;   r;   r<   
start_task  s   	

"zProgress.start_taskc                 C   sT   | j  | j| }|  }|jdu r||_||_W d   dS 1 s#w   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  rg   r   r   )r8   r2   r}   r   r;   r;   r<   	stop_task  s   

"zProgress.stop_task)rc   r5   r?   rb   r   r>   r5   r?   r   r>   r   c                K   s\  | j  | j| }	|	j}
|dur||	jkr||	_|	  |dur'|	 j|7  _|dur.||	_|dur5||	_|dur<||	_|	j| |	j|
 }| 	 }|| j
 }|	j}|j}|rk|d j|k rk|  |rk|d j|k s_t|dkrz|  t|dksq|dkr|t|| |	j|	jkr|	jdu r|	j|	_W d   n1 sw   Y  |r|   dS 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   rc   r  rb   r   r   rA   rg   r  r   popleftr   r*  appendr   r   r   r>   )r8   r2   rc   r5   r?   rb   r   r>   r   r}   completed_startupdate_completedr   old_sample_timer   r2  r;   r;   r<   rA     sF   


 zProgress.updater   )rG   rc   r5   r   rb   rG   c          
      K   s   |   }| j8 | j| }	|	  |r|nd|	_|dur||	_||	_|dur)||	_|r.||	_|dur5||	_	d|	_
W d   n1 sBw   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)rg   r   r  r  r   rc   r5   r   r   rb   r   r>   )
r8   r2   rG   rc   r5   r   rb   r   r   r}   r;   r;   r<   reset   s"   
zProgress.resetr   c           
      C   s  |   }| jw | j| }|j}| j|7  _|j| }|| j }|j}|j}	|r<|d j|k r<|	  |r<|d j|k s0t|dkrK|	  t|dksB|	t
|| |j|jkrn|jdu rv|j|_|j|_W d   dS W d   dS W d   dS 1 sw   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)rg   r   r  r5   r  r   r2  r   r*  r3  r   rc   r   r   r   r   )
r8   r2   r?   r   r}   r4  r5  r6  r   r2  r;   r;   r<   r?   E  s0   



"zProgress.advancec                 C   s$   | j s| jjr| j  dS dS dS )z*Refresh (render) the progress information.N)rm   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<   r9  i  s   
zProgress.get_renderablesr"  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   s0    | ]}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    
)ru   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w   Y  dS )z+Makes the Progress class itself renderable.N)r   r  rH   r;   r;   r<   __rich__  s   $zProgress.__rich__r   c           	      K   s   | j 2 t| j|||||| j| j d}|| j| j< |r!| | j | j}tt| jd | _W d   n1 s8w   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  rg   r  r0  r-   r   r>   )	r8   rb   rG   rc   r5   r   r   r}   new_task_indexr;   r;   r<   r,    s&   
zProgress.add_taskc                 C   s4   | j  | j|= W d   dS 1 sw   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_taskrR   )r=   r1   )NNrZ   r`   )r   )Tr   r   T)0rS   rT   rU   rV   r   r   rv   r   r   r   rW   r  r7   r  re   r   r~   r"  r-   r%  r   rG   r'  rI   r   rX   r   rQ   r   r.   r   rt   r0  r1  r   rA   r   r7  r?   r>   r!   r  r9  r*   r:  rC  r,  rE  r;   r;   r;   r<   r1   -  s*   
	

+





1	

?	

%
!
*r1   __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_numbersfoobar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...examplezPretty printed)typer   z	Syntax...zGive it a try!)cycle)recordrn   ro   )re   rf   z[red]Downloadingr   r)  z[green]Processingz[yellow]Thinking)rc   rG   r   )r?   g333333?g{Gz?d   )rZ   NTNFNr[   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   re   r   r    r!   r"   r   r#   jupyterr$   r  r%   progress_barr&   r   r'   ri   r(   r;  r)   r*   r   r+   r,   r   r-   r.   rW   r  r/   r   r   rt   rv   r   r   rp   rr   r   rs   r   r   r   r   r   r~   r1   rS   randomtimepanelrG  rulerH  syntaxrI  rA  progress_renderables	itertoolsrT  examplesr0   r,  task1task2task3r   rA   sleeprandintr  r
  r;   r;   r;   r<   <module>   s   @
*	

D*2*		%	x   

	

$