o
    Ďib*                     @  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   F/home/jeff/fluffinator/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 | }|r
|dk sd|  krdk rdS  ndS t|j}|d }||d kr*dS d}t|d }||kr[|| d? }|| \}}	}
||k rL|d }n||	krU|d }n|
S ||ks6dS )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   r   )ordload_cell_tabler    len)r   r   	codepointtable
last_entrylower_boundupper_boundindexstartendwidthr   r   r   get_character_cell_size.   s*   


	r9   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r:   r   r   r   r   cached_cell_lenQ   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=   r;   r<   r   r   r   cell_lenb   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}d}t| }||k rc| | }||v rN|dkrB|d7 }n|rM|||jv 7 }d}nt|  }	r[|}||	7 }|d7 }||k s1|S )r>      ‍   ️c                 3  s    | ]}t | V  qd S )N)r9   )r   r   r   r   r   	<genexpr>   s    

z_cell_len.<locals>.<genexpr>r   Nr   )r   r/   sumr.   r!   r9   )
r:   r   
cell_tabletotal_widthlast_measured_characterSPECIALr5   character_countr   character_widthr   rB   r   r;   q   s6   
r;   '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 )zDivide 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@   rA   r
   r,   r   )r.   r/   r!   r9   append)r:   r   rE   codepoint_countr5   rG   rF   spansrH   r   r6   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/   rD   r   _span_get_cell_len)r:   rQ   r   rN   rO   offset	left_sizesplit_indexr6   r7   	cell_sizer   r   r   _split_text   s.    
 r[   c                 C  s,   t | r| d| | |d fS t| ||S )rS   N)r   r[   )r:   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[   )r:   r]   r   sizerZ   _r   r   r   set_cell_size  s   r`   r8   	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   r5   r:   r8   r   r   r   =  s    zchop_cells.<locals>.<listcomp>r   N)r   r   r/   rP   rL   )r:   r8   r   rN   r_   	line_sizelinesline_offsetr6   r7   rZ   r   rb   r   
chop_cells1  s    
rf   N)r(   )r   r   r   r   r)   r*   )r:   r   r   r   r)   r*   )r:   r   r   r   r)   rK   )r:   r   rQ   r*   r   r   r)   rR   )r:   r   r]   r*   r   r   r)   r   )r:   r   r8   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   r9   r=   r?   r;   rP   r[   r\   r`   rf   r   r   r   r   <module>   s:    

"
16-