o
    i_                  
   @   s  d dl Z d dl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 d dlmZ erbd dlmZ d dlmZmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlm Z  G dd dZ!G dd dZ"G dd dZ#eG dd dZ$e$e# dddddZ%e$e! ddddZ&e$e" ddd d!Z'd"d#d$d%d&e(d' d(e)e$d)f fd*d+Z*d"d#d$d%d&e(d' d(e)e$d)f fd,d-Z+eG d.d/ d/Z,eG d0d1 d1Z-dS )2    N)Iterable)
attrgetter)TYPE_CHECKINGLiteralOptionalUnionevolve)frozen)Box)ConsoleConsoleOptionsRenderableType)PaddingDimensionsPanel)	StyleTypeTable)	HelpEntry)Rendererc                   @   s.   e Zd ZdZddedB fddZdd
dZdS )NameRendererzRenderer for parameter/command names with optional text wrapping.

    Parameters
    ----------
    max_width : int | None
        Maximum width for wrapping. If None, no wrapping is applied.
    N	max_widthc                 C   
   || _ dS )zInitialize the renderer with formatting options.

        Parameters
        ----------
        max_width : int | None
            Maximum width for wrapping. If None, no wrapping is applied.
        Nr   )selfr    r   Y/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/help/specs.py__init__       
zNameRenderer.__init__entryr   returnr   c                 C   sx   |j r	d|j nd}|jrd|jnd}|r!|r!|d | }n|p$|}| jdu r,|S tj|| jdddd}d|S )	a&  Render the names column with optional text wrapping.

        Parameters
        ----------
        entry : HelpEntry
            The table entry to render.

        Returns
        -------
        ~rich.console.RenderableType
            Combined names and shorts, optionally wrapped.
          Nz  F   )subsequent_indentbreak_on_hyphenstabsize
)namesjoinshortsr   textwrapwrap)r   r    	names_str
shorts_strtextwrappedr   r   r   __call__*   s   

zNameRenderer.__call__Nr    r   r!   r   )__name__
__module____qualname____doc__intr   r2   r   r   r   r   r      s    
r   c                   @   s*   e Zd ZdZddefddZdd
dZdS )DescriptionRenderera  Renderer for descriptions with configurable metadata formatting.

    Parameters
    ----------
    newline_metadata : bool
        If True, display metadata (choices, env vars, defaults) on separate lines.
        If False (default), display metadata inline with the description.
    Fnewline_metadatac                 C   r   )zInitialize the renderer with formatting options.

        Parameters
        ----------
        newline_metadata : bool
            If True, display metadata on separate lines instead of inline.
        N)r;   )r   r;   r   r   r   r   W   r   zDescriptionRenderer.__init__r    r   r!   r   c                 C   sv  ddl m} ddlm} |j}|du r|| }nt||s7t|jdr)||}nddl m} ||t|}g }|jrNd	|j}|
|d| dd	 |jrcd	|j}|
|d
| dd	 |jduru|
|d|j dd	 |jr|
|dd | jr|rddlm} ddl m} g }	|jr|	
|j |D ]}
|	
|
 q|	r||	 S | S |D ]}
|
|
 q|S )a  Render parameter description with metadata annotations.

        Enriches the base description with choices, environment variables,
        default values, and required status.

        Parameters
        ----------
        entry : HelpEntry
            The table entry to render.

        Returns
        -------
        ~rich.console.RenderableType
            Description with appended metadata.
        r   )Text)
InlineTextN__rich_console__z, z
[choices: ]dimz
[env var: z
[default: z
[required]zdim red)Group)	rich.textr<   cyclopts.help.inline_textr=   description
isinstancehasattrstrchoicesr*   appendenv_vardefaultrequiredr;   rich.consolerA   primary_renderable)r   r    r<   r=   rD   metadata_itemschoices_strenv_vars_str	RichGrouprenderablesitemr   r   r   r2   a   sB   



zDescriptionRenderer.__call__N)Fr4   )r5   r6   r7   r8   boolr   r2   r   r   r   r   r:   M   s    	
r:   c                   @   s   e Zd ZdZd	ddZdS )
AsteriskRendererzRenderer for required parameter asterisk indicator.

    A simple renderer that displays an asterisk (*) for required parameters.
    r    r   r!   r   c                 C   s   |j rdS dS )a,  Render an asterisk for required parameters.

        Parameters
        ----------
        entry : HelpEntry
            The table entry to render.

        Returns
        -------
        ~rich.console.RenderableType
            An asterisk if the entry is required, empty string otherwise.
        *r#   rL   )r   r    r   r   r   r2      s   zAsteriskRenderer.__call__Nr4   )r5   r6   r7   r8   r2   r   r   r   r   rV      s    rV   c                   @   s&  e Zd ZU dZeedf ed< 	 dZeed< 	 dZeed< 	 dZ	e
