U
    祡c                     @   s   d dl mZ d dlmZmZmZmZmZ ddlm	Z	 ddl
mZmZ er\ddlmZmZmZ G dd deZd	d
ed ddddZdS )    )
itemgetter)TYPE_CHECKINGCallable
NamedTupleOptionalSequence   )errors)is_renderable	rich_cast)ConsoleConsoleOptionsRenderableTypec                   @   s   e Zd ZU dZeed< eed< eedddZd dddZed d	d
dZ	ed d	ddZ
dee ee d dddZedddd dddZdS )MeasurementzSStores the minimum and maximum widths (in characters) required to render an object.minimummaximum)returnc                 C   s   | j | j S )z+Get difference between maximum and minimum.)r   r   )self r   0/tmp/pip-unpacked-wheel-qkhzhrww/rich/measure.pyspan   s    zMeasurement.spanc                 C   s4   | \}}t td||}ttd|tdt||S )zGet measurement that ensures that minimum <= maximum and minimum >= 0

        Returns:
            Measurement: A normalized measurement.
        r   )minmaxr   )r   r   r   r   r   r   	normalize   s    zMeasurement.normalize)widthr   c                 C   s   | \}}t t||t||S )zGet a RenderableWith where the widths are <= width.

        Args:
            width (int): Maximum desired width.

        Returns:
            Measurement: New Measurement object.
        )r   r   r   r   r   r   r   r   r   with_maximum"   s    	zMeasurement.with_maximumc                 C   s(   | \}}t d|}tt ||t ||S )zGet a RenderableWith where the widths are >= width.

        Args:
            width (int): Minimum desired width.

        Returns:
            Measurement: New Measurement object.
        r   )r   r   r   r   r   r   with_minimum.   s    	
zMeasurement.with_minimumN)	min_width	max_widthr   c                 C   s,   | }|dk	r| |}|dk	r(||}|S )aN  Clamp a measurement within the specified range.

        Args:
            min_width (int): Minimum desired width, or ``None`` for no minimum. Defaults to None.
            max_width (int): Maximum desired width, or ``None`` for no maximum. Defaults to None.

        Returns:
            Measurement: New Measurement object.
        N)r   r   )r   r   r   Zmeasurementr   r   r   clamp;   s    

zMeasurement.clampr   r   r   )consoleoptions
renderabler   c                 C   s   |j }|dk rtddS t|tr4|j||jdd}t|}t|rt|dd}|dk	r|||	 
|}|jdk rtddS |	 S td|S ntd|ddS )	a  Get a measurement for a renderable.

        Args:
            console (~rich.console.Console): Console instance.
            options (~rich.console.ConsoleOptions): Console options.
            renderable (RenderableType): An object that may be rendered with Rich.

        Raises:
            errors.NotRenderableError: If the object is not renderable.

        Returns:
            Measurement: Measurement object containing range of character widths required to render the object.
        r   r   F)markupZ	highlightZ__rich_measure__NzUnable to get render width for zD; a str, Segment, or object with __rich_console__ method is required)r   r   
isinstancestrZ
render_strr$   r   r
   getattrr   r   r   r	   ZNotRenderableError)clsr!   r"   r#   Z
_max_widthZget_console_widthZrender_widthr   r   r   getN   s2    

  



zMeasurement.get)NN)__name__
__module____qualname____doc__int__annotations__propertyr   r   r   r   r   r    classmethodr)   r   r   r   r   r      s*   

      r   r   r   r   )r!   r"   renderablesr   c                    sT   |st ddS t j fdd|D }t t|tddjt|tddj}|S )a  Get a measurement that would fit a number of renderables.

    Args:
        console (~rich.console.Console): Console instance.
        options (~rich.console.ConsoleOptions): Console options.
        renderables (Iterable[RenderableType]): One or more renderable objects.

    Returns:
        Measurement: Measurement object containing range of character widths required to
            contain all given renderables.
    r   c                    s   g | ]} |qS r   r   ).0r#   r!   Zget_measurementr"   r   r   
<listcomp>   s    z'measure_renderables.<locals>.<listcomp>)keyr   )r   r)   r   r   r   r   )r!   r"   r2   ZmeasurementsZmeasured_widthr   r4   r   measure_renderables}   s    
r7   N)operatorr   typingr   r   r   r   r    r	   protocolr
   r   r!   r   r   r   r   r7   r   r   r   r   <module>   s   s