U
    祡c                     @   s,  d dl 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 ddl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 dZG dd deZedkr(e ZedddZd dlZed e d ddD ].Z!e"e! e#e ej$%d e&d qed e#  dS )    N)	lru_cache)	monotonic)IterableListOptional   )Color	blend_rgb)ColorTriplet)ConsoleConsoleOptionsRenderResult)JupyterMixin)Measurement)Segment)Style	StyleType   c                   @   s   e Zd ZdZd"ee eee eeeeeee d
	ddZ	e
dddZeee dddZeddd#eee
eeee dddZd$eee ddddZd%eeeee dddZeeedddZeeedd d!ZdS )&ProgressBara  Renders a (progress) bar. Used by rich.progress.

    Args:
        total (float, optional): Number of steps in the bar. Defaults to 100. Set to None to render a pulsing animation.
        completed (float, optional): Number of steps completed. Defaults to 0.
        width (int, optional): Width of the bar, or ``None`` for maximum width. Defaults to None.
        pulse (bool, optional): Enable pulse effect. Defaults to False. Will pulse if a None total was passed.
        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.finished".
        pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
        animation_time (Optional[float], optional): Time in seconds to use for animation, or None to use system time.
          Y@r   NFbar.backbar.completebar.finished	bar.pulse)	total	completedwidthpulsestylecomplete_stylefinished_stylepulse_styleanimation_timec
           
      C   s@   || _ || _|| _|| _|| _|| _|| _|| _|	| _d | _	d S )N)
r   r   r   r   r   r   r    r!   r"   Z_pulse_segments)
selfr   r   r   r   r   r   r    r!   r"    r$   5/tmp/pip-unpacked-wheel-qkhzhrww/rich/progress_bar.py__init__!   s    zProgressBar.__init__)returnc                 C   s   d| j d| jdS )Nz<Bar z of >r   r   )r#   r$   r$   r%   __repr__9   s    zProgressBar.__repr__c                 C   s2   | j dkrdS | j| j  d }tdtd|}|S )zCalculate percentage complete.Nr   d   g        )r   r   minmax)r#   r   r$   r$   r%   percentage_completed<   s
    
z ProgressBar.percentage_completed   )maxsize)
fore_style
back_stylecolor_systemno_colorasciir'   c                 C   s  |rdnd}g }|dks|r\|t ||gtd  7 }|t |r@dn||gttd   7 }|S |j}|jrr|j n
tddd}	|jr|j n
tddd}
tj}tj}t }t	}t
j}ttD ]J}|t }d||| d d	  }t|	|
|d
}||||||d q|S )zGet a list of segments to render a pulse animation.

        Returns:
            List[Segment]: A list of segments, one segment per character.
        -   ━)standardZ	eight_bitZ	truecolor       r   g      ?g       @)Z
cross_fade)color)r   
PULSE_SIZEappendr<   Zget_truecolorr
   mathcospir   r   from_tripletranger	   )r#   r1   r2   r3   r4   r5   barsegmentsr>   Z
fore_colorZ
back_colorr@   rA   _SegmentZ_StylerB   indexpositionZfader<   r$   r$   r%   _get_pulse_segmentsE   s8    


zProgressBar._get_pulse_segments)r   r   r'   c                 C   s   || _ |dk	r|n| j| _dS )zUpdate progress with new values.

        Args:
            completed (float): Number of steps completed.
            total (float, optional): Total number of steps, or ``None`` to not change. Defaults to None.
        Nr)   )r#   r   r   r$   r$   r%   updatet   s    zProgressBar.update)consoler   r5   r'   c                 c   s   |j | jdd}|j | jdd}| j|||j|j|d}t|}| jdkrPt n| j}|t	|| d  }	t	| d | }
|	|
|
|  }	|	E dH  dS )a)  Renders the pulse animation.

        Args:
            console (Console): Console instance.
            width (int): Width in characters of pulse animation.

        Returns:
            RenderResult: [description]

        Yields:
            Iterator[Segment]: Segments to render pulse
        Zwhite)defaultZblackr5   Nr9      )
	get_styler!   r   rI   r3   r4   lenr"   r   int)r#   rK   r   r5   r1   r2   Zpulse_segmentsZsegment_countcurrent_timerE   offsetr$   r$   r%   _render_pulse~   s         zProgressBar._render_pulse)rK   optionsr'   c                 c   s  t | jp|j|j}|jp|j}| jp.| jd k}|rN| j|||dE d H  d S | jd k	rlt | jtd| j	nd }|rxdnd}|rdnd}|rdnd}	| jr|d k	rt
|d | | j n|d }
|
d }|
d }|| j}| jd kp| j	| jk}||r| jn| j}t}|r$||| |V  |r:||| |V  |js|| | }|r|jd k	r|s|r||	|V  |d	8 }|r||| |V  d S )
NrM   r   r6   r7   r:   u   ╸u   ╺r9   r   )r,   r   	max_widthZlegacy_windowsZ
ascii_onlyr   r   rT   r-   r   rQ   rO   r   r    r   r   r4   r3   )r#   rK   rU   r   r5   Zshould_pulser   rD   Zhalf_bar_rightZhalf_bar_leftZcomplete_halvesZ	bar_countZhalf_bar_countr   Zis_finishedr   rF   Zremaining_barsr$   r$   r%   __rich_console__   sJ     zProgressBar.__rich_console__c                 C   s$   | j d k	rt| j | j S td|jS )N   )r   r   rV   )r#   rK   rU   r$   r$   r%   __rich_measure__   s    
zProgressBar.__rich_measure__)	r   r   NFr   r   r   r   N)F)N)F)__name__
__module____qualname____doc__r   floatrQ   boolr   r&   strr*   propertyr.   r   r   r   r   rI   rJ   r   r   rT   r   r   rW   r   rY   r$   r$   r$   r%   r      sd             .    . r   __main__2   r+   )r   r   Fe   g?T)'r?   	functoolsr   timer   typingr   r   r   r<   r   r	   Zcolor_tripletr
   rK   r   r   r   Zjupyterr   Zmeasurer   segmentr   r   r   r   r=   r   rZ   rD   Zshow_cursorrC   nrJ   printfilewritesleepr$   r$   r$   r%   <module>   s2    B




