U cf @sddlmZmZmZddlmZmZmZmZm Z m Z m Z m Z m Z ddlmZmZddlmZmZddlmZddlmZmZddlmZdd lmZdd lmZdd lm Z m!Z!dd l"m#Z#dd l$m%Z%ddl&m'Z'm(Z(ddl)m*Z*m+Z+erddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2eGdddZ3eGdddZ4Gddde Z5GdddeZ6e7dkrddl8m-Z-ddl9m:Z:ddl;m6Z6ddle>j?d#d$d%d&d'e>j?d(d)d*e>j?d+d!d,d-e>@d.d/d0e>@d1d2d3e>j@d4d5d6d7d&d8e>@d9d:d;eAdd?ZBe-Z,e:ZCeBd@e,jDe>dAdBd&e>_EeBdCe,De>dDe>_FeBdEe,jDe>dAdBd_FdFe>_EdGdHge>_GeBdIe,jDe>dAdBd_FdFe>_EdGdHge>_Gde>_HeBdJe,jDe>dAdBd_FdFe>_EdGdHge>_Gd&e>_Ide>_HeBdKe,jDe>dAdBW5QRXdr?rArArBcellsvsz Column.cellscCs |jdk S)z!Check if this column is flexible.N)r7r?rArArBflexible{szColumn.flexible) __name__ __module__ __qualname____doc__r)__annotations__r*r+rr,r-r/r1r3r4r intr5r6r7r8boolr9rlistr;rrCpropertyrrDrErArArArBr'&s*           r'c@s.eZdZUdZdZeeed<dZe ed<dS)RowzInformation regarding a row.Nr-F end_section) rFrGrHrIr-r rrJrPrLrArArArBrOs  rOc@s*eZdZUdZeed<ded<eed<dS)_CellzA single cell in a table.r-r% renderabler1N)rFrGrHrIrrJrrArArArBrQs rQc@seZdZUdZeeed<eeed<ddddej ddddddddddd dd d dddd d dd e ee fe e e e e ee ee eje eeeeeeeeeeee eee ee ee ee ee eddeddddZeddddde ee feeeeddddZeedddZejeddddZeedddZeedddZdeed d!d"Zdd#ed$d%d&Zeeeeeefdd'd(Z e jedd)d*d(Z dPdddd,d-d.dddddd/ d0d0e ee ee edd1d2e ee ee ee eedd3d4d5Z!ddd6e d0e eedd7d8d9Z"ddd:d;Z#dd#dZ$dd#eed$d?d@Z%eeeeeeeedAdBdCZ&deeee'dDdEdFZ(eedGdHdIZ)dd#eedJdKdLZ*dd#eedOsz*Table.__rich_measure__..css|] }|jVqdSN)minimumr measurementrArArB Tsz)Table.__rich_measure__..css|] }|jVqdSrmaximumrrArArBrWs) r6r4rrysum_calculate_column_widthsrrrTclampr5)r@r{r extra_width measurements minimum_width maximum_widthrrArrB__rich_measure__>s2     zTable.__rich_measure__cCs|jS)zGet cell padding.)rmr?rArArBr[_sz Table.padding)r[r=cCst||_|S)zSet cell padding.)rrlrm)r@r[rArArBr[ds r(r.r0r2) r+r,r-r/r1r3r4r5r6r7r8r%rr$)r)r*r+r,r-r/r1r3r4r5r6r7r8r=c CsFtt|j|||pd|pd|p d|||| | | | | d}|j|dS)aAdd a column to the table. Args: header (RenderableType, optional): Text or renderable for the header. Defaults to "". footer (RenderableType, optional): Text or renderable for the footer. Defaults to "". header_style (Union[str, Style], optional): Style for the header, or None for default. Defaults to None. footer_style (Union[str, Style], optional): Style for the footer, or None for default. Defaults to None. style (Union[str, Style], optional): Style for the column cells, or None for default. Defaults to None. justify (JustifyMethod, optional): Alignment for cells. Defaults to "left". vertical (VerticalAlignMethod, optional): Vertical alignment, one of "top", "middle", or "bottom". Defaults to "top". overflow (OverflowMethod): Overflow method: "crop", "fold", "ellipsis". Defaults to "ellipsis". width (int, optional): Desired width of column in characters, or None to fit to contents. Defaults to None. min_width (Optional[int], optional): Minimum width of column, or ``None`` for no minimum. Defaults to None. max_width (Optional[int], optional): Maximum width of column, or ``None`` for no maximum. Defaults to None. ratio (int, optional): Flexible ratio for the column (requires ``Table.expand`` or ``Table.width``). Defaults to None. no_wrap (bool, optional): Set to ``True`` to disable wrapping of this column. r()r9r)r*r+r,r-r/r1r3r4r5r6r7r8N)r'rsrTro)r@r)r*r+r,r-r/r1r3r4r5r6r7r8rrArArBrrjs"%zTable.add_columnr-rP) renderablesr-rPr=c Gstddddd}t|}|j}t|t|krJ|dgt|t|}t|D]\}}|t|krt|d} |jD]} || tdqv|j| n||} |dkr|| dqRt|r|| |qRt dt |j d qR|jt ||d dS) aAdd a row of renderables. Args: *renderables (None or renderable): Each cell in a row must be a renderable object (including str), or ``None`` for a blank cell. style (StyleType, optional): An optional style to apply to the entire row. Defaults to None. end_section (bool, optional): End a section and draw a line. Defaults to False. Raises: errors.NotRenderableError: If you add something that can't be rendered. r%N)rrRr=cSs|j|dSr)r;ro)rrRrArArBadd_cellszTable.add_row..add_cell)r9r(zunable to render z1; a string or other renderable object is requiredr)r'rMrTrs enumeraterUrrorrNotRenderableErrortyperFrO) r@r-rPrrcell_renderablesrTr|rRr_rArArBadd_rows,     z Table.add_rowcCs|jrd|jd_dS)z2Add a new section (draw a line after current row).TN)rUrPr?rArArB add_sectionszTable.add_sectionr&c#s|jstdVdS|j}|jdk r*|j}|j}||||}t||}|j||j ddd t t dddfdd }|j r||j t |jd |jd EdH||EdH|jr||jt |jd |jd EdHdS) N )r4rjheightrWr#r&)textr-r/r=cs4t|trj||ddn|}j|j|ddS)NF)r-rjr/)r)rprq render_strrenderupdate)rr-r/ render_textr{render_optionsrArBrender_annotations z1Table.__rich_console__..render_annotationz table.title)r-r/z table.caption)rW)rTrr6r4ryrrrrrjr rrXr pick_firstrfrh_renderrYrgri)r@r{rr6rwidths table_widthrrArrB__rich_console__sJ        zTable.__rich_console__csj}j}fdd|D}dd|D}jj}jrdd|D}t|rddt||D} fdd|D} |t| } t| || } t | } t |D]"\}}|j r| |t | ||<qt|}||krX |dd|D|}t|}||kr.||}t|dgt|||}t|}fd dt||D}d d|D}||krjjsjd k r|j|krȈjd kr|ntj||}t|||}d dt||D}|S) zNCalculate the widths of each column, including padding, not including borders.csg|]}|qSrA)rrr{rr@rArBr sz2Table._calculate_column_widths..cSsg|]}|jpdqSrrr_rangerArArBrscSsg|]}|jr|jpdqSr)rEr7)rcolrArArBrscSs g|]\}}|jrdn|jqSr)rEr)rrrrArArBrscs&g|]}|jr|jpd|jqSr)rEr4r9r)get_padding_widthrArBrscSsg|]}|jdko|j qSr)r4r8rrArArBr(srcs$g|]\}}||qSrA)rr)rr4rrrArBr2scSsg|]}|jpdqSrrrrArArBr6sNcSsg|]\}}||qSrArA)r_widthpadrArArBrAs)r6rT_get_padding_widthryr^anyziprriterrrEnext_collapse_widthsrrsr5min)r@r{rr6rT width_rangesrrratios fixed_widths flex_minimumflexible_width flex_widthsiter_flex_widthsr|rr excess_width _max_width pad_widthsrA)r{rrr@rBrsf       zTable._calculate_column_widths)rwrapabler6r=c st|}||}t|r|r|dkrtddt||Dtfddt||D}|}fddt||D}t|r|sqt||gt|} t||| |}t|}||}q|S)aMReduce widths so that the total is under max_width. Args: widths (List[int]): List of widths. wrapable (List[bool]): List of booleans that indicate if a column may shrink. max_width (int): Maximum width to reduce to. Returns: List[int]: A new list of widths. rcss|]\}}|r|VqdSrrArr4 allow_wraprArArBrWsz)Table._collapse_widths..c3s&|]\}}|r|kr|ndVqdS)rNrAr max_columnrArBrZscs$g|]\}}|kr|rdndqS)rrrArrrArBr_sz*Table._collapse_widths..)rrmaxrrrsr) rvrrr6 total_widthrsecond_max_columncolumn_differencer max_reducerArrBrEs*     zTable._collapse_widths)r{ column_indexrr=c#st|j|j|jt}|dk|t|jdkitttttttfdfdd }g}|j }|j }|j r||j pd||j } || |j f||jpd} |jD]} || | fq|jr||jpd||j} || |jf|rBt} t|D]:\}}\}}t|| ||||t|ddp6|jVqn.|D](\}}t||t|ddpf|jVqFdS) z3Get all the cells with padding and optional header.rr) first_rowlast_rowr=cs||f}|r|S\}}}}rJs8td||}|sJtd||}snrVd}r^d}|rfd}|rnd}||||f}|||f<|S)Nr)getr)rrcachedr0rightbottomr.rm_padding_cacher\ first_column last_columnr]r[rArB get_padding{s*   z%Table._get_cells..get_paddingr(r1N)r\r]r[rrsrTrLr rKror~r_r+r)r-rDr`r,r*rrrQgetattrr1)r@r{rr any_paddingr raw_cells_appendr~r+ cell_stylecellr,_Paddingfirstlastr-rRrArrB _get_cellslsL,   zTable._get_cells)rr=cCs2|j\}}}}|jr*|dkr*td||}||S)zGet extra width from padding.r)r[r\r)r@rr pad_rightpad_leftrArArBrs zTable._get_padding_width)r{rrr=cCs|j}|dkrtddS||j}|jdk rJt|j||j||Sg}g}|j}|j} tj} |||j|D]&} | ||| j \} } || | | qtt|rt |nd|rt |n||}| |j dkrdn|j ||jdkrdn|j|}|S)z0Get the minimum and maximum width of the column.rrN) r6rrr9r4 with_maximumrorrrRrrr5)r@r{rrr6 padding_width min_widths max_widths append_min append_maxget_render_widthr_min_maxrrArArBrs>    zTable._measure_column)r{rrr=c% #s jp d}| jpd} fddt jD}tt|} jrj jj|t j j dnd}|r j s| n|}t } j} j } j} j} j} j}t |r>|j||j||j|f|j||j||j|f|j||j||j|fg}| rB|||V| Vng} j}jtt|D]\}\}}}|or| |o|| ss j|| nd}dg}srt !n|| r|dn|t||| D]Z\}}}|j"||j#|j$|j%dd}j&|j'||jd}t(t)||*|qt(dd|Dt+t+t d t,t t+t+t d fd d fd dt|||| D|dd<|r|r| r܈|j-|d| d|V| V||rdn |rdnd\}}}|j./r|n|j.j0|j} t1D]R}!| r:|Vt2|D]$\}"}#|#|!EdH|"sB| VqB| rt|V| Vq*n0t1D]&}!|D]}#|#|!EdHq| Vq|r|r| r|j-|d| d|V| V|o|j3}$|rZ|s|s|$rZ|sZ| r"|t)|dksZ| r.sZ|rR|j-|d| d||Vn|j-|d| d|V| VqZ|r| r|4||V| VdS)Nr(c3s |]\}}||VqdSr)r)rrr)r{r@rArBrsz Table._render..)safer)r4r/r8r3rr-css|]}t|VqdSr)rs)rrrArArBr>sr)rr1r4r-r=csRr d}nrd}|dkr*|||S|dkrB|||S|||S)Nrr0middle) align_top align_middle align_bottom)rr1r4r-)_Segment footer_row header_row row_heightrArB align_cell@sz!Table._render..align_cellc s8g|]0\}}}}||j||j|qSrA) set_shaper1r-)rr4_cellrr)rrr~ max_heightrrArBrQs  z!Table._render..foot)edgerrxheadmidrow)5r~r-rerrTrMrr substituterrZr_get_plain_headed_boxrliner`rarbrc head_left head_right head_vertical foot_left foot_right foot_verticalmid_left mid_right mid_verticalget_toprrrUrr}rr/r8r3 render_linesrRrrsrorrKget_rowrstripbackground_stylerangerrP get_bottom)%r@r{rr table_stylere _column_cells row_cells_boxnew_linerTr_r`rarbrc box_segmentsrr|rrrow_cellr rDr4rrrlinesr.r_dividerdividerline_no last_cell rendered_cellrPrA) rrr{rr~rrrrr@rBrs                     "         z Table._render)r(r(),rFrGrHrIrr'rJrOr HEAVY_HEADr rqr r rKBoxrLrrrru classmethodrwrNr^setterryrzrrrr r[rrrrrrrrQrrrrrArArArBrSs8      @     ! : / 0 @' I  )rS__main__)r!)ReprHighlighter)rS)timerz Table renderzStar Wars MovieszRich example tabler)rXrYriZReleased bright_cyancyanT)r+r-r8ZTitlemagentarz Box Officegreen)r/r-z Dec 20, 2019z Star Wars: The Rise of Skywalkerz $952,110,690z May 25, 2018zSolo: A Star Wars Storyz $393,151,347z Dec 15, 2017z!Star Wars Ep. V111: The Last Jediz$1,332,539,889zon blackrz Dec 16, 2016zRogue One: A Star Wars Storyz$1,332,439,889N)rr=cCs"ttt|tdSr)r{printrulerj)rrArArBr)sr)z Example TablerWrz expand=True2zwidth=50FdimrVzrow_styles=['dim', 'none']z%leading=1, row_styles=['dim', 'none']z+show_lines=True, row_styles=['dim', 'none'])J dataclassesrrrtypingrrrrr r r r r r(rr_looprr_pickr_ratiorralignrjupyterrmeasurerr[rrprotocolrsegmentrr-rrrrr r{r!r"r#r$r%r&r'rOrQrSrFpip._vendor.rich.consoleZpip._vendor.rich.highlighterr0pip._vendor.rich.tableZ_timerr1tablerrrrqr)rjr6r^r4rdrcrbrArArArBs,        Z