o
    ÏÉvi"*  ã                   @  s&  U d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZ e
eeef ZedƒZg d¢Zded	< ed
d„ eD ƒƒZejZded< G dd„ deƒZeddd2d3dd„ƒZedƒd2d4dd„ƒZd2d4dd„Zd4dd „Z	d2d5d"d#„Z	d2d6d&d'„Z	d2d6d(d)„Zd2d7d+d,„Zd2d8d/d0„Z d1S )9é    )Úannotations)Ú	lru_cache)Ú
itemgetter)ÚCallableÚ
NamedTupleÚSequenceÚTuple)Úloadé   ))é    é~   )é    é¬   )é®   iÿ  )ip  i‚  )i %  iü%  )i (  iÿ(  zlist[tuple[int, int]]Ú_SINGLE_CELL_UNICODE_RANGESc                 C  s.   g | ]\}}t tt||d  ƒƒD ]}|‘qqS )é   )ÚmapÚchrÚrange)Ú.0Ú_startÚ_endÚ	character© r   úI/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/rich/cells.pyÚ
<listcomp>   s    ýþÿr   zCallable[[str], bool]Ú_is_single_cell_widthsc                   @  s*   e Zd ZU dZded< ded< ded< dS )	Ú	CellTablezDContains unicode data required to measure the cell widths of glyphs.ÚstrÚunicode_versionzSequence[tuple[int, int, int]]Úwidthszfrozenset[str]Únarrow_to_wideN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r   r   r   &   s
   
 r   i   )ÚmaxsizeÚautor   r   r   ÚreturnÚintc           
      C  s¢   t | ƒ}t|ƒj}||d d krdS d}t|ƒd }|| d }	 || \}}}	||k r2|d }n||kr;|d }n|	dkrAdS |	S ||k rJ	 dS || d }q")a  Get the cell size of a character.

    Args:
        character (str): A single character.
        unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version.

    Returns:
        int: Number of cells (0, 1 or 2) occupied by that character.
    éÿÿÿÿr   r   r
   )ÚordÚload_cell_tabler    Úlen)
r   r   Ú	codepointÚtableÚlower_boundÚupper_boundÚindexÚstartÚendÚwidthr   r   r   Úget_character_cell_size.   s&   


ÿör7   Útextc                 C  s
   t | |ƒS )a  Get the number of cells required to display text.

    This method always caches, which may use up a lot of memory. It is recommended to use
    `cell_len` over this method.

    Args:
        text (str): Text to display.
        unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version.

    Returns:
        int: Get the number of cells required to display text.
    )Ú	_cell_len©r8   r   r   r   r   Úcached_cell_lenN   s   
r;   c                 C  s    t | ƒdk rt| |ƒS t| |ƒS )á   Get the cell length of a string (length as it appears in the terminal).

    Args:
        text: String to measure.
        unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version.

    Returns:
        Length of string in terminal cells.
    i   )r.   r;   r9   r:   r   r   r   Úcell_len_   s   


r=   c           	        s°   t | ƒrt| ƒS d| vrd| vrt‡ fdd„| D ƒƒS tˆ ƒ}d}d}ddh}t| ƒ}|D ](}||v rH|dkr<t|ƒ q-|rG|||jv 7 }d}q-t|ˆ ƒ }rU|}||7 }q-|S )r<   õ   â€õ   ï¸c                 3  s    | ]}t |ˆ ƒV  qd S )N)r7   )r   r   ©r   r   r   Ú	<genexpr>€   s   € 

ÿz_cell_len.<locals>.<genexpr>r   N)r   r.   Úsumr-   ÚiterÚnextr!   r7   )	r8   r   Ú
cell_tableÚtotal_widthÚlast_measured_characterÚSPECIALÚiter_charactersr   Úcharacter_widthr   r@   r   r9   n   s0   ÿ
€€r9   ú'tuple[list[CellSpan], int]'c                 C  s*  t |ƒ}t| ƒ}d}d}d}g }ddh}||k r‘| |  }	|v r_|	dkr9|d7 }|r8|d \}
}}|
||f|d< n%|r^|d7 }|r^|d \}
}}||jv rWd}|d7 }|d7 }|
||f|d< qt|	|ƒ }ry|	}| ||d  }|f¡ ||7 }n|r|d \}
}}|
|d  }|f|d< ||k s||fS )z÷Divide text into spans that define a single grapheme.

    Args:
        text: String to split.
        unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version.

    Returns:
        List of spans.
    r   Nr>   r?   r
   r+   r   )r-   r.   r!   r7   Úappend)r8   r   rE   Úcodepoint_countr3   rG   rF   ÚspansrH   r   r4   r   Úcell_lengthrJ   r   r   r   Úsplit_graphemes›   sD   €

