U
    祡cT                     @   s2  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 e j
dkrPd dlmZ nd dlmZ G dd deZee	e ee d	d
dZeee ee ee ee dddZdeee eee  ee dddZedkr.d dlmZ eG dd dZededddedddedddgZeee dS )    N)Fraction)ceil)castListOptionalSequence)      )Protocolc                   @   s:   e Zd ZU dZdZee ed< dZeed< dZ	eed< dS )Edgez1Any object that defines an edge (such as Layout).Nsize   ratiominimum_size)
__name__
__module____qualname____doc__r   r   int__annotations__r   r    r   r   //tmp/pip-unpacked-wheel-qkhzhrww/rich/_ratio.pyr      s   
r   )totaledgesreturnc                 C   s   dd |D }t }d|krdd tt||D }| tdd |D  }|dkrddd t||D S ||td	d |D }|D ]&\}}||j |jkr|j||<  qq|d}	|D ](\}}t||j |	 d
\}
}	|
||< qqqttt	 |S )a  Divide total space to satisfy size, ratio, and minimum_size, constraints.

    The returned list of integers should add up to total in most cases, unless it is
    impossible to satisfy all the constraints. For instance, if there are two edges
    with a minimum size of 20 each and `total` is 30 then the returned list will be
    greater than total. In practice, this would mean that a Layout object would
    clip the rows that would overflow the screen height.

    Args:
        total (int): Total number of characters.
        edges (List[Edge]): Edges within total space.

    Returns:
        List[int]: Number of characters for each edge.
    c                 S   s   g | ]}|j pd qS N)r   ).0edger   r   r   
<listcomp>%   s     z!ratio_resolve.<locals>.<listcomp>Nc                 S   s$   g | ]\}\}}|d kr||fqS r   r   )r   indexr   r   r   r   r   r   ,   s   
c                 s   s   | ]}|pd V  qdS )r   Nr   )r   r   r   r   r   	<genexpr>2   s     z ratio_resolve.<locals>.<genexpr>r   c                 S   s&   g | ]\}}|d kr|j p dn|qS )Nr   )r   )r   r   r   r   r   r   r   5   s   c                 s   s   | ]\}}|j pd V  qdS )r   N)r   )r   _r   r   r   r   r    ;   s     r   )
r   	enumeratezipsumr   r   divmodr   r   r   )r   r   ZsizesZ	_FractionZflexible_edges	remainingportionr   r   	remainderr   r   r   r   ratio_resolve   s2     

r)   )r   ratiosmaximumsvaluesr   c                 C   s   dd t ||D }t|}|s,|dd S | }g }|j}t |||D ]R\}}	}
|r|dkrt|	t|| | }||
|  ||8 }||8 }qF||
 qF|S )ad  Divide an integer total in to parts based on ratios.

    Args:
        total (int): The total to divide.
        ratios (List[int]): A list of integer ratios.
        maximums (List[int]): List of maximums values for each slot.
        values (List[int]): List of values

    Returns:
        List[int]: A list of integers guaranteed to sum to total.
    c                 S   s   g | ]\}}|r|nd qS r   r   )r   r   Z_maxr   r   r   r   _   s     z ratio_reduce.<locals>.<listcomp>Nr   )r#   r$   appendminround)r   r*   r+   r,   total_ratiototal_remainingresultr.   r   maximumvaluedistributedr   r   r   ratio_reduceQ   s    

r7   )r   r*   minimumsr   c                 C   s   |rdd t ||D }t|}|dks0td| }g }|j}|dkrVdgt| }n|}t ||D ]D\}}	|dkrt|	t|| | }
n|}
||
 ||8 }||
8 }qd|S )a<  Distribute an integer total in to parts based on ratios.

    Args:
        total (int): The total to divide.
        ratios (List[int]): A list of integer ratios.
        minimums (List[int]): List of minimum values for each slot.

    Returns:
        List[int]: A list of integers guaranteed to sum to total.
    c                 S   s   g | ]\}}|r|nd qS r-   r   )r   r   Z_minr   r   r   r      s     z$ratio_distribute.<locals>.<listcomp>r   zSum of ratios must be > 0N)r#   r$   AssertionErrorr.   lenmaxr   )r   r*   r8   r1   r2   Zdistributed_totalr.   Z	_minimumsr   Zminimumr6   r   r   r   ratio_distributeq   s$    
r<   __main__)	dataclassc                   @   s6   e Zd ZU dZee ed< dZeed< dZeed< dS )ENr   r   r   r   )	r   r   r   r   r   r   r   r   r   r   r   r   r   r?      s   
r?   n   r   )N)sysZ	fractionsr   mathr   typingr   r   r   r   version_infor
   Ztyping_extensionsr   r   r)   r7   r<   r   Zdataclassesr>   r?   Zresolvedprintr$   r   r   r   r   <module>   s6   
>   !   
$
(