o
    =Ѯa                      @   sZ   d Z ddlZddlZddlmZ edZdd Zddd	Zdd
dZ	dd Z
dd ZdS )z6Functions that handle alignment, padding, widths, etc.    N)terminal_sizez(\033\[[\d;]+m)c              	   C   sl   d| v r
t d| } z| d} W n ttfy   Y nw d}| D ]}t|dv r/|d7 }q!|d7 }q!|S )a(  Get the visible width of a unicode string.

    Some CJK unicode characters are more than one byte unlike ASCII and latin unicode characters.

    From: https://github.com/Robpol86/terminaltables/pull/9

    :param str string: String to measure.

    :return: String's width.
    :rtype: int
     u8r   )FW      )RE_COLOR_ANSIsubdecodeAttributeErrorUnicodeEncodeErrorunicodedataZeast_asian_width)stringwidthchar r   Fusr/lib/python3.10/site-packages/terminaltables/width_and_alignment.pyvisible_width   s   

r    c           
      C   s  t | ds	t| } |  pdg}| dr|d d|v r7dg|d t| |d   | dg|d   }nAd|v r`|d t| }dg|d |d  |d   | dg|d |d    }ndg|d  | dg|d t| |d    }t|D ]R\}}|d	 t| t| }	d
|v r||d	 |	 |||d   ||< q|d|v r||d	  |	|	| ||d   ||< q|||d	  |
|	|d  | ||< q||S )aT  Align a string horizontally and vertically. Also add additional padding in both dimensions.

    :param str string: Input string to operate on.
    :param tuple align: Tuple that contains one of left/center/right and/or top/middle/bottom.
    :param tuple inner_dimensions: Width and height ints to expand string to without padding.
    :param iter padding: Number of space chars for left, right, top, and bottom (4 ints).
    :param str space: Character to use as white space for resizing/padding (use single visible chars only).

    :return: Padded cell split into lines.
    :rtype: list
    
splitlinesr   
Zbottomr	   r      middler   rightcenter)hasattrstrr   endswithappendlen	enumerater   rjustr   ljust)
r   ZalignZinner_dimensionspaddingspacelinesdeltailineZ	new_widthr   r   r   align_and_pad_cell*   s&   


2:0&*&r+   c                    s   dg| rt dd | D nd }dgt|  }t| D ]?\}}t|D ]6\}	}
t|
dr1t|
ds5t|
}
|
s8q#t || |
dd ||< t ||	 gdd	 |
 D R  ||	< q#qfd
d	|D } fdd	|D }||||fS )aD  Get maximum widths of each column and maximum height of each row.

    :param iter table_data: List of list of strings (unmodified table data).
    :param int padding_left: Number of space chars on left side of cell.
    :param int padding_right: Number of space chars on right side of cell.
    :param int padding_top: Number of empty lines on top side of cell.
    :param int padding_bottom: Number of empty lines on bottom side of cell.

    :return: 4-item tuple of n-item lists. Inner column widths and row heights, outer column widths and row heights.
    :rtype: tuple
    r   c                 s   s    | ]}t |V  qd S )N)r!   ).0rr   r   r   	<genexpr>`   s    z!max_dimensions.<locals>.<genexpr>countr   r   r	   c                 S   s   g | ]}t |qS r   )r   )r,   lr   r   r   
<listcomp>k   s    z"max_dimensions.<locals>.<listcomp>c                    s   g | ]} |  qS r   r   r,   r)   )padding_leftpadding_rightr   r   r1   n       c                    s   g | ]}|   qS r   r   r2   )padding_bottompadding_topr   r   r1   o   r5   )maxr!   r"   r   r   r/   r   )Z
table_datar3   r4   r7   r6   inner_widthsZinner_heightsjrowr)   cellouter_widthsZouter_heightsr   )r6   r3   r4   r7   r   max_dimensionsT   s    (	r>   c           	      C   sN   t | }t d }|}|||d  7 }||| 7 }t| | |  }|| | S )a	  Determine the maximum width of a column based on the current terminal width.

    :param iter inner_widths: List of widths (no padding) for each column.
    :param int column_number: The column number to query.
    :param int outer_border: Sum of left and right outer border visible widths.
    :param int inner_border: Visible width of the inner border character.
    :param int padding: Total padding per cell (left + right padding).

    :return: The maximum width the column can be without causing line wrapping.
    r   r	   )r!   r   sum)	r9   Zcolumn_numberouter_borderinner_borderr%   column_countZterminal_widthnon_data_space
data_spacer   r   r   column_max_widtht   s   
rE   c                 C   s0   t | }|}|r|||d  7 }t| }|| S )ai  Determine the width of the entire table including borders and padding.

    :param iter outer_widths: List of widths (with padding) for each column.
    :param int outer_border: Sum of left and right outer border visible widths.
    :param int inner_border: Visible width of the inner border character.

    :return: The width of the table.
    :rtype: int
    r	   )r!   r?   )r=   r@   rA   rB   rC   rD   r   r   r   table_width   s   
rF   )r   )r   r   r   r   )__doc__rer   Zterminaltables.terminal_ior   compiler
   r   r+   r>   rE   rF   r   r   r   r   <module>   s    


* 