årP   Úcell_positionútuple[str, str]c                 C  s.  |dkrd| fS t | |ƒ\}}t|| t|ƒ ƒ}ttt|d|… ƒƒ}	 ||krE|t|ƒkr3| dfS || d }| d|… | |d… fS ||k ro|| \}}	}
||
 |krf| d|… d d| |	d…  fS |d7 }||
7 }n'||d  \}}	}
||
 |k rŽ| d|… d d| |	d…  fS |d8 }||
8 }q%)áp  Split text by cell position.

    If the cell position falls within a double width character, it is converted to two spaces.

    Args:
        text: Text to split.
        cell_position Offset in cells.
        unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version.

    Returns:
        Tuple to two split strings.
    r   Ú NTú r   )rP   r*   r.   rB   r   Ú_span_get_cell_len)r8   rQ   r   rN   rO   ÚoffsetÚ	left_sizeÚsplit_indexr4   r5   Ú	cell_sizer   r   r   Ú_split_textÐ   s.    
 ïr[   c                 C  s,   t | ƒr| d|… | |d… fS t| ||ƒS )rS   N)r   r[   )r8   rQ   r   r   r   r   Ú
split_textü   s   r\   Útotalc                 C  s€   t | ƒrt| ƒ}||k r| d||   S | d|… S |dkr dS t| ƒ}||kr*| S ||k r6| d||   S t| ||ƒ\} }| S )a$  Adjust a string by cropping or padding with spaces such that it fits within the given number of cells.

    Args:
        text: String to adjust.
        total: Desired size in cells.
        unicode_version: Unicode version.

    Returns:
        A string with cell size equal to total.
    rU   Nr   rT   )r   r.   r=   r[   )r8   r]   r   ÚsizerZ   Ú_r   r   r   Úset_cell_size  s   r`   r6   ú	list[str]c                   sš   t ˆ ƒr‡ ‡fdd„tdtˆ ƒˆƒD ƒS tˆ |ƒ\}}d}g }d}|D ]\}}	}
||
 ˆkr;| ˆ ||… ¡ |}d}||
7 }q#|rK| ˆ |d… ¡ |S )ah  Split text into lines such that each line fits within the available (cell) width.

    Args:
        text: The text to fold such that it fits in the given width.
        width: The width available (number of cells).

    Returns:
        A list of strings such that each string in the list has cell width
        less than or equal to the available width.
    c                   s   g | ]
}ˆ ||ˆ … ‘qS r   r   )r   r3   ©r8   r6   r   r   r   7  s    zchop_cells.<locals>.<listcomp>r   N)r   r   r.   rP   rL   )r8   r6   r   rN   r_   Ú	line_sizeÚlinesÚline_offsetr4   r5   rZ   r   rb   r   Ú
chop_cells+  s    
rf   N)r(   )r   r   r   r   r)   r*   )r8   r   r   r   r)   r*   )r8   r   r   r   r)   rK   )r8   r   rQ   r*   r   r   r)   rR   )r8   r   r]   r*   r   r   r)   r   )r8   r   r6   r*   r   r   r)   ra   )!Ú
__future__r   Ú	functoolsr   Úoperatorr   Útypingr   r   r   r   Úrich._unicode_datar	   r-   r*   ÚCellSpanrV   r   r&   Ú	frozensetÚ_SINGLE_CELLSÚ
issupersetr   r   r7   r;   r=   r9   rP   r[   r\   r`   rf   r   r   r   r   Ú<module>   s:    
þÿ

.ÿ6ÿ-ÿ