d ed	< 	 dZe
d ed
< 	 dZe
d ed< 	 dZed ed< 	 dZed ed< 	 dZed ed< 	 dZedB ed< 	 dZedB ed< 	 dZedB ed< 	 dZedB ed< 	 dZeed< 	 dZedB ed< 	 d$d d!Zd"d# ZdS )%
ColumnSpeca  Specification for a single column in a help table.

    Used by :class:`~cyclopts.help.formatters.default.DefaultFormatter` to define
    how individual columns are rendered in help tables. Each column can have its
    own renderer, styling, and layout properties.

    See Also
    --------
    ~cyclopts.help.formatters.default.DefaultFormatter : The formatter that uses these specs.
    ~cyclopts.help.specs.TableSpec : Specification for the entire table.
    ~cyclopts.help.specs.PanelSpec : Specification for the outer panel.
    r   rendererr#   headerfooterNr   header_stylefooter_stylestyleleft)rK   r`   centerrightfulljustifytop)re   middlebottomverticalellipsis)foldcropri   ignoreoverflowwidth	min_widthr   ratioFno_wrap	highlightr    r   r!   r   c                 C   sF   t | jtrt| j|}nt| jr| |}nd}|du r!dS |S )zRender the cell content based on the renderer type.

        If renderer is a string, retrieves that attribute from the entry.
        If renderer is callable, calls it with the entry.
        Nr#   )rE   rZ   rG   r   callable)r   r    valuer   r   r   _render_cellL  s   
zColumnSpec._render_cellc                 K      t | fi |S r3   r   r   kwargsr   r   r   copyZ     zColumnSpec.copyr4   )r5   r6   r7   r8   r   rG   __annotations__r[   r\   r]   r   r^   r_   rd   r   rh   rm   rn   r9   ro   r   rp   rq   rU   rr   ru   ry   r   r   r   r   rY      sD   
 
