
    h6                         S r SSKrSSKrSSKJr  SSKJrJrJrJ	r	J
r
Jr  Sr\R                  " S5      rS rSS jrS	 rSS
 jrSS jrS rg)a  
Functions that render ASCII tables.

Some generic notes about the table formatting functions in this module:

- These functions were not written with performance in mind (*at all*) because
  they're intended to format tabular data to be presented on a terminal. If
  someone were to run into a performance problem using these functions, they'd
  be printing so much tabular data to the terminal that a human wouldn't be
  able to digest the tabular data anyway, so the point is moot :-).

- These functions ignore ANSI escape sequences (at least the ones generated by
  the :mod:`~humanfriendly.terminal` module) in the calculation of columns
  widths. On reason for this is that column names are highlighted in color when
  connected to a terminal. It also means that you can use ANSI escape sequences
  to highlight certain column's values if you feel like it (for example to
  highlight deviations from the norm in an overview of calculated values).
    N)coerce_string)
ansi_strip
ansi_width	ansi_wrapterminal_supports_colorsfind_terminal_sizeHIGHLIGHT_COLOR)format_pretty_tableformat_robust_tableformat_rst_tableformat_smart_tablez^\d+(\.\d+)?$c                 &   U  Vs/ s H  n[        U5      PM     n n[        U5      n[        S U  5       5      (       dE  [        X5      n[        [	        [
        UR                  5       5      5      n[        5       u  pVXF::  a  U$ [        X5      $ s  snf )a  
Render tabular data using the most appropriate representation.

:param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
             containing the rows of the table, where each row is an
             iterable containing the columns of the table (strings).
:param column_names: An iterable of column names (strings).
:returns: The rendered table (a string).

If you want an easy way to render tabular data on a terminal in a human
friendly format then this function is for you! It works as follows:

- If the input data doesn't contain any line breaks the function
  :func:`format_pretty_table()` is used to render a pretty table. If the
  resulting table fits in the terminal without wrapping the rendered pretty
  table is returned.

- If the input data does contain line breaks or if a pretty table would
  wrap (given the width of the terminal) then the function
  :func:`format_robust_table()` is used to render a more robust table that
  can deal with data containing line breaks and long text.
c              3   F   #    U  H  n[        S  U 5       5      v   M     g7f)c              3   ,   #    U  H
  nS U;   v   M     g7f
N ).0cs     N/var/www/fran/franai/venv/lib/python3.13/site-packages/humanfriendly/tables.py	<genexpr>/format_smart_table.<locals>.<genexpr>.<genexpr>[   s     (a419as   N)anyr   rs     r   r   %format_smart_table.<locals>.<genexpr>[   s     7$Qs(a((($s   !)	normalize_columnsr   r
   maxmapr   
splitlinesr   r   )datacolumn_namesr   pretty_tabletable_widthnum_rowsnum_columnss          r   r   r   >   s    2 +//$Qa $D/$\2L 7$777*4>#j,*A*A*CDE 2 4%t22 0s   Bc                    U  Vs/ s H  n[        USS9PM     n nUbN  [        U5      nU(       a<  [        5       (       a  U Vs/ s H  n[        U5      PM     nnU R                  SU5        [        R
                  " [        5      n[        R
                  " [        5      n[        U 5       Hy  u  p[        U	5       He  u  p[        Xj   [        U5      5      Xj'   U(       a  US:X  a  M-  Xz   R                  [        [        R                  [        U5      5      5      5        Mg     M{     U[!        UR#                  5       5      [%        U5      S-  -   S-   -  nU/n[        U 5       H  u  pU/n[        U	5       Hp  u  pSXj   [        U5      -
  -  n['        Xz   5      (       a  UR                  SU-   U-   S-   5        OUR                  SU-   U-   S-   5        UR                  U5        Mr     UR                  SR)                  U5      5        U(       d  M  US:X  d  M  UR                  U5        M     UR                  U5        SR)                  U5      $ s  snf s  snf )	a  
Render a table using characters like dashes and vertical bars to emulate borders.

:param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
             containing the rows of the table, where each row is an
             iterable containing the columns of the table (strings).
:param column_names: An iterable of column names (strings).
:param horizontal_bar: The character used to represent a horizontal bar (a
                       string).
:param vertical_bar: The character used to represent a vertical bar (a
                     string).
:returns: The rendered table (a string).

Here's an example:

>>> from humanfriendly.tables import format_pretty_table
>>> column_names = ['Version', 'Uploaded on', 'Downloads']
>>> humanfriendly_releases = [
... ['1.23', '2015-05-25', '218'],
... ['1.23.1', '2015-05-26', '1354'],
... ['1.24', '2015-05-26', '223'],
... ['1.25', '2015-05-26', '4319'],
... ['1.25.1', '2015-06-02', '197'],
... ]
>>> print(format_pretty_table(humanfriendly_releases, column_names))
-------------------------------------
| Version | Uploaded on | Downloads |
-------------------------------------
| 1.23    | 2015-05-25  |       218 |
| 1.23.1  | 2015-05-26  |      1354 |
| 1.24    | 2015-05-26  |       223 |
| 1.25    | 2015-05-26  |      4319 |
| 1.25.1  | 2015-06-02  |       197 |
-------------------------------------

Notes about the resulting table:

- If a column contains numeric data (integer and/or floating point
  numbers) in all rows (ignoring column names of course) then the content
  of that column is right-aligned, as can be seen in the example above. The
  idea here is to make it easier to compare the numbers in different
  columns to each other.

- The column names are highlighted in color so they stand out a bit more
  (see also :data:`.HIGHLIGHT_COLOR`). The following screen shot shows what
  that looks like (my terminals are always set to white text on a black
  background):

  .. image:: images/pretty-table.png
T)
expandtabsr           r   )r   r   highlight_column_nameinsertcollectionsdefaultdictintlist	enumerater   r   appendboolNUMERIC_DATA_PATTERNmatchr   sumvalueslenalljoin)r!   r"   horizontal_barvertical_barr   nwidthsnumeric_data	row_indexrowcolumn_indexcolumnline_delimiterlineslinepaddings                   r   r
   r
   h   s   h <@@4aaD14D@(6'))BNO,Q 5a 8,OKK<($$S)F**40L#D/	$-cN L#&v';Z=O#PF  Y!^*11$7K7Q7QR\]cRd7e2fg %3 * $s6==?';c&kAo'MPQ'QRNE#D/	~$-cN LV1Jv4FFGG<-..C'MF2S89C&L72S89KK% %3 	SXXd^$<INLL( * 
LL ::eG A
  Ps   IIc                    / n[        U5       Vs/ s H  nSU-  PM
     nn[        5       (       a  U Vs/ s H  n[        U5      PM     nnU  H  n/ n[        [        U5      5       Hw  u  pgUR	                  5       nSU;  a  UR                  X   < SU< 35        M7  UR                  X   5        UR                  UR                  5       R                  5       5        My     UR                  U5        M     [        5       u  p[        S U 5       5      nSS[        X5      -  -  nUR                  SS5        UR                  S5        UR                  S	 U 5       5      R	                  5       $ s  snf s  snf )
a@  
Render tabular data with one column per line (allowing columns with line breaks).

:param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
             containing the rows of the table, where each row is an
             iterable containing the columns of the table (strings).
:param column_names: An iterable of column names (strings).
:returns: The rendered table (a string).

Here's an example:

>>> from humanfriendly.tables import format_robust_table
>>> column_names = ['Version', 'Uploaded on', 'Downloads']
>>> humanfriendly_releases = [
... ['1.23', '2015-05-25', '218'],
... ['1.23.1', '2015-05-26', '1354'],
... ['1.24', '2015-05-26', '223'],
... ['1.25', '2015-05-26', '4319'],
... ['1.25.1', '2015-06-02', '197'],
... ]
>>> print(format_robust_table(humanfriendly_releases, column_names))
-----------------------
Version: 1.23
Uploaded on: 2015-05-25
Downloads: 218
-----------------------
Version: 1.23.1
Uploaded on: 2015-05-26
Downloads: 1354
-----------------------
Version: 1.24
Uploaded on: 2015-05-26
Downloads: 223
-----------------------
Version: 1.25
Uploaded on: 2015-05-26
Downloads: 4319
-----------------------
Version: 1.25.1
Uploaded on: 2015-06-02
Downloads: 197
-----------------------

The column names are highlighted in bold font and color so they stand out a
bit more (see :data:`.HIGHLIGHT_COLOR`).
z%s:r   r+   c              3   T   #    U  H  n[        [        [        U5      5      v   M      g 7fN)r   r   r   )r   rG   s     r   r   &format_robust_table.<locals>.<genexpr>  s     Gus3z5122s   &(z
%s
-r   r,   c              3   D   #    U  H  nS R                  U5      v   M     g7fr   r<   )r   bs     r   r   rM     s     8A%**Q--    )r   r   r-   r3   stripr4   extendrstripr    r   r   minr.   r<   )r!   r"   blocksr?   rC   rG   rD   column_textstripped_columnr%   r&   longest_line	delimiters                r   r   r      sO   ^ F'8'FG'F!EAI'FLG!!:FG,Q-a0,G )23DS3I)J%L)//1O?*(BOTU \78[//1<<>? *K 	e   /0HGGGLS3|#AABI
MM!R
MM">>888>>@@9 HGs
   E.E3c                    U  Vs/ s H  n[        U5      PM     n nU(       a  U R                  S[        U5      5        [        R                  " [        5      nU  H0  n[        U5       H  u  pV[        X5   [        U5      5      X5'   M      M2     U  H@  n[        U5       H.  u  pVU[        U5      S-
  :  d  M  UR                  X5   5      XE'   M0     MB     [        UR                  5       5       VVs/ s H
  u  pxSU-  PM     n	nnU(       a  U R                  SU	5        U R                  SU	5        U R                  U	5        SR                  S U  5       5      $ s  snf s  snnf )aI  
Render a table in reStructuredText_ format.

:param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
             containing the rows of the table, where each row is an
             iterable containing the columns of the table (strings).
:param column_names: An iterable of column names (strings).
:returns: The rendered table (a string).

Here's an example:

>>> from humanfriendly.tables import format_rst_table
>>> column_names = ['Version', 'Uploaded on', 'Downloads']
>>> humanfriendly_releases = [
... ['1.23', '2015-05-25', '218'],
... ['1.23.1', '2015-05-26', '1354'],
... ['1.24', '2015-05-26', '223'],
... ['1.25', '2015-05-26', '4319'],
... ['1.25.1', '2015-06-02', '197'],
... ]
>>> print(format_rst_table(humanfriendly_releases, column_names))
=======  ===========  =========
Version  Uploaded on  Downloads
=======  ===========  =========
1.23     2015-05-25   218
1.23.1   2015-05-26   1354
1.24     2015-05-26   223
1.25     2015-05-26   4319
1.25.1   2015-06-02   197
=======  ===========  =========

.. _reStructuredText: https://en.wikipedia.org/wiki/ReStructuredText
r   r*   =r   c              3   D   #    U  H  nS R                  U5      v   M     g7f)z  NrP   r   s     r   r   #format_rst_table.<locals>.<genexpr>G  s     04aTYYq\\4rR   )r   r.   r/   r0   r1   r3   r   r:   ljustsorteditemsr4   r<   )
r!   r"   r   r@   rC   indexrE   iwr[   s
             r   r   r     s%   D +//$Qa $D/A(67$$S)F&s^MEs6{;FM ,  &s^MEC1%#\\&-8
 , 
 &,FLLN%;<%;TQq%;I<Ay!KK9KK	9904000) 0 =s   E0Ec                     / nU  H6  n[        U5      nU(       a  UR                  5       nUR                  U5        M8     U$ rL   )r   r(   r4   )rC   r(   resultsvaluetexts        r   r   r   J  s=    GU#??$Dt	 
 N    c                      [        U S[        S9$ )NT)boldcolor)r   r	   )names    r   r-   r-   T  s    TO<<rj   )NrN   |rL   )F)__doc__r/   rehumanfriendly.compatr   humanfriendly.terminalr   r   r   r   r   r	   __all__compiler6   r   r
   r   r   r   r-   r   rj   r   <module>rv      s_   (  	 / " zz"23 '3TWtLA^61r=rj   