rY   r#   r`      zred bold)rZ   r[   rd   rn   r_   Optioncyan)rZ   r[   rd   r_   Descriptionrj   )rZ   r[   rd   rm   consoler   optionsr   entriesr   r!   .c                 C   s0   t | jd }tt|dddd|d}|tfS )a  Get default column specifications for command display.

    Parameters
    ----------
    console : ~rich.console.Console
        Rich console for width calculations.
    options : ~rich.console.ConsoleOptions
        Console rendering options.
    entries : list[HelpEntry]
        Command entries to display.

    Returns
    -------
    tuple[ColumnSpec, ...]
        Column specifications for command table.
    ffffff?r   Commandr`   r~   rZ   r[   rd   r_   r   )mathceilrn   rY   r   DescriptionColumn)r   r   r   r   command_columnr   r   r   get_default_command_columnsq  s   	r   c                 C   sL   t | jd }tt|dddd|d}tdd |D r"t|tfS |tfS )	a  Get default column specifications for parameter display.

    Parameters
    ----------
    console : ~rich.console.Console
        Rich console for width calculations.
    options : ~rich.console.ConsoleOptions
        Console rendering options.
    entries : list[HelpEntry]
        Parameter entries to display.

    Returns
    -------
    tuple[ColumnSpec, ...]
        Column specifications for parameter table.
    r   r   r}   r`   r~   r   c                 s   s    | ]}|j V  qd S r3   rX   ).0xr   r   r   	<genexpr>  s    z0get_default_parameter_columns.<locals>.<genexpr>)r   r   rn   rY   r   anyAsteriskColumnr   )r   r   r   r   name_columnr   r   r   get_default_parameter_columns  s    r   c                   @   s`  e Zd ZU dZdZedB ed< 	 dZedB ed< 	 dZe	d ed< 	 dZ
e	d ed< 	 dZe	d ed< 	 dZe	d ed	< 	 dZe	d
 ed< 	 dZeed< 	 dZeed< 	 dZeed< 	 dZeed< 	 dZeed< 	 dZeed< 	 dZded< 	 dZeed< 	 dZedB ed< 	 dZedB ed< 	 dZedB ed< 	 deedf ded dd fd!d"Zd#d$ ZdS )%	TableSpeca$  Specification for table layout and styling.

    Used by :class:`~cyclopts.help.formatters.default.DefaultFormatter` to control
    the appearance of tables that display commands and parameters. This spec defines
    table-wide properties like borders, headers, and padding.

    See Also
    --------
    ~cyclopts.help.formatters.default.DefaultFormatter : The formatter that uses these specs.
    ~cyclopts.help.specs.ColumnSpec : Specification for individual columns.
    ~cyclopts.help.specs.PanelSpec : Specification for the outer panel.
    Ntitlecaptionr   r_   border_styler]   r^   r   boxFshow_headershow_footer
show_linesT	show_edgeexpandpad_edge)r      r   r   r   paddingcollapse_paddingrn   ro   safe_boxcolumns.r   r   r!   r   c                    sj  | j }|rtdd |D rd}i d| jd| jd| jd| jd| jd	| jd
| jd|d| j	d| j
d| jd| jd| jd| jd| jd| jd| jd| ji}|| ddlm} |di |}|D ]1}|j|j|j|j|j|j|j|j|j|j|j|j|jd}	|jdur|j|	d< |jdi |	 qn|D ]  fdd|D }
|j |
  q|S )a  Construct and populate a rich.Table.

        Parameters
        ----------
        columns : tuple[ColumnSpec, ...]
            Column specifications defining the table structure.
        entries : Iterable[HelpEntry]
            Table entries to populate the table with.
        **overrides
            Per-render overrides for table settings.

        Returns
        -------
        Table
            A populated Rich Table.
        c                 s   s    | ]}|j  V  qd S r3   )r[   r   colr   r   r   r   Q  s    z"TableSpec.build.<locals>.<genexpr>Fr   r   r_   r   r]   r^   r   r   r   r   r   r   r   r   r   rn   ro   r   r   r   )r[   r\   r]   r^   r_   rd   rh   rm   rn   ro   r   rp   rq   Nrr   c                    s   g | ]}|  qS r   )ru   r   er   r   
<listcomp>  s    z#TableSpec.build.<locals>.<listcomp>r   )!r   allr   r   r_   r   r]   r^   r   r   r   r   r   r   r   r   rn   ro   r   update
rich.tabler   r[   r\   rd   rh   rm   r   rp   rq   rr   
add_columnadd_row)r   r   r   	overridesr   optsr   tablecolumncol_optscellsr   r   r   build8  s   	



zTableSpec.buildc                 K   rv   r3   r   rw   r   r   r   ry     rz   zTableSpec.copy) r5   r6   r7   r8   r   rG   r{   r   r_   r   r   r]   r^   r   r   rU   r   r   r   r   r   r   r   rn   r9   ro   r   tuplerY   r   r   ry   r   r   r   r   r     s\   
 

Rr   c                   @   s  e Zd ZU dZdZed ed< 	 dZed ed< 	 dZe	d ed< 	 d	Z
e	d ed
< 	 dZed ed< 	 dZed ed< 	 dZed ed< 	 dZded< 	 dZeed< 	 dZedB ed< 	 dZedB ed< 	 dZedB ed< 	 dZeed< 	 d"ddZd d! ZdS )#	PanelSpeca"  Specification for panel (outer box) styling.

    Used by :class:`~cyclopts.help.formatters.default.DefaultFormatter` to control
    the appearance of the outer panel that wraps help sections. This spec defines
    the panel's border, title, subtitle, and overall styling.

    See Also
    --------
    ~cyclopts.help.formatters.default.DefaultFormatter : The formatter that uses these specs.
    ~cyclopts.help.specs.TableSpec : Specification for the inner table.
    ~cyclopts.help.specs.ColumnSpec : Specification for individual columns.
    Nr   r   subtitler`   )r`   ra   rb   title_alignra   subtitle_alignnoner   r_   r   r   r   )r   r|   r   r   Tr   rn   heightr   Frr   
renderabler!   r   c                 K   s   | j }|du rddlm} |}| j| j| j| j|| j| j| j	| j
| j| jd}| jdur1| j|d< | jdur;| j|d< || ddlm} ||fi |S )zKCreate a Panel around `renderable`. Use kwargs to override spec per render.Nr   )ROUNDED)r   r   r_   r   r   r   r   rn   r   r   rr   r   r   r   )r   rich.boxr   r   r   r_   r   r   r   rn   r   r   rr   r   r   r   
rich.panelr   )r   r   r   r   r   r   r   r   r   r   r     s.   




zPanelSpec.buildc                 K   rv   r3   r   rw   r   r   r   ry     rz   zPanelSpec.copy)r   r   r!   r   )r5   r6   r7   r8   r   r   r{   r   r   r   r   r_   r   r   r   r   rU   rn   r9   r   r   rr   r   ry   r   r   r   r   r     s<   
 
!r   ).r   r,   collections.abcr   operatorr   typingr   r   r   r   attrsr	   cyclopts.utilsr
   r   r   rM   r   r   r   rich.paddingr   r   r   
rich.styler   r   r   cyclopts.helpr   cyclopts.help.protocolsr   r   r:   rV   rY   r   
NameColumnr   listr   r   r   r   r   r   r   r   r   <module>   sv    6b 

"

) R